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.
2277 results for "@a"
:%s/pattern/\=MyFunc(submatch(0))/g
The \= prefix in Vim's substitute replacement invokes the expression register, which can call any Vimscript function.
:g/pattern/normal @q
The :global command combined with :normal lets you execute a recorded macro on every line that matches a given pattern.
macros #macros #command-line #ex-commands #global #batch-editing
:let @q = @a . @b
Macros in Vim are stored as plain text in named registers.
:let @q = "dd"
Macros are just strings stored in named registers.
qa0f lll i-<Esc>llli-<Esc>jq
Record a macro that positions the cursor in a 10-digit number and inserts dashes at positions 3 and 6 to create the format xxx-xxx-xxxx.
<C-v>jjxp
Visual block mode lets you select, cut, and paste rectangular columns of text.
<C-r><C-r>a
In insert mode, a pastes register a but processes the text as if typed, which can trigger abbreviations and mappings.
let mapleader = ' '
Set mapleader to space (or any key) to use as a prefix for custom mappings.
<C-v>jjll
Press for visual block mode, then use movement keys to select a rectangle.
:/start/,/end/s/pattern/replacement/g
You can restrict a substitution to a range defined by two patterns.
gqap
The gq operator reformats text by wrapping lines to fit within the textwidth setting.
:let @q='commands'
Macros in Vim are stored in registers as plain text.
qavip:sort\n}jq
Record a macro that visually selects the inner paragraph, sorts its lines, then moves to the next paragraph.
qa"bpq
While recording macro a, paste from register b with "bp.
<C-v>jjr*
Enter visual block with , select a column with j, press r followed by the replacement character.
:call timer_start(1000, {-> execute('echo "done"')})
Vim's timerstart() function lets you schedule code to run after a specified delay in milliseconds.
:bufdo %s/old/new/g
Use :bufdo to execute a command in every buffer.
nnoremap <leader>b :b#<CR>
Map b to :b# which switches to the alternate buffer.
mA
Uppercase marks (A–Z) are global marks in Vim — they persist across files and even across sessions (when viminfo or shada is configured).
:'>,'<normal @q
Running a macro over a range usually goes top to bottom, but that can break when the macro inserts or deletes lines.
macros #macros #ex-commands #visual-mode #normal-mode #refactoring