Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to disable a built-in command in vim

In vim, when I hit :wq it is almost always an accident that occurred when attempting to input :w. I would like to disable :wq.

The closest I found is cmap, but it has some odd behavior. If I do something like

:cmap wq w

I can no longer even input :wq; it just remaps the keystroke sequence wq to w in command mode. Now I cannot, for example, input a search/replace command on a string containing wq.

I would just like to alias the exact command :wq to :w or a no-op. Is there a way to do this?

EDIT: clarified why :cmap is not an option for me

like image 435
user85509 Avatar asked Nov 24 '09 00:11

user85509


People also ask

How do I stop a command in Vim?

Now that we are in Command-line Mode , just type in quit and press Enter or return . You may also notice the :q there, which is actually the shorthand version of the :quit command. That being said, we can also quit Vim by executing :q .

How do you unset an option in Vim?

In general any option that is a switch can be disabled by using the no word just before the name of the option (Do not use spaces between no and the option otherwise you will receive an error saying “E518: Unknown option: no”). Now the lines should no longer be there!


1 Answers

A better solution can be:

:cabbrev wq w

But I'm not sure why cmap doesn't work as excepted. Actually I had mapped one my function keys to save files:

:map <F2> :w<CR>
:nmap <F2> <ESC>:w<CR>i

UPDATE: typo corrected in the first command.

UPDATE2: possible workaround:

:cabbrev wq<CR> w

HTH

like image 86
Zsolt Botykai Avatar answered Oct 21 '22 19:10

Zsolt Botykai