How do I reverse the order of all lines in a file using Vim?
:g/^/m 0
This clever use of the :global command reverses every line in the current buffer.
command-line #editing #ex-commands #global #text-manipulation
:g/^/m 0
This clever use of the :global command reverses every line in the current buffer.
command-line #editing #ex-commands #global #text-manipulation
autocmd FileType python setlocal ts=4 sw=4 et
Using autocmd FileType, you can configure Vim to automatically apply buffer-local settings whenever a file of a particular type is opened.
dp and do
When comparing two files side by side in Vim's diff mode (:diffsplit or vim -d file1 file2), you often want to accept or push individual changes between the fil
gq
The gq operator reformats text to fit within your configured textwidth.
:ilist /pattern/
The :ilist command searches for a pattern not only in the current buffer but also in files referenced by #include directives (or whatever 'include' is set to).
<C-t> and <C-d>
When you're typing in insert mode and realize the current line needs more or less indentation, you don't have to leave insert mode to fix it.
zf{motion}
Vim supports several fold methods, but manual folding with zf gives you precise control over exactly which lines to collapse.
:syntax sync fromstart
Vim's syntax highlighting engine does not always parse the entire file from the beginning — it uses sync points to determine where to start parsing for the vi
:keepjumps
When writing scripts or running commands that move the cursor (like :g, :s, or :normal), Vim normally adds each cursor position to the jump list.
qaq qa...@aq @a
A recursive macro calls itself at the end of its recording, causing it to repeat indefinitely until a command inside it fails (like a search hitting the end of
:lockmarks
Many Ex commands silently adjust or delete marks as a side effect of modifying buffer content.
<C-o> (in insert mode)
Pressing while in insert mode drops you into a special "insert-normal" mode where you can execute exactly one normal mode command, then immediately return to in
<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.
:%s/\<\w\+\>/\=toupper(submatch(0))/g
The \= flag in the replacement part of :substitute tells Vim to evaluate what follows as a Vimscript expression instead of treating it as a literal string.
:g/pattern/normal A;
The :global command combined with :normal lets you execute arbitrary normal mode keystrokes on every line that matches a pattern.
command-line #global #normal-mode #editing #ex-commands #batch-editing
]m / [m / ]M / [M
When navigating large source files, ]m and [m let you jump directly to the start of the next or previous method.
<C-o>{command}
While typing in insert mode, you sometimes need to do a quick normal-mode action — center the screen, jump to a mark, or delete a word backward.
:&&
After running a :s/pattern/replacement/g command, you often need to repeat it on another line or range.
:cfdo %s/old/new/ge | update
When you grep across your project and want to perform a search-and-replace on every file that matched, :cfdo is the most efficient approach.
command-line #quickfix #substitute #search #ex-commands #editing
:set operatorfunc=MyFunc<CR>g@{motion}
Vim's operatorfunc and g@ let you define custom operators that accept any motion or text object, just like built-in operators d, c, and y.