search
/ and ?
/ searches forward for a regex, ? searches backward. After the search, press n to jump to the next match and N for the previous — searching is a first-class motion, so d/foo deletes up to the next 'foo'.
/{pattern}<Enter> ?{pattern}<Enter> (normal mode) Variations
| Keystroke | Effect |
|---|---|
| /\c{pattern} | Force case-insensitive search for this query |
| /\<word\> | Whole-word match |
| /{pattern}/e | Place cursor at END of match, not start |
| :set hlsearch / incsearch | Highlight matches and search as you type |
Examples
/TODO<Enter> Jump to next TODO in file
?function<Enter> Search backward for 'function'
d/;<Enter> Delete everything up to the next semicolon
Gotcha
Vim uses its own 'magic' regex flavor — \(...\) for groups, \+ for one-or-more. Prefix with \v for 'very magic' (PCRE-ish) syntax.
Related
n / N
n jumps to the next match of the last search, N to the previous. Direction is relative to the ORIGINAL search direction — if you searched with ?, then n goes backward.
* / #
* searches forward for the whole word under the cursor; # searches backward. Zero typing needed — put your cursor on the identifier and hit *.
:s and :%s
Substitute (find-and-replace) within a range of lines. Without a range :s works on the current line only; :%s uses % as shorthand for the whole file (equivalent to :1,$s). Add flags like g (all matches per line) and c (confirm).