jEdit Search & Replace Tips

This article explores, in a cookbook fashion, using jEdit's Search & Replace to do text manipulation. Using regular expressions with jEdit's Search & Replace allows one to automate simple, but often tedious & repetitive, edits.

The examples I've provided are ones that I've either found myself doing frequently or solutions to problems I felt were worth sharing. If you have examples of text manipulations you commonly do or good ways of using jEdit's Search & Replace to do text manipulation, please let me know -- I'd love to include them.

Note

This article assumes the reader has some working knowledge of regular expressions, and as such will not explain the syntax of the regular expressions used. It's meant to provide examples of using regular expressions with jEdit, not to be a tutorial on regular expressions.

Examples

Replace Multiple Spaces with a Tab

This finds multiple space characters and replaces them with a single tab character.

Search for:

[ ]{2,}

Replace with:

\t

Duplicate Lines, in Order

Replaces every line with itself, a newline, and a copy of the line.

Search for:

^(.*)$

Replace with:

$1\n$1

Remove Empty Lines

This finds multiple newline characters and replaces them with a single newline character. Regardless of whether a file uses \n, \r\n, or \r, jEdit uses \n internally

Search for:

\n{2,}

Replace with:

\n

Prepending Text to Each Line

Literally, this finds the start of each line and replaces it with text. However, as the start of the line can't actually be replaced, in practice, the replacement text is prepended to the line.

Search for:

^

Replace with:

[text to prepend]

Appending Text to Each Line

Literally, this finds the end of each line and replaces it with text. However, as the end of the line can't actually be replaced, in practice, the replacement text is appended to the line.

Search for:

$

Replace with:

[text to append]

Transposing Columnar Data

This demonstrates moving text within a line. For example, let's assume you have a tab delimited text file, and you'd like to move data in column 2 to column 3, and vice-versa.

Search for:

^\t(.+)\t(.+)\t(.+)$

Replace with:

\t$1\t$3\t$2

Multiple Columns of Data Into One

For example if have:

one     two
three   four

and you want:

one
two
three
four

Search for:

\s+

Replace with:

\n

Indenting Comments

If you have comments like //this is a comment and you want a space after //:

Search for:

//([A-z])

Replace with:

// $1

Generating SQL Statements

In essence, this demonstrates using jEdit's Search & Replace as a very basic template processor, using the contents of the buffer as the input. In this recipe, we're generating SQL statements, which I've found handy for quick data manipulations, but this technique can also applied for other basic templating tasks.

For example, if you've got a tab delimited data file with two columns of data that looks like:

1       One
2       Two
3       Three
4       Four
5       Five

To generate SQL INSERT statements for the table "Foo", using the following.

Search for:

^(\d+)\t(.+)$

Replace with:

INSERT INTO Foo VALUES($1, '$2')

Results:

INSERT INTO Foo VALUES(1, 'One')
INSERT INTO Foo VALUES(2, 'Two')
INSERT INTO Foo VALUES(3, 'Three')
INSERT INTO Foo VALUES(4, 'Four')
INSERT INTO Foo VALUES(5, 'Five')

Warning

As this will change the contents of the current buffer, be careful about ruining your data file. You may wish to copy the data file to a new buffer before doing this.

Changing a Variable Name

This recipe demonstrates a more complicated search and replace -- one we couldn't do without regular expressions. In this recipe we're renaming a private variable corresponding accessor methods in Visual Basic code. The example names are contrived, but illustrate why using regular expressions is important for doing this -- we want to make sure that we only match whole "words".

Let's say you you have a class which contains an integer property named Foo, then your class module might contain the following:

Private m_strFoo As String
Private m_strFood As String

Public Property Get Foo() As String
...
Public Property Let Foo(NewValue As String)
...
Public Property Get Food() As String
...
Public Property Let Food(NewValue As String)
...

Here are the changes we'd like to make:

Original Replacement
m_strFoo m_strZoo
Foo Zoo

However, we want to make sure we don't change m_strFood or Food.

Search for:

(m_str|\b)(Foo)\b

Replace with:

$1Zoo

Search & Replace Macros

If you find yourself doing the same text manipulations with jEdit's Search & Replace, you may wish to create a macro to save time in the future. Here's an easy way to do this.

  1. Figure out your search and replace expressions.
  2. Select Macros -> Record Macro, and enter a name.
  3. Do your Search & Replace.
  4. Select Macros -> Stop Recording.

Here's what Remove Empty Lines looks like as a recorded Beanshell macro:

// This is a recorded macro. First, check over the
// commands to make sure this is what you intended. Then,
// save this buffer, and the macro should appear in the
// Macros menu.
SearchAndReplace.setSearchString("\\n{2,}");
SearchAndReplace.setReplaceString("\\n");
SearchAndReplace.setBeanShellReplace(false);
SearchAndReplace.setIgnoreCase(false);
SearchAndReplace.setRegexp(true);
SearchAndReplace.setSearchFileSet(new CurrentBufferSet());
SearchAndReplace.replaceAll(view);

Here's the Jython version of Remove Empty Lines:

from org.gjt.sp.jedit.search import *

SearchAndReplace.setSearchString('\\n{2,}')
SearchAndReplace.setReplaceString('\\n')
SearchAndReplace.setBeanShellReplace(0)
SearchAndReplace.setIgnoreCase(0)
SearchAndReplace.setRegexp(1)
SearchAndReplace.setSearchFileSet(CurrentBufferSet())
SearchAndReplace.replaceAll(init.view)

Note

As you must escape backslashes in Beanshell or Jython (though you may use raw strings in Python to avoid this) your regular expression may look different in the macro. For example, to specify \n instead of a literal newline character you must escape the backslash, like so: \\n.