I find the safest alternative is to use an expression abbreviation:
cnoreabbrev <expr> x getcmdtype() == ":" && getcmdline() == 'x' ? 'bd' : 'x'
This will ensure the abbreviation will only be expanded to bd
when :x
is used otherwise just expand to x
.
For more help:
:h map-<expr>
:h getcmdtype()
:h getcmdline()
Upon further inspection there appears to be a plugin that does exactly this by Hari Krishna Dara called cmdalias.vim. It uses a variation of the technique above.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…