Vim / Neovim Search, Substitute, and Macros

Search efficiently, run substitutions, and automate repetitive edits with macros.

View
StandardDetailedCompact
Export
Copy the compact sheet, download it, or print it.
Download
`D` dense toggle · `C` copy all

Search navigation

Search forward and backward and reuse patterns efficiently.

Search forward

Find the next match after the cursor.

vimANYvimsearchpattern
vim
/pattern

Type Enter to execute the search.

Search backward

Search upward through the file.

vimANYvimsearchpattern
vim
?pattern

Useful when you overshoot or are tracing a prior reference.

Jump among matches

Repeat the last search in either direction.

vimANYvimsearchnavigation
vim
n  N

`n` follows the search direction and `N` reverses it.

Search for word under cursor

Search the symbol at the cursor instantly.

vimANYvimsearchsymbol
vim
*  #

Great for moving through repeated identifiers in code.

Substitution patterns

Use Ex substitution commands for local and global refactors.

Substitute on current line

Replace matches only on the current line.

vimANYvimsubstitutereplace
vim
:s/old/new/g

The `g` flag replaces all matches on the current line.

Substitute in entire file

Perform a global replacement across the buffer.

vimANYvimsubstitutereplace
vim
:%s/old/new/gc

Add `c` to confirm each replacement interactively.

Substitute inside visual selection

Restrict replacement to a selected range.

vimANYvimsubstitutevisual
vim
:'<,'>s/old/new/g

Visual ranges are useful for safer targeted edits.

Clear search highlight

Remove visual search highlighting.

vimANYvimsearchhighlight
vim
:nohlsearch

Keeps the screen clean after repeated searching.

Macros

Record and replay repeatable editing sequences.

Record a macro

Start recording keystrokes into a register.

vimANYvimmacrosregisters
vim
qq

Start with `q{register}` and press `q` again to stop.

Play a macro

Execute a recorded macro once or many times.

vimANYvimmacrosautomation
vim
@q  10@q

Counts let you replay the macro over many lines.

Repeat last macro

Run the last-executed macro again.

vimANYvimmacrosautomation
vim
@@

Useful when stepping through repetitive records line by line.

Run a macro over a range

Apply a macro to every line in a selected range.

vimANYvimmacrosvisual
vim
:'<,'>normal @q

A very efficient way to transform multiple lines using one recording.

Recommended next

No recommendations yet.