Some user commands are intended as replacements for built-in commands,
for example :Grep
may replace :grep
. Given the existence of such
a "replacement" user command when typing :grep
it would be convenient
to have it replaced with :Grep
for the sake of muscle memory.
For other user commands a similar replacement scheme may be desirable simply to avoid having to chord with the shift key.
We can achieve this with command-line abbreviations.
function! Cabbrev(command, local) abort
let lowercase = tolower(a:command)
let rhs = "(getcmdtype() ==# ':' && getcmdline() ==# '" . lowercase . "')"
let rhs .= " ? "
let rhs .= "'" . a:command . "'"
let rhs .= " : "
let rhs .= "'" . lowercase . "'"
if !hasmapto(rhs, 'c', 1)
execute 'cnoreabbrev' (a:local ? '<buffer>' : '') '<expr>' lowercase rhs
endif
endfunction
augroup AutoAbbreviate
autocmd!
autocmd VimEnter *
\ call map(getcompletion('[A-Z]', 'command'),
\ "Cabbrev(v:val, 0)")
autocmd FileType *
\ call map(getcompletion('[A-Z]', 'command'),
\ "Cabbrev(v:val, 1)")
augroup END