Search and Replace
Prev | Chapter 5. Editing Text | Next

## Search and Replace

### Searching For Text

Search>Find (shortcut: `C+f`) displays the search and replace dialog box.

The search string can be entered in the Search for text field. This text field remembers previously entered strings; see Appendix C, History Text Fields for details.

If text was selected in the text area and the selection does not span a line break, the selected text becomes the default search string.

If the selection spans a line break, the Search in Selection and HyperSearch buttons will be pre-selected, and the search string field will be initially blank. (See the section called “HyperSearch” for information about the HyperSearch feature.)

Selecting the Ignore case check box makes the search case insensitive - for example, searching for “Hello” will match “hello”, “HELLO” and “HeLlO”.

To search for special characters (such as newlines or non-printable characters), inexact sequences of text, or strings that span multiple lines, we use Regular Expressions. Selecting the Regular expressions check box allows special characters to be used in the search string. Regular expression syntax is described in Appendix E, Regular Expressions. If you use (groups) in the search field, you back-reference them with $1 through $9 in the replace field.

The Backward and Forward buttons specify the search direction. Note that regular expressions can only be used when searching in a forward direction.

Clicking Find will locate the next occurrence of the search string (or previous occurrence, if searching backwards). If the Keep dialog check box is selected, the dialog box will remain open after the search string has been located; otherwise, it will close.

If no occurrences could be found and the Auto wrap check box is selected, the search will automatically restart from the beginning of the buffer (or the end, if searching backwards). If Auto wrap is not selected, a confirmation dialog box is shown before restarting the search.

Search>Find Next (shortcut: `C+g`) locates the next occurrence of the most recent search string without displaying the search and replace dialog box.

Search>Find Previous (shortcut: `C+h`) locates the previous occurrence of the most recent search string without displaying the search and replace dialog box.

### Replacing Text

The replace string text field of the search dialog remembers previously entered strings; see Appendix C, History Text Fields for details.

Clicking Replace & Find will perform a replacement in the current selection and locate the next occurrence of the search string. Clicking Replace All will replace all occurrences of the search string with the replacement string in the current search scope (which is either the selection, the current buffer, or a set of buffers, as specified in the search and replace dialog box).

Occurrences of the search string can be replaced with either a replacement string, or the return value of a BeanShell script snippet. Two radio buttons in the search and replace dialog box select between the two replacement modes, which are described in detail below.

#### Text Replace

If the Text button is selected, the search string is simply replaced with the replacement string.

If regular expressions are enabled, positional parameters (`$0`, `$1`, `$2`, and so on) can be used to insert the contents of matched subexpressions in the replacement string; see Appendix E, Regular Expressions for more information.

If the search is case-insensitive, jEdit attempts to modify the case of the replacement string to match that of the particular instance of the search string being replaced. For example, searching for “label” and replacing it with “text”, will perform the following replacements:

- “String label” would become “String text”

- “setLabel” would become “setText”

- “DEFAULT_LABEL” would become “DEFAULT_TEXT”

#### BeanShell Replace

In BeanShell replacement mode, the search string is replaced with the return value of a BeanShell snippet. The following predefined variables can be referenced in the snippet:

- `_0` -- the text to be replaced

- `_1` - `_9` -- if regular expressions are enabled, these contain the values of matched subexpressions.

BeanShell syntax and features are covered in great detail in Part III, “Writing Macros”, but here are some examples:

To replace each occurrence of “Windows” with “Linux”, and each occurrence of “Linux” with “Windows”, search for the following regular expression:

    (Windows|Linux)

Replacing it with the following BeanShell snippet:

    _1.equals("Windows") ? "Linux" : "Windows"

To convert all HTML tags to lower case, search for the following regular expression:

    <\S+

Replacing it with the following BeanShell snippet:

    _0.toLowerCase()

To replace arithmetic expressions contained in curly braces with the result of evaluating the expression, search for the following regular expression:

    \{(.+?)\}

Replacing it with the following BeanShell snippet:

    eval(_1)

These examples only scratch the surface; the possibilities are endless.

### HyperSearch

If the HyperSearch check box in the search and replace dialog box is selected, clicking Find lists all occurrences of the search string, instead of locating the next match.

By default, HyperSearch results are shown in a floating window. This window can be docked using the commands in its top-left corner popup menu; see the section called “Window Docking”.

If the Multiple results check box is selected in the results window, past search results are retained.

Running searches can be stopped in the Utilities>Troubleshooting>I/O Progress Monitor dialog box.

### Multiple File Search

Search and replace commands can be performed over an arbitrary set of files in one step. The set of files to search is selected with a set of buttons in the search dialog box.

If the Current buffer button is selected, only the current buffer is searched. This is the default behavior.

If the All buffers button is selected, all open buffers whose names match the glob pattern entered in the Filter text field will be searched. See Appendix D, Glob Patterns for more information about glob patterns.

If the Directory radio button is selected, all files contained in the specified directory whose names match the glob will be searched. The directory to search in can either be entered in the Directory text field, or chosen in a file selector dialog box by clicking the Choose button next to the field. If the Search subdirectories check box is selected, all subdirectories of the specified directory will also be searched. Keep in mind that searching through directories containing many files can take a long time.

The Directory and Filter text fields remember previously entered strings; see Appendix C, History Text Fields for details.

When the search and replace dialog box is opened, the directory and file name filter fields are set to their previous values. They can be set to match the current buffer's directory and file name extension by clicking Synchronize.

Note that clicking the All Buffers or Directory radio buttons also selects the HyperSearch check box since that is what you would want, most of the time. However, the HyperSearch check box can be unchecked, for stepping through search results in multiple files one at a time.

Two convenience commands are provided for performing multiple file searches.

Search>Search in Open Buffers (shortcut: `C+e C+b`) displays the search dialog box and selects the All buffers button.

Search>Search in Directory (shortcut: `C+e C+d`) displays the search dialog box and selects the Directory button.

### The Search Bar

The search bar feature provides a convenient way to search in the current buffer without opening the search dialog box. The search bar does not support replacement or multiple file search. Previously entered strings can be recalled in the search bar with the `Up` and `Down` arrow keys; see Appendix C, History Text Fields.

By default, the search bar remains hidden until one of the quick search commands (described below) is invoked; however you can choose to have it always visible in the View pane of the Utilities>Global Options dialog box; see the section called “The View Pane”.

Search>Incremental Search Bar (shortcut: `C+COMMA`) displays the search bar if necessary, and gives it keyboard focus.

Search>Incremental Search for Word (shortcut: `A+COMMA`) behaves like the above command except it places the word at the caret in the search string field. If this command is invoked while there is a selection, the selection is placed in the search string field instead.

Unless the HyperSearch check box is selected, the search bar will perform an incremental search. In incremental search mode, the first occurrence of the search string is located in the current buffer as it is being typed. Pressing `ENTER` and `S+ENTER` searches for the next and previous occurrence, respectively. Once the desired occurrence has been located, pressing `ESCAPE` returns keyboard focus to the text area. Unless the search bar is set to be always visible (see above), pressing `ESCAPE` will also hide the search bar.

### Note

Incremental searches cannot be not recorded in macros. If your macro needs to perform a search, use the search and replace dialog box instead. See Chapter 8, Using Macros for information about macros.

Search>HyperSearch Bar (shortcut: `C+PERIOD`) displays the search bar if necessary, gives it keyboard focus, and selects the HyperSearch check box. If this command is invoked while there is a selection, the selected text will be searched for immediately and the search bar will not be shown.

If the HyperSearch check box is selected, pressing `Enter` in the search string field will perform a HyperSearch in the current buffer.

Search>HyperSearch for Word (shortcut: `A+PERIOD`) performs a HyperSearch for the word at the caret. This command does not show the search bar or give it keyboard focus.

Prev | Up | Next
Markers | Home | Chapter 6. Editing Source Code