How do I count the number of lines containing a pattern without making any replacements?
:%s/pattern//n
The n flag in the substitute command suppresses the actual replacement and instead reports the match count.
:%s/pattern//n
The n flag in the substitute command suppresses the actual replacement and instead reports the match count.
nnoremap <key> <Cmd>command<CR>
The special key (Vim 8.
config #config #macros #ex-commands #normal-mode #insert-mode
:TOhtml
The :TOhtml command converts the current buffer — complete with its syntax highlighting colors — into a standalone HTML file.
:g/pattern/join
The :g/pattern/join command combines the :global command with :join to merge every line matching a pattern with the line immediately following it.
command-line #ex-commands #editing #command-line #search #normal-mode
:update
The :update command (abbreviated :up) writes the buffer to disk only if it has been modified since the last save.
:norm! {cmd}
Both :norm and :norm! execute a sequence of Normal mode keystrokes from the command line or a script, but they differ in how they handle user-defined mappings.
:g/pattern1/s/pattern2/replacement/g
Combining :g with :s lets you apply a substitution using two independent patterns: :g selects which lines to act on, and :s controls what gets replaced within t
:call setreg("a", substitute(getreg("a"), "old", "new", "g"))
The getreg() and setreg() functions let you read and write register contents as plain strings, making it possible to surgically edit a macro without re-recordin
:right
Vim has three built-in Ex commands for text alignment that most users never discover: :right [width] right-justifies lines, :left [width] left-justifies (strips
:set wildignore
:set wildignore defines a comma-separated list of glob patterns that Vim excludes from wildcard expansion and file completion.
feedkeys()
The feedkeys({keys}, {flags}) function inserts a string of keystrokes into Vim's input queue as if the user had typed them.
map() and filter() with lambdas
Vim 8 introduced lambda expressions with the syntax {args -> expr}, enabling concise inline list transformations.
setqflist()
setqflist() lets you build the quickfix list from a Vimscript list of dictionaries rather than relying on compiler output or :vimgrep.
winsaveview() and winrestview()
When writing Vimscript functions or complex mappings that move the cursor, it is essential to restore the original view afterward so the user does not notice an
:diffget 2
When Vim's diff mode has three or more buffers open, :diffget (or do) without an argument is ambiguous — Vim cannot determine which buffer to pull from.
:set path^=./src
Vim's :set command supports three operators for modifying list-style options: += appends, -= removes, and ^= prepends.
:let i=0 | g/pattern/s/pattern/\=printf('%d', i+=1)/
By combining :let, the :g global command, and an expression substitution with \=, you can replace every match of a pattern with a unique incrementing number.
:earlier 1h
Vim's undo history is annotated with timestamps, allowing you to travel back to any point in time using :earlier and forward using :later.
:g/./,/^$/join
Hard-wrapped text (where each sentence is on its own line) is common in commit messages, email threads, and older documentation.
:%s/pattern/replacement/ge
The e flag on :substitute silences the "Pattern not found" error that Vim normally reports when a substitution has no matches.