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 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 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 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
How do I sort lines based on a specific field or column rather than the beginning of each line?
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.
category:
command-line
tags:
#ex-commands
#editing
#visual-mode
How do I create a macro that runs continuously until it hits an error?
A recursive macro is a macro that calls itself as its last step, causing it to loop automatically until an operation fails (such as reaching the end of the file
category:
macros
tags:
#macros
#registers
#normal-mode
#editing
How do I create text shortcuts that auto-expand while typing in Vim?
:iabbrev defines insert-mode abbreviations that expand automatically when you type a non-keyword character (space, punctuation, ) after the abbreviation.
category:
config
tags:
#insert-mode
#config
#editing
How do I replace a line (or range) with the output of a shell command in Vim?
:.
category:
command-line
tags:
#ex-commands
#editing
#command-line