next up previous
Next: Modes Up: Basic Editing Previous: Using the mouse

Replacement

The following commands allow you to do systematic replacement of one string or pattern with another within a given buffer.
M-q
performs a query-replace operation. It prompts for a search string and a replacement string. Terminate each of the two with a RET. The command will then display each instance of the search string found, and prompt for its disposal. The options are described below. If ARG is supplied, it will only match things surrounded by word boundaries, so that if the search string is ``top'', there will be no replacement inside the string ``stop'' or ``topping''. In standard Emacs, this is M-%.
M-Q
is the same as M-q, but replaces patterns designated by regular expressions, rather than just simple strings. The replacement string may contain instances of ` 1#1n', for na digit, which, as described in the section on regular expressions, denotes the string matched by the 8#8 regular expression in ` 1#1( 1#1)' braces in the search string. Thus, for example, the search pattern `( 1#1([a-z_][a-z0-9_]+ 1#1))' with the replacement pattern `[ 1#11]' will replace each C identifier surrounded by parentheses by the same identifier surrounded by square brackets.
By default, the replacement will preserve the case of the letters replaced if the search string or pattern has no uppercase letters, and otherwise will use the case supplied in the replacement string.

At each instance of the search string or pattern, you are prompted for an action. Here are some common ones.

SPC
replaces the indicated occurrence and goes to the next.
DEL
keeps the indicated occurrence unchanged and go to the next.
RET
exits with no further replacements.
,
makes one replacement, but waits for another SPC or DEL before moving to the next match.
.
makes one replacement and then exits.
!
replaces all remaining occurrences without prompting again.
?
prints a help message.
C-r
enters a recursive edit level. That is, you are put back in ordinary Emacs at the point of the current occurrence and can edit in the usual manner. Typing C-M-c then goes back to the query-replace command.
y
same as SPC.
n
same as DEL.
q
same as RET.

In addition to replacement, there are two often-useful commands for deleting selected lines.

M-x delete-matching-lines
prompts for a regular expression and deletes (without prompting) each line after the point that contains a match for it.
M-x delete-non-matching-lines
prompts for a regular expression and deletes each line after the point that does not contain a match for it.


next up previous
Next: Modes Up: Basic Editing Previous: Using the mouse
David Wolfe
1998-12-15