I would like to make a custom single-key Vim mapping that could be used both to start recording a macro to the unnamed register (q"
) and to stop recording a macro (q
).
Can i detect from Vim script if Vim is in recording mode?
If detecting recording mode is possible, will there be any problems if my function will be evoked in the recording mode (to stop recording)? I mean, can it happen that the function call will be recorded inside the macro?
To repeat the macro execution, press @@. Finally, you can repeat the macro on all remaining lines by using Vim number<COMMAND> syntax. For example, to execute the macro on the five remaining lines, press 5@@ or 5@h.
You start recording by q <letter> and you can end it by typing q again. Recording is a really useful feature of Vim. It records everything you type. You can then replay it simply by typing @ <letter> .
Vim 8.0 on MacOS Mojave (10.14. 6) actually persists macros and named buffers automatically (by default, although I haven't looked for a way of turning this behavior off). Closing a Vim session will update the ~/. viminfo file with any named buffers / macros.
By default*, the l command is such a command, so you can use it to stop a recursive macro. If the cursor is not at the end of the line, then you just need to move it back afterwards with the command h .
Well, I don't recommend this, since starting recording is only two keypresses, and you get the flexibility of selecting a register.
That said, here's how you could do it. I don't know a way to directly detect recording mode, but you can create a mapping that will remap itself on each keypress, thus alternating between starting and stopping recording:
" Helper mappings
nnoremap @{ :nmap ; @}<CR>qq
nnoremap @} q:nmap ; @{<CR>
" Toggle recording
nmap ; @{
" Playback
nnoremap , @q
Pick your own keys to map, as suit you best.
Also, I don't recommend using "
for the recording register: it's too
easily overwritten.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With