How do I reverse the order of all lines in a file?
:g/^/m0
The :g/^/m0 command is a clever use of Vim's global command to reverse every line in the file.
:g/^/m0
The :g/^/m0 command is a clever use of Vim's global command to reverse every line in the file.
:g/pattern/normal @a
The :g (global) command combined with :normal @a lets you execute a recorded macro only on lines matching a pattern.
macros #macros #ex-commands #global-command #editing #automation
vim: set ts=2 sw=2 :
A modeline is a special comment embedded in a file that Vim reads to apply file-specific settings automatically.
:m+1 / :m-2
The :m (move) command relocates lines to a new position in the file without using registers.
editing #editing #ex-commands #lines #productivity #mappings
g; / g,
The g; and g, commands let you navigate Vim's changelist — a per-buffer history of every position where you made a change.
navigation #navigation #changelist #editing #normal-mode #marks
:'<,'>norm {commands}
The :normal (or :norm) command lets you execute normal mode keystrokes from the command line.
command-line #editing #ex-commands #normal-mode #productivity #ranges
"%p
The % register in Vim always contains the name of the current file.
registers #registers #editing #insert-mode #productivity #filename
".p
The ".
registers #registers #editing #insert-mode #normal-mode #productivity
:w !{cmd}
The :w !{cmd} command writes the buffer contents to the stdin of an external shell command without modifying the buffer or saving to disk.
command-line #command-line #ex-commands #shell #editing #productivity
:%!{cmd}
The :%!{cmd} command pipes the entire buffer through an external shell command and replaces the buffer contents with the command's output.
command-line #editing #ex-commands #shell #filtering #productivity
zf / zo / zc / za
Vim's folding system lets you collapse blocks of code into a single line, hiding the details so you can focus on the structure.
editing #editing #folding #navigation #normal-mode #productivity
gn
The gn motion searches forward for the next match of the last search pattern and visually selects it.
search #navigation #search #motions #normal-mode #repeat #editing
<C-k>{char1}{char2}
Vim has a built-in digraph system that lets you insert hundreds of special characters by typing two-character mnemonics.
editing #editing #insert-mode #special-characters #unicode #productivity
<C-r>{register}
Pressing followed by a register name in insert mode inserts the contents of that register at the cursor position without leaving insert mode.
<C-w>
Pressing in insert mode deletes the word before the cursor instantly, without requiring you to switch to normal mode.
:'<,'>s/\n/, /g
Vim's J command joins lines with a single space, but sometimes you need a custom separator like a comma, pipe, or semicolon.
editing #editing #ex-commands #visual-mode #substitution #lines
<C-n> / <C-p>
Vim has a powerful built-in completion system that requires zero plugins.
qaYp<C-a>q99@a
By recording a macro that duplicates a line and increments its number, you can generate a numbered list of any length with a single replay command.
macros #macros #editing #normal-mode #automation #productivity
q:
The command-line window is a special buffer that shows your entire Ex command history and lets you edit entries using the full power of Vim's normal mode before
command-line #command-line #ex-commands #history #editing #productivity
<C-x><C-l>
The command triggers whole-line completion in insert mode.