How do I run a command without moving the cursor or changing the scroll position?
:let view=winsaveview() | {cmd} | call winrestview(view)
When writing Vimscript functions or mappings, commands like :substitute, gg, or :%normal will move the cursor and change the scroll position.
category:
command-line
tags:
#ex-commands
#editing
#vimscript
#navigation
How do I count the number of matches for a search pattern?
The :%s/pattern//gn command counts how many times a pattern appears in the file without making any changes.
category:
search
tags:
#search
#ex-commands
#editing
How do I paste the contents of a register into the command line or search prompt?
When typing an Ex command or search pattern, you often need to insert text you've already yanked or deleted.
category:
registers
tags:
#registers
#command-line
#search
#editing
How do I run a command on every line that does NOT match a pattern?
:v (short for :vglobal) is the inverse of :g.
category:
command-line
tags:
#ex-commands
#command-line
#editing
How do I turn a visual selection of lines into a numbered list?
: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
category:
visual-mode
tags:
#visual-mode
#editing
#ex-commands
#formatting
#substitute
How do I use marks to define a range for Ex commands?
Marks can be used as range specifiers in any Ex command.
category:
command-line
tags:
#marks
#command-line
#ex-commands
#ranges
#editing
How do I search and replace with confirmation for each match?
Adding the c flag to a substitute command makes Vim pause at every match and ask you whether to replace it.
category:
search
tags:
#search
#substitution
#ex-commands
#editing
How do I search and replace text only on the current line?
The :s/old/new/g command replaces all occurrences of old with new on the current line only.
category:
search
tags:
#search
#ex-commands
#editing
How do I use a register value as the replacement string in a substitution?
How it works In Vim's substitute command, the replacement string can be a Vimscript expression when prefixed with \=.
category:
registers
tags:
#registers
#search
#ex-commands
#editing
How do I delete all blocks of text between two patterns throughout a file?
The :g (global) command can operate on ranges, not just single lines.
category:
search
tags:
#search
#editing
#ex-commands
How do I change the case of text using operators and motions?
gU{motion} / gu{motion} / g~{motion}
Vim has three case operators that work with any motion or text object: gU for uppercase, gu for lowercase, and g~ for toggle case.
category:
editing
tags:
#editing
#case
#operators
#text-objects
#normal-mode
How do I use search patterns as line ranges in Ex commands to operate on a block of text?
Instead of specifying line numbers for Ex command ranges, you can use search patterns.
category:
command-line
tags:
#ex-commands
#editing
#search
#ranges
#command-line
How do I select or operate on the entire buffer as a text object?
While Vim doesn't have a built-in "entire buffer" text object, the ggVG sequence achieves it: go to the first line, enter line-wise visual mode, then select to
category:
editing
tags:
#editing
#text-objects
#visual-mode
#normal-mode
How do I search for a pattern and land on a specific line offset from the match?
/pattern/+N or /pattern/-N
Vim's search command accepts an offset that places your cursor on a line relative to the match.
category:
navigation
tags:
#navigation
#search
#offset
#motions
How do I understand which register Vim uses for each operation?
Vim has 10 types of registers, each serving a specific purpose.
category:
registers
tags:
#registers
#reference
#clipboard
#workflow
How do I edit a complex Ex command in a full editing window?
q: or <C-f> from : prompt
The command-line window (q:) opens a full Vim buffer containing your Ex command history.
category:
command-line
tags:
#command-line
#history
#editing
#workflow
How do I automatically remove trailing whitespace every time I save a file?
autocmd BufWritePre * :%s/\s\+$//e
By adding an autocmd for the BufWritePre event, you can make Vim automatically strip trailing whitespace from every line each time you save.
category:
config
tags:
#config
#autocmd
#editing
#whitespace
#vimrc
How do I open the command-line window while typing a command on the : prompt?
<C-f> (from command-line mode)
When you are partway through typing a long or complex Ex command on the : prompt, you can press to open the command-line window.
category:
search
tags:
#command-line
#ex-commands
#editing
#normal-mode
How do I search for text only within a visual selection?
The \%V atom restricts a search pattern to only match inside the most recent visual selection.
category:
search
tags:
#search
#visual-mode
#regex
#substitution
#advanced
How do I perform the same operation across multiple files using the argument list?
:args **/*.js | argdo %s/old/new/ge | update
The argument list (arglist) is Vim's mechanism for loading a set of files and running commands across all of them.
category:
command-line
tags:
#command-line
#ex-commands
#arglist
#productivity
#batch
#editing