How to disable a built-in command in vim
Asked Answered
M

3

8

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

Moersch answered 24/11, 2009 at 0:53 Comment(0)
M
0

It looks like the best option is to just get used to :cmap behavior. In the rare event I want to input the keyboard seqeunce wq I can just hit wq, wait a second, then hit q again. I did find this possible solution but it is too complex for my tastes.

Moersch answered 4/2, 2010 at 4:48 Comment(0)
N
5

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

Nasia answered 24/11, 2009 at 5:20 Comment(5)
Can you please explain :cabbrew ?Pupil
I think it should be :cabbrev. :cabbrev wq w will change wq to w after you press space or enter, so it will make :wq do :w. See :help :cabbrev and :help :ab for more information.Fugato
Sorry, I think I was not clear enough in my question. If you do :cmap and it also seems :cabbrew, it means the "wq" keystroke sequence in command mode is mapped to something other than "wq". So then you cannot, for example, do a search/replace on a string containing "wq". All I would like to do is make it so if I input the exact command :wq, it is aliased to "w" or no-op.Moersch
Thanks AI, typo corrected. I'm diving into the problem. as it's not the correct solution.Nasia
Just stopping by to say thanks - I am trying to weene myself off tabs, so doing cabbrev tabnew help!<CR> made vim give an error message every time I try to make a tab.Mastitis
M
0

It looks like the best option is to just get used to :cmap behavior. In the rare event I want to input the keyboard seqeunce wq I can just hit wq, wait a second, then hit q again. I did find this possible solution but it is too complex for my tastes.

Moersch answered 4/2, 2010 at 4:48 Comment(0)
B
0

I added a one-line patch and ran dpkg-rebuildpackage for renaming :wa to :wwa:

--- neovim/src/nvim/ex_cmds.lua
+++ neovim/src/nvim/ex_cmds.lua
@@ -3057,7 +3057,7 @@
     func='ex_wnext',
   },
   {
-    command='wall',
+    command='wwall',
     flags=bit.bor(BANG, TRLBAR, CMDWIN),
     addr_type=ADDR_LINES,
     func='do_wqall',

You will need to create this patch in debian/patches/ and add it to debian/patches/series.

Boss answered 18/12, 2019 at 12:8 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.