How do I apply a recorded macro to every line in a visual selection?
:'<,'>norm @a
Combining :normal with a visual range lets you replay a macro on each line of a selection individually — far more targeted than recursive macros or @@ repeati
:'<,'>norm @a
Combining :normal with a visual range lets you replay a macro on each line of a selection individually — far more targeted than recursive macros or @@ repeati
:sort /regex/
The :sort /pattern/ command sorts lines by the text that appears after the first match of a pattern, not from the start of each line.
:'<,'>norm I//
After making a visual selection, :norm {commands} executes normal-mode keystrokes on every line in the range.
p (in visual mode)
In visual mode, pressing p replaces the selected text with the contents of the default register.
* (in visual mode)
In normal mode, searches for the word under the cursor with word-boundary anchors.
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).
v{motion}<C-g>
Vim has a lesser-known select mode that behaves like selection in typical GUI editors: any typed character replaces the selection.
grn / grc / grm
Neovim's nvim-treesitter plugin provides incremental selection based on the abstract syntax tree (AST) of your code.
<C-v>jjlU
Visual block mode lets you select rectangular regions of text, which means you can target a specific column and apply case changes only to that area.
:s/^/\=line('.') - line("'<") + 1 . '. '/
When you need to quickly number a set of lines — such as TODO items, steps, or bullet points — you can use a visual selection combined with a substitution e
visual-mode #visual-mode #editing #ex-commands #formatting #substitute
v_o
When you start a visual selection, the cursor is at one end and the anchor is at the other.
:'<,'>!column -t
When working with data that has uneven spacing — such as variable assignments, CSV-like data, or configuration entries — you can select the lines and pipe t
visual-mode #visual-mode #editing #formatting #external-command #alignment
v / V / <C-v> (while in visual mode)
When you are already in visual mode and realize you need a different selection type, you do not have to exit and re-enter.
gq
The gq operator reformats text to fit within your configured textwidth.
<C-\><C-n>
While works to leave insert or visual mode, it does not work in every situation — particularly in terminal mode (:terminal), where is consumed by the running
zf{motion}
Vim supports several fold methods, but manual folding with zf gives you precise control over exactly which lines to collapse.
g<C-g>
While shows basic file info (filename, line count, position), g provides a much more detailed statistical breakdown of your file or visual selection.
<C-v>$A
When you need to append text to the end of several lines that have different lengths, visual block mode with $ is the key.
<C-v>jj$A;
Visual block mode normally selects a fixed-width column, which makes appending tricky when lines have different lengths.