next up previous
Next: Deletion, insertion, and text Up: Basic Editing Previous: Regions.

Context searches.

The search commands provide a convenient way to position the cursor quickly over long distances. One can search either for specific strings or for patterns specified by regular expressions. Both kinds of searches are carried out incrementally; that is, as you type in the target string or pattern, the cursor's position is continually changed to point to the first point in the buffer (if any) that matches what you have typed so far.

searches forward incrementally.
C-s C-s
is as for C-s, but initialize the search string to the one used in the last string search.
is as for C-s, but searches for a regular expression.
C-M-s C-s
As for C-M-s, but initialize the search pattern to the last pattern used.
Search backward incrementally.
C-r C-r
As for C-r, but initialize the search string as for C-s C-s.
M-x occur
prompts for a regular expression and lists each line that follows the point and contains a match for the expression in a buffer. If you give an ARG, it will list that number of lines of context around each match.
M-x count-matches
prompts for a regular expression and displays in the echo area the number of lines following the point that contain a match for it.
M-x grep
prompts for arguments to the UNIX grep utility (which searches files for lines matching a given regular expression) and runs it asynchronously, allowing other editing while the search continues. See the command C-x ` in section [*] for a description of how to look at each of the lines found in turn.
M-x kill-grep
stops a grep that was started by M-x grep.

As you type the search string or pattern, the cursor moves in the appropriate direction to the first matching string, if any (specifically, to the right end of that string for a forward search and to the left end for a reverse search). By default, the case (upper or lower) of characters is ignored as long as the pattern you type contains no upper-case characters; `a' will each match either `a' or `A'. When the pattern contains at least one upper-case character, the search becomes case-sensitive; `a' will not match `A', nor will `A' match `a'. If matching fails at any point, you will receive a message to that effect in the echo area. While entering a search string or pattern, certain command characters have altered effects, as follows.

ends the search, leaving the point at the string found, and setting the mark at the original position of the point.
undoes the effect of the last character typed (and not previously DELed), moving the search back to wherever it was previously.
aborts the search and returns the cursor to where it was at the beginning of the search.
quotes the next character. That is, it causes the next character to be added to the search string or pattern as an ordinary character, ignoring any control action it might normally have. Use this, for example to search for a C-g character or, in a regular-expression search, to search for a `.'.
begins searching forward at the point of the cursor for the next string satisfying the search string or pattern. If used in a reverse search, therefore, this reverses the sense of the search. If used at the point of a failing search, this starts the search over at the beginning of the buffer (``wraps around'').
is like C-s, but searches in the reverse direction, and can reverse the direction of a forward search.
adds the next word beginning at the cursor to the end of the search string or pattern. It follows that this has the effect of moving the cursor forward over that word.
adds the rest of the line to the end of the current search string or pattern.
Other control characters terminate the search, and then have their ordinary effect.

Ordinary searches (C-s and C-r) treat all ordinary characters as search characters. For regular-expression searches, several of these characters have special significance. See also the on-line documentation.

matches any character, except end-of-line.
matches the beginning of a line (that is, it matches the empty string, and only at the beginning of a line.)
matches the end of a line.
] matches any of the characters between the square brackets. A range of characters may be denoted using `-', as in [a-z0-9], which denotes any digit or letter. To include `]' as one of the characters, put it first. To include `-', use `--'. To include `^ ', do not make it the first character.
[^ 5#5
] matches any of the characters not included in the `5#5'. Thus, if end-of-line is not one of the characters, this will match it.
when following another regular expression, denotes zero or more occurrences of that regular expression--in other words, an optional occurrence. This character applies to the immediately preceding regular expression; it has ``highest precedence.'' There are special parentheses (see below) for cases where this is not what you want. Hence, the pattern `.*' denotes any number of characters, other than end-of-line. The pattern `[a-z][a-z0-9_]*' denotes a letter optionally followed by string of letters, digits, and underscores.
is like `*', but denotes at least one occurrence. Thus, `[0-9]+' denotes an integer literal.
is like `*', but denotes zero or one occurrence. Hence, the pattern `[0-9]+,?' denotes an integer literal optionally followed by a comma.
1#1(5#5 1#1)
groups the items `5#5'. Hence, ` 1#1([0-9]+, 1#1)?' denotes an optional string consisting of an integer literal followed by a comma. The pattern ` 1#1(01 1#1)* denotes zero or more occurrences of the two-character string `01'.
matches the empty string at the beginning or end of a word. Hence, ` 1#1bring 1#1b' matches ``ring'' standing alone, but not ``string'' or ``rings''.
matches the empty string, provided that it is not at the beginning or end of a word.
matches a string matching either the regular expression to its left or to its right. Use ` 1#1( 1#1)' to limit what regular expressions it applies to. Thus, ` 1#1bf[a-z]+ 1#1|[0-9]+' matches any integer literal or any word that begins with `f', while ` 1#1bf 1#1([a-z]+ 1#1|[0-9]+ 1#1)' matches any ``word'' that begins with `f' and continues with either all letters or with all digits.
where n is any digit, denotes the string that matched the pattern within the 6#6 set of ` 1#1( 1#1)' brackets in the current regular expression. Thus, ` 1#1b 1#1([0-9]+ 1#1), * 1#11' matches any integer literal that is followed by a comma, an optional space, and a repetition of the same literal; it matches ``23, 23'' and ``10,10'', but not ``23, 24''.

next up previous
Next: Deletion, insertion, and text Up: Basic Editing Previous: Regions.
David Wolfe