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
.
1 Answer
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.
:help :cabbrev
.cabbrev wq+a wqa
…