Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disable encryption with :X in vim

Tags:

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?

like image 590
TechplexEngineer Avatar asked Jul 22 '13 16:07

TechplexEngineer


4 Answers

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.

like image 133
evil otto Avatar answered Nov 05 '22 19:11

evil otto


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.

like image 35
Nikita Kouevda Avatar answered Nov 05 '22 21:11

Nikita Kouevda


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.

like image 11
ZyX Avatar answered Nov 05 '22 21:11

ZyX


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
like image 1
Ingo Karkat Avatar answered Nov 05 '22 19:11

Ingo Karkat