3Monkeys on Emacs: Part 4 - Search
Previous in this series : Part 3 - Buffers and Windows
One of the most common editing task is searching. Emacs has a rich set of commands for searching text.
Incremental Search
The commands for basic search are M-x search-forward and M-x search-backward, however these are rarely used. Instead most Emacs users will prefer to use the incremental search commands M-x isearch-forward and M-x isearch-backward, C-s and C-r respectively. Incremental search has several advantages over normal search. The first you will notice is that as you begin typing a search string in the echo area after issuing C-s or C-r, is that the search is immediate. Consider the following three screen shots.

After typing “e”

After typing “ea”

After typing “early”
As you can see, with each new character added to the search string, Emacs highlights all visible occurrences of the string and repositions the cursor to the first unique occurrence. Additionally, at any point in specifying a search string issuing the C-s or C-r command will reposition the cursor to the next or previous unique occurrence of the search string.
Regexp Search
Even more powerful than the standard incremental searches, are regular expression (regexp) search. Regexp searches allow the user to use wildcards to find text patterns rather than exact or unique matches. Again these searches come in both a nonincremental and incremental form. For reference only, the commands for the nonincremental versions are M-x re-search-forward and M-x re-search-backward. The more useful incremental regular expression search commands are M-x isearch-forward-regexp or C-M-s and M-x isearch-backward-regexp which does not have a default keystroke binding.
Emacs regular expressions are almost identical to normal UNIX regular expression and those used by other tools such as vi, sed and grep. Although a thorough discussion is outside of the scope of this article, a few points about them should provide some insight. Regular expression are composed of ordinary characters such as A, b, C, 1, 2, 3, and regular expression characters. The two tables below describe both the normal regular expression characters and the set of special regular expression characters, the third table list the syntactic class characters used in the \sc and \Sc special characters.
| Regular Expresion Character | |
| Character | What it Matches |
| . | Any single character except newline |
| ^ | Start of line |
| $ | End of linr |
| * | Zero or more of the previous ordinary character |
| + | One or more of the previous ordinary character |
| ? | Zero or One of the previous ordinary character |
| [ range ] | Any character specified by range |
| [^ range ] | Any charcter not specified by range |
| \ | Used to escape or remove any special meaning from a character |
-
| Special Characters | |
| Character | What it Matches |
| \t | Tab |
| \n | Newline |
| \\ | Backslash |
| \b | Empty string at the begining or end of a word |
| \< | Empty string at the begining of a word |
| \> | Empty string at the end of a word |
| \` | Empty string at the beginning of the buffer |
| \´ | Empty string at the end of the buffer |
| \w | Any character considered part of a word |
| \W | Any character not considered part of a word |
| \| | Seperates multiple regular expressions |
| \( group \) | Treats group as a group of characters to be treated as a single entity |
| \sc | A character of a syntactic class denoted by c |
| \Sc | A character not of a syntactic class denoted by c |
-
| Syntactic Class Characters | |
| Character | Syntactic Class |
| SPACE | Whitespace |
| w | Words |
| - | Symbol names |
| . | Punctuation |
| ( | Open balanced expression |
| ) | Close balanced expression |
| “ | String delimiter |
| < | Comment start |
| > | Comment end |
An additional construct of regular expression is the prior group reference, which allows you to substitute a prior matching portion to the regular expression. For example the regular expresion \([0123456789]\)\1 would match any occurrences of two identical digets, such as 11, 22, 33 and so on. Emacs regular expressions are greedy, or in other words, tend to try to match as much as possible. So be sure to construct your regular expression with this in mind. One final point, search terms can be reused in later searches by using the M-p and M-n commands when prompted for a search term in the echo area.
Next in this series: Part 5 - Replace
Until next time.
-3Monkeys
Popularity: 26% [?]















(2 votes, average: 9.5 out of 10)
July 13th, 2007 at 7:02 am
[…] Part 4 - Search […]