How do I detect which register was specified before executing a custom mapping in Vim?
v:register
When writing custom mappings or operator functions, v:register gives you the register name that the user prefixed the mapping with.
v:register
When writing custom mappings or operator functions, v:register gives you the register name that the user prefixed the mapping with.
getchar()
getchar() blocks and waits for the user to press a key, returning its numeric character code.
:let @a = substitute(@a, "old", "new", "g")
When a recorded macro has a typo or wrong command buried inside it, you don't have to re-record the entire thing.
:put {reg}
The :put Ex command always inserts a register's content as a new line below the current line, regardless of whether the register holds characterwise, linewise,
editing #registers #editing #paste #ex-commands #normal-mode
"Ay{motion}
Using an uppercase register letter with any operator appends to the register instead of replacing it.
vim.highlight.on_yank()
After yanking text in Vim it can be hard to tell exactly what was captured, especially with larger motions or text objects.
:let @a = '...'
When a recorded macro contains a typo or needs a small tweak, you can modify it directly via the :let command rather than re-recording the entire sequence.
yiw{nav}viwp{nav}viwp
When you paste over a visual selection in Vim, the displaced text is moved into the unnamed register "".
registers #registers #visual-mode #editing #text-objects #normal-mode
<C-a> in insert mode
Pressing while in insert mode inserts the same text that was typed during the previous insert mode session.
:let @/ = @a
Vim's search register (@/) holds the current search pattern — the same one used by n, N, *, and hlsearch highlighting.
:put =strftime('%Y-%m-%d')
The :put = command inserts the result of a Vimscript expression directly into the buffer as a new line.
:[range]yank {reg}
Vim's :[range]yank and :[range]delete Ex commands let you capture arbitrary line ranges into a register from the command line, bypassing the need to move the cu
"*
On X11 Linux systems, there are two independent clipboard-like buffers: the primary selection (") and the clipboard ("+).
:let @q = substitute(@q, "old", "new", "g")
Vim macros are stored as plain text in registers, which means you can inspect and modify them like any other string.
:let @a = execute('messages')
The execute() function (added in Vim 8.
@+
In Vim, @{register} executes the contents of any register as a macro.
qQ (or any uppercase register letter)
When recording a macro with qq, you can append additional keystrokes to the existing macro by recording into the uppercase version of the same register.
:call setreg("a", substitute(getreg("a"), "old", "new", "g"))
The getreg() and setreg() functions let you read and write register contents as plain strings, making it possible to surgically edit a macro without re-recordin
<C-r>=input('Enter: ')<CR>
By embedding =input('prompt: ') inside a recorded macro, you can pause the macro at any point to ask for user input and insert the result.
:let i=0 | g/pattern/s/pattern/\=printf('%d', i+=1)/
By combining :let, the :g global command, and an expression substitution with \=, you can replace every match of a pattern with a unique incrementing number.