How do I replace text using a custom Vimscript function in a substitution?
:%s/pattern/\=MyFunc(submatch(0))/g
The \= prefix in Vim's substitute replacement invokes the expression register, which can call any Vimscript function.
:%s/pattern/\=MyFunc(submatch(0))/g
The \= prefix in Vim's substitute replacement invokes the expression register, which can call any Vimscript function.
qq /pattern<CR> {commands} q
By incorporating a search command inside a macro, you can make it jump to the next occurrence of a pattern before performing its edits.
:let i=1 then use <C-r>=i<CR> in macro
By combining a Vimscript variable with the expression register inside a macro, you can create a counter that increments on each replay.
/\(pattern\)\@<=target or /target\(pattern\)\@=
Vim supports zero-width assertions (lookahead and lookbehind) in its regex engine.
:g/pattern/cmd1 | cmd2
The :g (global) command can execute multiple Ex commands per matching line by chaining them with .
command-line #command-line #global #ex-commands #batch-editing #advanced
d2i(
Vim text objects accept a count prefix that lets you target outer layers of nested delimiters.
onoremap ih :<C-u>execute "normal! ?^==\+$\r:noh\rkvg_"<CR>
Vim lets you define custom text objects using operator-pending mode mappings (onoremap) and visual mode mappings (vnoremap).
d<C-v>2j
Vim lets you override the natural type of any motion by pressing v, V, or between the operator and the motion.
editing #editing #motions #visual-mode #advanced #normal-mode
:%s/pattern/\=expression/g
Vim's substitute command supports expression replacements using \= in the replacement string.
:earlier {time} / :later {time}
Vim's :earlier and :later commands let you navigate the undo history by wall-clock time rather than by individual undo steps.
editing #editing #undo-redo #ex-commands #advanced #productivity
g- / g+
Vim doesn't have a simple linear undo stack — it maintains a full undo tree with branches.
editing #editing #undo-redo #normal-mode #advanced #productivity
/foo\_.*bar
Vim's default .
/pattern/+3
Vim's search command accepts an offset after the pattern that shifts where the cursor lands relative to the match.
/\%Vpattern
The \%V atom restricts a search pattern to only match inside the most recent visual selection.
qaqqa...@aq
A recursive macro calls itself at the end of its recording, creating a loop that repeats until a motion or command fails (like reaching the end of the file or f
:redir @a | {cmd} | redir END
The :redir command redirects the output of Ex commands to a register, file, or variable instead of displaying it on the screen.
command-line #command-line #ex-commands #registers #productivity #advanced
:set undofile
By default, Vim's undo history is lost when you close a file.