Next: , Previous: Changing Text, Up: Text Handling

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.

C-c /
Toggle case-sensitive search. With prefix argument, toggle vanilla/regular expression search.
<count> /<string>
To the <count>th occurrence of <string>.

Viper does not parse search patterns and does not expand special symbols found there (e.g., ‘~’ is not expanded to the result of the previous substitution).

After typing / or ? all the usual Emacs minibuffer commands, such as M-p and M-n are available. In addition, typing C-s will insert the last search string used by the Emacs incremental search command (which is bound to C-s everywhere except in this case).

<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>.