TechplexEngineer
TechplexEngineer

Reputation: 1928

Disable encryption with :X in vim

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?

Upvotes: 11

Views: 3254

Answers (4)

Ingo Karkat
Ingo Karkat

Reputation: 172698

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

Upvotes: 1

Nikita Kouevda
Nikita Kouevda

Reputation: 5756

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.

Upvotes: 7

ZyX
ZyX

Reputation: 53654

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.

Upvotes: 15

evil otto
evil otto

Reputation: 10582

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.

Upvotes: 2

Related Questions