How do I count how many times a pattern appears in a file without making any changes?
:%s/pattern//gn
The n flag on the substitute command makes it report the match count without actually performing any replacement.
:%s/pattern//gn
The n flag on the substitute command makes it report the match count without actually performing any replacement.
:call setreg('"', @", 'l')
Vim registers carry not just their text content but also a type: charwise (c), linewise (l), or blockwise (b).
p (in visual mode)
In visual mode, pressing p replaces the selected text with the contents of the default register.
dp / do
When reviewing differences between files in Vim's built-in diff mode, dp and do let you selectively apply individual hunks without leaving the editor.
:sort u
The :sort u command sorts all lines in the buffer (or a selected range) alphabetically and removes duplicate lines in a single pass.
"=
The expression register ("=) lets you evaluate any Vim expression and insert its result as text.
P (in visual mode)
When you paste over a visual selection using p (lowercase), Vim replaces the selection with your register contents — but the replaced text overwrites your unn
o (visual mode)
In visual mode, pressing o swaps the cursor between the two ends of the selection (the anchor and the free end).
'[ and ']
Vim automatically sets two marks whenever you yank, change, delete, or paste text: ` [ ` (backtick-bracket) marks the start of the affected region, and ] ` mark
<C-r><C-r>{register}
In insert mode, {reg} pastes from a register but treats certain bytes as key inputs — so a register containing \n triggers a newline, \x08 triggers backspace,
:wall
When working across multiple files, you often have unsaved changes in several buffers.
:g/start/,/end/d
The :g (global) command can operate on ranges, not just single lines.
{count}<C-a> / {count}<C-x>
While increments and decrements a number by 1, you can prefix either with a count to add or subtract a specific amount.
:set matchpairs+=<:>
By default, the % command jumps between (), {}, and [] pairs.
config #navigation #config #matchpairs #editing #normal-mode
:set suffixesadd+=.js,.ts,.jsx,.tsx
The gf (go to file) command opens the file under the cursor, but it fails when the path lacks an extension — common in JavaScript/TypeScript imports like impo
navigation #navigation #config #editing #buffers #file-management
:'<,'>!sort -t',' -k2 -n
Vim does not have a built-in multi-column sort, but you can leverage the external sort command to sort selected lines by any field.
editing #editing #sorting #ex-commands #external-commands #command-line
:set complete+=kspell
Vim's built-in completion ( / ) sources matches from buffers, included files, and tags by default.
<C-k>
Vim has a built-in digraph system that lets you type special characters using short two-character codes.
:bufdo
When you need to apply the same change to every file you have open in Vim, switching to each buffer manually is tedious and error-prone.
:let view=winsaveview() | {cmd} | call winrestview(view)
When writing Vimscript functions or mappings, commands like :substitute, gg, or :%normal will move the cursor and change the scroll position.