How do I use a Vimscript expression to compute the replacement text in a substitution?
Prefixing the replacement field of :s with \= makes Vim evaluate the rest as a Vimscript expression and use the result as the replacement string.
category:
command-line
tags:
#search
#ex-commands
#editing
#command-line
How do I split a complex Vim macro into reusable subroutines?
Record worker macro in @b, call it from @a with @b
Complex macros are hard to debug and maintain when crammed into a single register.
category:
macros
tags:
#macros
#registers
#editing
How do I paste a register's contents in insert mode without triggering autoindent or other text processing?
In insert mode, {register} pastes the register's contents but runs it through Vim's insert-mode processing — including autoindent, textwidth wrapping, and for
category:
registers
tags:
#registers
#insert-mode
#editing
How do I run a normal mode command on every line matching a pattern?
Combining :global with :normal lets you run any normal-mode keystrokes on every line that matches a pattern.
category:
editing
tags:
#editing
#ex-commands
#normal-mode
#macros
How do I match a pattern only when it is preceded or followed by another pattern, without including that context in the match?
Vim's regex engine supports zero-width lookahead and lookbehind assertions using the \@ family of atoms.
category:
search
tags:
#search
#editing
#normal-mode
How do I use a Vim expression to dynamically compute the replacement in a substitution?
:s/\d\+/\=submatch(0)+1/g
The \= prefix in a :substitute replacement field tells Vim to evaluate the following as a Vimscript expression rather than treating it as a literal string.
category:
registers
tags:
#search
#editing
#ex-commands
#registers
How do I run a substitution only within the exact columns of a visual selection, not the whole line?
:'<,'>s/\%Vpattern/replacement/g
When you press : after making a visual selection, Vim inserts ' to restrict the substitution to the selected lines.
category:
visual-mode
tags:
#visual-mode
#search
#editing
#ex-commands
#normal-mode
How do I reflow and hard-wrap a paragraph or selected lines to a fixed width in Vim?
Pressing gq on a visual selection reformats the selected lines to hard-wrap at textwidth columns.
category:
visual-mode
tags:
#visual-mode
#editing
#formatting
How do I insert the current filename into the buffer or use it in commands?
Vim has several read-only registers that hold special values.
category:
registers
tags:
#registers
#insert-mode
#normal-mode
#editing
How do I insert the full WORD under the cursor (including slashes and dots) into the command line?
When you are in command-line mode, inserts the word under the cursor (alphanumeric and _ only).
category:
command-line
tags:
#command-line
#editing
#registers
How do I make Ctrl-A and Ctrl-X increment and decrement alphabetic characters?
By default, and only increment and decrement numbers (decimal, hex, and octal depending on format).
category:
config
tags:
#editing
#normal-mode
#config
#increment
How do I use a VimScript expression as the replacement in a substitution?
:s/pattern/\=expression/g
Prefixing the replacement string with \= in a :substitute command tells Vim to evaluate the rest as a VimScript expression rather than literal text.
category:
search
tags:
#search
#ex-commands
#editing
#normal-mode
How do I programmatically combine or modify the contents of Vim registers?
You can manipulate register contents directly using the :let command with the @{reg} syntax.
category:
registers
tags:
#registers
#editing
#normal-mode
#ex-commands
How do I adjust the width of a visual block selection without restarting it?
In visual block mode (), pressing O (uppercase) moves your cursor to the other end of the current line — letting you expand or contract the block's horizontal
category:
visual-mode
tags:
#visual-mode
#visual-block
#selection
#editing
How do I reindent a block of code using a visual selection?
After making a visual selection, pressing = applies Vim's auto-indent to every selected line at once.
category:
visual-mode
tags:
#visual-mode
#indentation
#editing
#formatting
How do I quickly switch between the current file and the last file I was editing?
:e # opens the alternate file — the file you were editing just before the current one.
category:
buffers-windows
tags:
#buffers
#editing
#ex-commands
#navigation
How do I insert a newline in the replacement string of a :s substitution?
In Vim's :substitute command, \r in the replacement string inserts a literal newline — it splits the line at that point.
category:
search
tags:
#search
#editing
#ex-commands
#normal-mode
How do I insert a Unicode character by its codepoint in insert mode?
In insert mode, pressing followed by u and a 4-digit hexadecimal codepoint inserts the corresponding Unicode character directly into the buffer.
category:
editing
tags:
#insert-mode
#editing
#unicode
How do I run a substitution or command on every line in the quickfix list?
:cdo {cmd} executes {cmd} on each entry in the quickfix list — one by one, jumping to each location in turn.
category:
command-line
tags:
#ex-commands
#quickfix
#search
#editing
#buffers
How do I split a line by inserting a newline with the substitute command?
In Vim's substitute command, use \r (not \n) in the replacement to insert a real newline.
category:
editing
tags:
#editing
#search
#ex-commands
#substitution