I find myself hitting :X when I mean to type :x
Is there a way to disable :X as encryption so that typing :X in vim has no effect?
I assume there is a command that can be placed in the .vimrc file?
You can use :cmap to map X into x, but there are side effects, like not being able to use the letter X anywhere
:cmap X x
For a slightly less intrusive version
:cmap X^M x^M
which will only map X to x when you hit enter immediately afterwards.
Use :cnoreabbrev to override :X with the same functionality as :x:
cnoreabbrev X x
:cnoreabbrev is preferred to :cabbrev since :x might already be remapped to something else.
Note that using cabbrev in general will affect all single-letter words X in the command line, e.g. :X X will translate to :x x, probably not what's intended. See @ZyX's answer for a fix to this.
There is a somewhat standard way of using cnoreabbrev/cnoremap for this: before replacing X with x check whether it is the only character on the command-line:
cnoremap <expr> X (getcmdtype() is# ':' && empty(getcmdline())) ? 'x' : 'X'
or
cnoreabbrev <expr> X (getcmdtype() is# ':' && getcmdline() is# 'X') ? 'x' : 'X'
. The difference is that first will prevent you from typing :Xfoo (will translate into :xfoo), second will not, but will prevent from typing :X! (will translate into :x! which indeed makes sense unlike :X!).
There is exactly no difference for searching (/X is fine), input() prompt and so on and no difference if typed X is not the first one.
The cmdalias.vim - Create aliases for Vim commands plugin provides a more robust implementation of ZyX's answer. If you don't mind installing a plugin, you get a comfortable and extensible way to define abbreviations:
:Alias X x
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With