Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can i detect Vim macro recording mode from Vim script function, and call this function in Vim macro recording mode?

Tags:

vim

macros

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?

like image 761
Alexey Avatar asked Oct 27 '13 11:10

Alexey


People also ask

How do you call a macro in Vim?

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.

What is recording @Q in Vim?

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> .

Are Vim macros persistent?

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.

How do you stop a macro in Vim?

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 .


1 Answers

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.

like image 157
1983 Avatar answered Oct 03 '22 11:10

1983