6

Is there a way to programmatically check if Vim is currently in the recording mode triggered by q?

2 Answers 2

14

Since Vim 8.1-0020, there is a reg_recording() function that'll return the name of the current register being recorded. An empty string is returned if we are not recording.

5
  • That is the correct answer. Aug 6, 2018 at 12:32
  • 2
    @ChristianBrabandt. Well, it depends if the script needs to target older versions of Vim. In that case, statox' solution may be the only workaround available. Aug 6, 2018 at 12:35
  • Why is vim using underscore notations for new functions? Isn't underscore notation deprecated in vim (I noticed a lot like buffer_name() is replaced by bufname())?
    – tga
    Aug 24, 2018 at 15:11
  • @wengwengweng you should ask such questions on vim-dev mailing list Aug 24, 2018 at 17:04
  • @wengwengweng you should ask such questions on vim-dev mailing list Aug 24, 2018 at 17:05
3

Edit I wasn't aware of reg_recording() but if you have a newer version of Vim Luc's answer is clearly the best answer.

I'm not aware of a built-in way to check if Vim is recording but you could use the following workaround in your .vimrc:

let g:isRecording = get(g:, 'isRecording', 0)
nnoremap q :let g:isRecording = !g:isRecording<CR>q

The first line will create a global variable g:isRecording which is falsy by default or takes its existing value if you re-source your .vimrc.

Then you remap q to toggle the value of g:isRecording when it toggles the recording mode and you can then test g:isRecording.

That's not the most elegant solution but as :h recording doesn't seem to mention a variable which would change with q and :h autocmd-events doesn't mention an event related to recording, I guess that the easiest way to do.

2
  • 1
    I'm curious. Why have you preferred to define an autocommand to initialize the global variable instead of setting it in the vimrc? (with let g:isRecording = get(g:, 'isRecording', 0) to permit to source the vimrc as many times as we wish) Aug 6, 2018 at 12:01
  • 1
    @LucHermitte It was because I didn't think of resourcing the vimrc :)
    – statox
    Aug 6, 2018 at 13:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.