How do I use capture groups in Vim substitutions to rearrange or swap matched text?
:s/\v(\w+) (\w+)/\2 \1/
Vim's substitute command supports capture groups using \( and \) (or ( and ) in \v very-magic mode).
Search Vim Tricks
Searching...:s/\v(\w+) (\w+)/\2 \1/
Vim's substitute command supports capture groups using \( and \) (or ( and ) in \v very-magic mode).
zg and zw
When Vim's spell checker marks a word as incorrect but it is intentionally spelled that way (a name, abbreviation, or domain-specific term), you can permanently
- (vim-vinegar)
The vim-vinegar plugin by Tim Pope enhances Neovim's built-in netrw file browser.
packadd matchit
Vim ships with matchit.
:'<,'>center
After making a visual selection, typing : automatically inserts ' to scope the command to the selection.
visual-mode #visual-mode #formatting #ex-commands #indentation
[q / ]q
The vim-unimpaired plugin by Tim Pope provides bracket-pair mappings for navigating quickfix and location list entries: [q moves to the previous entry and ]q mo
:pedit
Vim has a built-in preview window — a special window distinct from regular splits.
:e +/pattern filename
The +{cmd} flag on :edit (and most file-opening commands) runs an Ex command immediately after the file loads.
command-line #command-line #ex-commands #navigation #search #buffers
gI
Most Vim users know I to insert at the start of a line — but I actually jumps to the first non-blank character, skipping leading whitespace.
# vim: set tabstop=2 shiftwidth=2 expandtab :
A modeline is a specially formatted comment that Vim reads when it opens a file and applies as local settings — no plugin or project-level vimrc required.
/\%>5l\%<10l pattern
Vim's \%>{lnum}l and \%5l — matches only at positions after line 5 (i.
ysiw"
The ysiw" command wraps the word under the cursor in double quotes using the vim-surround plugin.
:let @a = 'text'
Vim's :let command lets you assign a value directly to any named register without performing a yank or delete operation.
registers #registers #macros #vimscript #ex-commands #normal-mode
:argdo normal @a
The :argdo command applies any Ex command to every file in the argument list.
:execute
:execute evaluates a string as an Ex command, letting you build commands dynamically or embed special key sequences (like or ) as literal characters.
/\%>80v.
The pattern /\%>80v.
vip=
Pressing = in visual mode re-indents all selected lines according to the current filetype's indent rules — the same engine used by == for a single line, but a
visual-mode #visual-mode #indentation #editing #text-objects
:for i in range(1,10) | execute "normal @q" | endfor
Using a Vimscript :for loop with execute "normal @q" lets you run a macro with a dynamically computed iteration count and interleave other Ex commands between i
:put =execute('{cmd}')
The :put =execute('{cmd}') idiom inserts the output of any Vim Ex command as text in your buffer.
registers #registers #ex-commands #command-line #normal-mode
:augroup
Wrapping autocmds in a named augroup with autocmd! at the start prevents duplicate autocommands from accumulating every time your vimrc is sourced.