There are several ways to do this, and it is not so simple.
With commandyou need to take care of the attributes:
command! -nargs=* -complete=file -range=% -bang -bar W w
command! -bang -bar Q q
C cabbrevtraps are described in the wiki , so you need to do this as follows:
cnoreabbrev W <C-r>=(getcmdtype()==':' && getcmdpos()==1 ? 'w' : 'W')<CR>
I have a function for this purpose:
function! s:CAbbrev(from, to)
execute 'cnoreabbrev ' . a:from . ' <C-r>=(getcmdtype()==#'':'' && getcmdpos()==1 ? ' . string(a:to) . ' : ' . string(a:from) . ')<CR>'
endfunction
cmapYou need a qualifier with you <expr>, and you need more or less the same precautions as for cabbrev:
cnoremap <nowait> <expr> W getcmdtype() ==# ':' && getcmdpos() == 1 ? 'w' : 'W'
The safest way is probably cabbrev.
source
share