1

After years of torture, my trusty rusty keyboard is finally waning its power—it now seems to believe that I'm jitter-clicking whenever I press q. Before getting a new one, I would like to ask if there is a way to gracefully exit vim with :qqqqq or :wqqq (or dare I dream, :qqqqqq! and :wqqqqa). I know custom commands cannot begin with a lowercase letter, for future expansion reasons; but genuinely, I highly doubt that dear Bram would be interested in creating a new command called qqqqq so we should be in the clear. Therefore, any trickeries to bypass that? If there's no magic, I suppose I'll just turn to use ZZ.

4
  • See :help :cabbrev.
    – romainl
    May 21, 2023 at 7:15
  • @romainl I don’t think it is possible to pass a regex into cabbrev, something like cabbrev wq+a wqa
    – Futarimiti
    May 21, 2023 at 10:05
  • 3
    It's not. Just make as many abbreviations as needed while you are waiting for your new keyboard to be delivered.
    – romainl
    May 21, 2023 at 10:07
  • 1
    Make use of the cmdalias plugin or vimalias plugin May 23, 2023 at 14:41

1 Answer 1

1

I suppose I agree with @romainl you need a new keyboard.

Here is my proposition:

function! CorrectCommand()
  if getcmdtype() != ':'
    return "\<CR>"
  endif

  if matchstr(getcmdline(), '^w\=q\+!\=') == ''
    return "\<CR>"
  endif

  let l:cmdline = getcmdline()

  " Parse the cmd line
  let l:newcmdline = l:cmdline
  let l:good = v:true
  if matchstr(l:cmdline, 'qq\+!\=') != ''
    let l:newcmdline = substitute(l:cmdline, 'q\+', 'q', 'g')
    let l:good = v:false
  endif

  if l:good
    return "\<CR>"
  else
    return "\<C-u>" . l:newcmdline
  endif

endfunction
cnoremap <expr> <CR> CorrectCommand()

In case your command match :w\=q\+!\= (an optional w followed by qqq followed by an optional !) the mapping will propose a correction when you hit Enter. If you like the correction you can just hit Enter once more and the command will be executed.

Your Answer

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

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