PREV Changing Text UP Text Handling NEXT Yanking

4.2.7: Search and Replace

See Groundwork, for Ex address syntax. See Options, to see how to get literal (non-regular-expression) search and how to stop search from wrapping around.

<count> /<string>

To the <count>th occurrence of <string>.

<count> ?<string> To the <count>th previous occurrence of <string>.
<count> g<move> Search for the text described by move. (off by default)
n Repeat latest / ? (next).
N Idem in opposite direction.
% Find the next bracket and go to its match
:[x,y]g/<string>/<cmd> Search globally [from line x to y] for <string> and execute the Ex <cmd> on each occurrence.
:[x,y]v/<string>/<cmd> Execute <cmd> on the lines that don't match.
#g<move> Execute the last keyboard macro for each line in the region. See Macros and Registers, for more info.
Q Query Replace.
:ta <name> Search in the tags file where <name> is defined (file, line), and go to it.
:[x,y]s/<pat>/<repl>/<f> Substitute (on lines x through y) the pattern <pat> (default the last pattern) with <repl>. Useful flags <f> are `g' for `global' (i.e. change every non-overlapping occurrence of <pat>) and `c' for `confirm' (type `y' to confirm a particular substitution, else `n'). Instead of / any punctuation character other than <space> <tab> and <lf> can be used as delimiter.

Note: The newline character (inserted as C-qC-j) can be used in <repl>.

& Repeat latest Ex substitute command, e.g. :s/wrong/right.
:global /<pattern>/<ex-command>
:g /<pattern>/<ex-command>
Execute <ex-command> on all lines that match <pattern>.
:vglobal /<pattern>/<ex-command>
:v /<pattern>/<ex-command>
Execute <ex-command> on all lines that do not match <pattern>.

PREV Changing Text UP Text Handling NEXT Yanking