How do I run a normal mode command on every line in a range?
:%normal command
The :normal command executes normal mode commands programmatically on a range of lines.
Search Vim Tricks
Searching...:%normal command
The :normal command executes normal mode commands programmatically on a range of lines.
:vimgrep /pattern/ **/*.ext
The :vimgrep command searches for a pattern across multiple files and populates the quickfix list with the results.
/\_.pattern
The \.
:grep pattern files
The :grep command runs an external grep tool and loads the results into Vim's quickfix list.
y/<C-r>"<CR>
To search for the exact text you have selected in visual mode, yank it and paste it into the search prompt.
/\<word\>
The \ atoms create word boundaries in a search pattern, matching only complete words and not substrings within larger words.
/\v pattern
The \v flag enables "very magic" mode in Vim regex, where most special characters work like standard regular expressions without needing backslashes.
:%s/\(group\)/\1/g
Capture groups in Vim substitute let you match parts of a pattern and reuse them in the replacement.
:%s/pattern/\r/g
In the replacement part of :s, \r inserts a newline.
:%s/\(\w\+\)/\u\1/g
Vim provides case-conversion atoms in substitute replacements: \u uppercases the next character, \l lowercases it, \U uppercases until \e, and \L lowercases unt
n
After performing a search with / or ?, pressing n repeats the search in the same direction to find the next match.
/pattern\@!
The \@! atom is a negative lookahead in Vim regex.
\@<=pattern
The \@<= atom is a positive lookbehind in Vim regex.
/pattern\ze followed
The \ze atom marks the end of the match, so you can match a pattern only when it appears before specific text.
:[{,]}s/old/new/g
By using the range [{,]}, you can limit a substitute command to the lines between the enclosing braces — effectively the current function or block.
:set hlsearch
The hlsearch option highlights all matches of the current search pattern throughout the file, making it easy to see where matches occur.
/{pattern}
The / command initiates a forward search in the file.
?{pattern}
The ? command searches backward from the cursor position.
:%s/\V literal.text/replacement/g
The \V (very nomagic) flag treats all characters as literal except for \.
`< and `>
The ` ` marks automatically track the boundaries of the last visual selection.