3

I'm trying to retrain my fingers to refrain from using Escape so often.

Can I disable Escape when I'm in insert mode, leaving just Control+[ and Control+C as the only ways to return to normal mode?

I've tried :inoremap <Esc> <nop> but that seems to half-break both Escape and Control+[.

  • 1
    Do you mean "leaving just Control+[" rather than "leaving just Escape+["? – Rich Feb 5 '15 at 12:51
7

I don't think that's possible, the problem is that <Esc> is the same as <C-[> as in the key codes that vim receives when you type it is exactly the same.

You can reproduce this and validate it in insert mode by hitting <C-v> first and then <Esc> and <C-[> respectively and you'll see it sends the same key code to vim.

Hence VIM cannot distinguish between the two (at least within a terminal), I am not too sure if things are different for gui versions.

6

If you use Linux (though not an exact answer), I recommend that you map Caps Lock to Ctrl and then use xcape to adjust Caps Lock behavior as follows:

  • when pressed by itself, Caps Lock behaves like Esc;
  • when combined with another key, Caps Lock behaves like Ctrl.

So you can press both Esc and a Ctrl combination more easily than any other solution.

To remap the keys, try this command in Linux:

setxkbmap -option '' -option 'ctrl:nocaps'

It resets already existing options first and then set Caps Lock as an additional Ctrl. I know, weird name. If you don't want to reset options first, omit -option ''. It may be useful if you have other options set in another place.

There are other options related to Esc:

$ grep escape /usr/share/X11/xkb/rules/base.lst
800:  caps:swapescape      Swap ESC and Caps Lock
801:  caps:escape          Make Caps Lock an additional ESC

xcape allows you to use a modifier key as another key when pressed and released on its own.

This is my .xinitrc:

... snip ...
setxkbmap -option '' -option 'ctrl:nocaps'
pkill xcape
xcape
exec xmonad
  • 1
    I would suggest "s/recommand/recommend/", "s/already exist options/already existing options/", "s/then set capslock as additional ctrl/then sets capslock as an additional ctrl/", "s/may useful in case of you have/may be useful in case you have/", "s/is other options relate/are other options related to/". Apologies for not doing it myself, but don't have the rep yet – jalanb Feb 5 '15 at 11:42
  • This is a nice workaround, but I've already remapped [CapsLock] to be my [Control]... – alxndr Feb 5 '15 at 18:20
  • 1
    muru, jalanb Thanks for correct my terrible writing. – ds6AUW Feb 6 '15 at 9:33
  • @alxndr This method could make [CapsLock] as both [Control] and [Escape] – ds6AUW Feb 6 '15 at 9:36
  • Interesting idea. On OS X it looks like this is achievable by combining the Karabiner (formerly known as KeyRemap4MacBook) and Seil utilities from Takayama Fumihiko on pqrs.org. – alxndr Feb 6 '15 at 18:43
5

In insert mode, you can type Ctrl-v Esc and Ctrl-v Ctrl[. You will see the literal interpretation of those key sequences according to vim. (See :h i_CTRL_V)

Most importantly, you'll notice that they are the same (at least for me in both vim in xterm and gvim). So you cannot disable one and not the other.

In general, I'd say it's a bad idea to remap Esc, as you'll have unintended consequences.

-1

I think you missed the backslash. Disable Esc like this:

:inoremap \<Esc> Nop

From comment of user under this answer:

The backslash is required because otherwise vim will recognize < Space> as a normal string and not the code for the space key. See :help expr-string for examples.

  • 2
    This doesn't really apply for map. The '\' is used to escape key codes when using normal via execute calls. Very confusing imho. Example from the execute help: :execute "normal ixxx\<Esc>". The use in mapleader is probably related to that. Or it just accepts expr-strings. – tokoyami Feb 5 '15 at 10:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.