You could also define 2 custom commands :Cab
and :Sab
which would make the definition of such abbreviations easier.
:Cab
would define an abbreviation which would only affect an Ex command, while :Sab
would only modify a search.
The following code tries to define these commands:
command! -nargs=+ Cab call s:StrictAbbr(<q-args>, 1)
command! -nargs=+ Sab call s:StrictAbbr(<q-args>, 0)
function! s:StrictAbbr(args, cmd) abort
let l:lhs = matchstr(a:args, '^\s*\zs\S*')
let l:rhs = matchstr(a:args, '^\s*\S*\s\+\zs.*')
if a:cmd
execute printf("cnoreabbrev <expr> %s getcmdtype() ==# ':' ? '%s' : '%s'", l:lhs, l:rhs, l:lhs)
else
execute printf("cnoreabbrev <expr> %s getcmdtype() =~ '[/?]' ? '%s' : '%s'", l:lhs, l:rhs, l:lhs)
endif
endfunction
Now to define the abbreviation mentioned in your post, you could write :
Cab csf cscope find
And to define the abbreviation which transforms fb
in foo bar
, but only in a search:
Sab fb foo bar
These abbreviations could be written anywhere in your vimrc
, but only after the previous code, otherwise you'll have an error such as:
E492: Not an editor command: Sab fb foo bar
The code is heavily inspired from this post and from the accepted answer.