Vim - mapping q only when it is not being written?

When not recording a macro, a press q

followed by a register starts recording the macro in that register. Other commands are also available, eg. q:

to open a command window.

While recording a macro, pressing q

stops the recording. In other words, the display q

behaves differently regardless of whether or not a macro is recorded. You cannot nest one entry within another.

I want to redirect q:

when not recording a macro, but keep the regular behavior q

to stop the macro if recording is currently in progress. Regular display does not work correctly - if I record a macro and then press q

there is a delay because Vim is trying to determine if I will follow it with :

to trigger the mapping. I want the mapping to be applied only when I am not recording a macro so that this delay is not triggered. How can I detect this?

My use case is that I want to q:

behave like :q

and instead displays g:

to open the command window, since half the time I q:

press I wanted to press :q

.

+3


source to share


1 answer


I believe I have found a solution based on this answer .



nnoremap <silent> q :<C-u>call <SID>SmartQ()<CR>
function! s:SmartQ()
  if exists("g:recording_macro")
    let r = g:recording_macro
    unlet g:recording_macro
    normal! q
    execute 'let @'.r.' = @'.r.'[:-2]'
  else
    let c = nr2char(getchar())
    if c == ':'
      quit
    else
      if c =~ '\v[0-9a-zA-Z"]'
        let g:recording_macro = c
      endif
      execute 'normal! q'.c
    endif
  endif
endfunction

      

+1


source







All Articles