sponsor Vim development Vim logo Vim Book Ad

basic Tip #285: Don't use the escape key!

 tip karma   Rating 328/132, Viewed by 14841 

created:   July 22, 2002 10:26      complexity:   basic
author:   David A. Rogers      as of Vim:   5.7

Vim (any vi really) is a dream for touch typists... Until you want to switch from insert mode to normal mode.  Then you've got to reach way up to whack the escape key.

Or at least that's what I was doing until I realized that (drum roll please)

                    Esc is exactly equivalent to control-[  (that's the control key plus the left square bracket key)


That little bit of knowledge, plus mapping my caps lock to another control key, was what turned my fascination with Vim into true love.  You never have to lose track of the home row again!

For Xfree86 users - you can make the capslock key another control key by adding

Option "XkbOptions" "ctrl:nocaps"

to the InputDevice section of your XF86Config file.

For Windows NT/2000 users  - use the following .reg file to do the same thing:

REGEDIT4

[HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Keyboard Layout]
"Scancode Map"=hex:00,00,00,00,00,00,00,00,02,00,00,00,1d,00,3a,00,00,00,00,00

 rate this tip  Life Changing Helpful Unfulfilling 

<<Mapping to print syntax highlighted buffer in B&W; | Recover after doing something... ugly. >>

Additional Notes

[email protected], July 24, 2002 12:25

I was also frustrated by location of ESC and CONTROL when using vim. My solution was to buy two Happy Hacking keyboards:

http://www.pfuca.com/products/hhkb/hhkblite2.html

Not only does it have ESC and CONTROL where I want them, but it makes it easier to switch between my Mac computer
at work and my FreeBSD box at home.
[email protected], July 25, 2002 12:01
I've used Ctrl-[ for a long time-- I guess at some point over the years it ceased being obvious.
As for the ctrl/caps lock under NT, I use:
00 00 00 00 00 00 00 00 03 00 00 00 1d 00 3a 00 3a 00 1d 00 00 00 00 00

which swaps the caps lock and ctrl keys.  Format is 2 dwords of 0, 1 dword length (#mappings + 1), dword mappings (input scan code 16-bit, output scan code 16-bit), final dword of 0.  Can remap any keys this way, even the Esc and ~ key to make your keyboard work like a happy hacking one.

Sanjay
Anonymous, July 25, 2002 14:01

What could I put in my XFree86 config file to turn Caps Lock into Ctrl-[ instead of just making it a control modifier key?

In other words, I want CapsLock keystroke to generate "Ctrl-[" key symbol.

Thanks.
jf_____ /at/ fastmail (.) fm, July 26, 2002 1:17
hey neat! I must say that I hadnt realized that about Ctrl-]. Nice to see that somebody was thinking along (well, sorta) the same lines as me. I was in fact just a few minutes ago working for an alternative solution to the problematic "<esc>" key, when something struck me.

I've always had situations where I had to "make a quick insert" - go into insert mode, insert a quick word, then come out into normal mode, and navigate elsewhere. To make a long story short, this is the solution that I came up with -

:map! <S-space> <esc>

or even
:map! <S-space><S-space> <esc>
if you find the first mapping getting in the may of your typing

With this quick combo ("Shift", + <space>), one can easily (and might I add, intuitively) go into and come out of insert mode. I guess I always thought the <space> would be a good way to do this sort of thing, since it is after all, so intuitive in the typing process. So why not make it such that it can "escape" you out into normal mode as well? Just type 'i', to go into insert mode, type in your stuff, and once you're done, hit Shift-space!

(note: I havent automated 'i' into the whole process yet, but does anybody think that it would be possible? I would certainly like to hear about it if you do work out something)
dubu AT gmx.net, July 26, 2002 17:35
I changed my CapsLock key to Escape under XFree86 with the following lines in my ~/.Xmodmap:

  ! Esc on caps lock :-))
  remove Lock = Caps_Lock
  keysym Caps_Lock = Escape

In my ~/.xinitrc, xmodmap is called to set the mapping:

  if [ -f ~/.Xmodmap ]; then
      xmodmap ~/.Xmodmap
  fi

Finally, my typing speed in vim is the same as with the original HP 9000 ITF HIL keyboards, which MUST have been designed by vi hackers  (Esc key on the outer left, next to the shift key). :-)
Anonymous, August 11, 2002 6:29
This is not a good tip if you have e.g. norwegian keyboard layout.
ctrl [ for me is pressing CTRL+Alt Gr + 9
grinderf at adelphia.net, August 20, 2002 20:35
i don't know if it's the "proper" way to do it, but i've always used ctrl+c myself
[email protected], October 8, 2002 23:35
I have a better tip.

You can use some useful 'imap' keymaps for inserting mode...

for example :
<C-j> <ESC>lji
<C-k> <ESC>lki
<C-u> <ESC>i
<C-l> <ESC> l

<C-h> is crash with scancode of Insert key... so I use <C-u> keymap

Frankly speaking, I bring this ideas from emacs keymaps.

Anyway, if you do so you can move your cursors freely everytimes. :)
Anonymous, October 13, 2002 21:57
Lots of great adivse here!
I love using shift-spacebar for esc
Thanks
Mark, November 11, 2002 15:50
Thanks for the tip on Shift-Spacebar for ESC
[email protected], November 21, 2002 1:03
The shift-space mapping is good thinking indeed.
Esc is way too cumbersome
Anonymous, December 26, 2002 1:46
Since on the french canadian keyboard the key combination CRTR-[ is perform with CTRL+AltCar+"the cedilla key" it's not very convinient.

But the TIP of using CTRL-C key combination work well on any keyboard mapping. It doesn't required you to modify any keymap, so it will work on any vanilla installation. From my point of view (switching from PC with English_US and Frech_CA keyboard layout) using CTRL-C instead of the hard to reach ESC key is the real TIP here.
Anonymous, January 14, 2003 22:22
I cannot get 'map! <S-space> <esc>' to work for me. I am running Vim throught a terminal client(putty), would this effect this mapping to not work.   I would like to use Shift-Space as ESC, any suggestions?
[email protected], January 22, 2003 3:24
If you just want the caps lock key to be a second escape key then see my comment on https://www.vim8.org/tip_view.php?tip_id=166
Goggs, February 21, 2003 20:58
Is there any way to remap Shift+CapsLock to ESC (or S+CL to plain CL)? I use the caps key every now and then (not much I admit)
[email protected], April 30, 2003 9:59
I have tried mapping "qq" and "jf" to <Esc> but I'm going to try to shift-space.
Incidentally, I mapped the space bar to i.  So I will press <Space> to get into insert mode and <S-space> to get out.
Piet Delport <[email protected]>, May 4, 2003 12:15
The above-mentioned "Scancode Map" registry key for Win2K/XP is documented here:

http://www.microsoft.com/hwdev/tech/input/w2kscan-map.asp

You can find a full listing of keyboard scancodes here:

http://panda.cs.ndsu.nodak.edu/~achapwes/PICmicro/keyboard/scancodes1.html

Quick list of pertinent scancodes:

0x01 Esc
0x1D Ctrl
0x38 Alt
0x3A Caps Lock
Anonymous, May 10, 2003 8:13
I'm a touch typist myself and one of the reasons I use Vim is BECAUSE of the Esc. key!  I can't stand all that <Meta>, <Alt>, <Ctrl>, <whatever> nonsense in, for example, Emacs -- the Esc. key is incredibly easy to locate (just keep going until your finger can't find any more keys...) and much faster to use.  As for losing the home row, well you know those little bumps on the 'F' and 'J' keys?.....
Anonymous, May 12, 2003 15:48
:map <S-space> i
:imap <S-space> <esc>

From command mode <S-space> takes you to insert mode, from insert mode <S-space> takes you to command mode.
Anonymous, May 19, 2003 7:57
I am trying to get the <S-space> thing happening in Mac OS X 10.2+ with no luck.

I was able to assign a normal char (e.g. 'a') to the <esc> key as an experiment but not the <S-space>

Does anyone have any ideas?

David

[email protected], June 5, 2003 7:57
You can also remap the Caps Lock key to Control in Win9x using Microsoft's Windows 95 Kernel Toys (find them at http://www.microsoft.com/windows95/downloads/contents/WUToys/W95KernelToy/Default.asp); one of them ("keyboard remap") will let you do this. Despite their saying they're Win95 only, I've used this utility under Win98 and Win98SE without problems.
Anonymous, June 18, 2003 22:00
"I cannot get 'map! <S-space> <esc>' to work for me. I am running Vim throught a terminal client(putty)"

Since you are in INSERT mode when you want to press escape...it should be:
imap <S-space> <esc>
[email protected], June 24, 2003 9:23
Can anybody help me remap the Ctrl-Space key?  I have an autocomplete function (for tab) and I want to make it use Ctrl-Space instead.  I cannot get any mapping for the space key to work (including all of the examples above).  What would keep vim from mapping the space key?
Zitty, July 1, 2003 18:37
On Mac OS X:

I, too, am having problems getting the imap <S-space> <esc> scheme to work. I've noticed some really funky stuff. First, it doesn't work at all using App Terminal version of vim, nor does it work using the GUI version for Jaguar either. Where the story gets strange is that it DOES work using the X-Windows version of gvim (via Apple X11) but not the xterm version.

To sum up, it ONLY works on gvim via Apple's X11.

Hopefully someone can help us figure out what's going on here.
[email protected], July 11, 2003 10:05
Well, "map! <S-space> <esc>" doesn't work for me either on xterm, eterm or whatever. For
those with a 'normal' pc keyboard, you probably have the 'windows' key on the bottom row. Just
use it. Type the following on the commandline or put it in your .bash_profile or .xinitrc :
"xmodmap -e "keysym Super_L = Escape"
Zitty, July 13, 2003 0:41
Okay a solution. Get the shareware utility "Keyboard Maestro". It allows you to remap keystrokes for all sorts of tasks. You can add a general remap for the Esc key to what ever you like, S-Space, or Ctrl-space, etc. Not exactly elegant via vim, but it works.
[email protected], August 7, 2003 21:59
Try:

map! <S-Enter> <Esc>

instead of <S-Space>

works a lot better and won't get pressed accidentally.
Anonymous, August 14, 2003 16:13
If you're having trouble getting <C-space> to map in vim, it may be that <C-space> is being mapped to another keystoke.  My keyboard was using <Nul> for <C-space>.  So, you could put in your vimrc:

" Map <C-space> to <Esc>.  CTRL-@ will also be mapped to <Esc>
map <Nul> <Esc>

To get the <> value for a particular keystroke, try pressing CTRL-K before the keystroke, while in insert mode.
[email protected], September 19, 2003 11:04
imap <Nul> <C-p>
This is really works.
But what is <Nul>? Is it keyboard or X11 defines it?
[email protected], October 3, 2003 10:04
For Win2k and WinXP users, KeyTweak is probably your best bet, since it automates the registry access.  I just downloaded it to swap my escape and capslock keys and it works great.

http://webpages.charter.net/krumsick/
mtwebb at cs.princeton.edu, October 15, 2003 21:44
more on the os X front:

no dice with shift space, but the enter key adjacent to the right apple key also seems to serve as an esc key.

Any ideas why this is?

using 10.2.8 and gvim
Anonymous, December 19, 2003 3:07
And for those of us who prefer the console...

Alt-[key] is usually actually sent as <Esc>[key], and generally if you're coming out of insert mode it's because you want to execute a normal mode command, so...

Instead of <Esc>: to start an Ex command from insert mode, just press Alt-:. Sim. for everything else.
Anonymous, February 7, 2004 10:39
It's a good idea to use <ctrl-c> to get back to normal mode. these shift-space-things aren't easier to use and are user defined.. so if you use another vim installation, you have to define these two mappings first. so why not using <ctrl-c> instead? however, the only difference between <ctrl-c> and ESC is that incomplete abbreviations won't be expanded.
Anonymous, February 22, 2004 0:05
alternate key for SHIFT KEY in keyboard
Anonymous, May 21, 2004 19:58
Get a Linux 101 keyboard from http://www.pckeyboards.com. Not only are the CTRL and ALT keys more convenient but the ESC key is where your little finger can hit it.
Anonymous, May 27, 2004 17:49
I use  ":imap ;; <esc>"

The ";" key is right at the fingertips, and I almost never actually need to type ";;".  The <s-space> is good too but it doesn't work when I use vi in a terminal (with putty).
[email protected], June 6, 2004 2:00
How about remapping one of those irrelevant and annoying Windows-specific keys that are on every standard 104 key keyboard: the windows key and the context-menu key? They aren't used for anything on any of the Linux systems I've ever used.

This command turns the left Windows key into another Esc key. Just add it to your ~/.bashrc to make the change permanent.

xmodmap -e 'keysym Super_L = Escape'
Anonymous, July 10, 2004 19:40
> I use  ":imap ;; <esc>"
>
> The ";" key is right at the fingertips, and I almost never actually need to type ";;".  The <s-space> is good too but it
> doesn't work when I use vi in a terminal (with putty).

That's a great idea. I'm going to use that in the future.

I wish vim would make more effort to disseminate this particular tip... or at least the CTRL-C tip. The need to reach all the way up to the ESC key is a real achilles heel for vi. It tends to interfere with touch typists-- especially on modern PC keyboards, where the ESC key is usually all the way up by the function keys.
[email protected], July 13, 2004 15:47
for(;;)
{
    do_stuff();
}

I don't think that mapping ;; to Control-[ is viable to C programmers.

The following .Xmodmap swaps the CapsLock and Esc Keys:

remove Lock = Caps_Lock
add Lock = Escape
keysym Caps_Lock = Escape
keysym Escape = Caps_Lock
Anonymous, August 10, 2004 8:09
> I don't think that mapping ;; to Control-[ is viable to C programmers.

If you wait for a few seconds after pressing the first semicolon, vim will let you type ";;" even if you have it set as an abbreviation for ESC.

The idea of making CAPS LOCK into another ESC is good, but not if you have to use VIM on an unfamiliar computer. CTRL-C I don't use, because it tends to have weird effects that are NOT the same as those of ESC.
[email protected], August 11, 2004 9:58
I am having trouble mapping the <S-space> to anything... I actually was looking for a way to map <S-space> to the underscore key but can't get it to map to anything!  Is there a way to have it record keys or display what keys vi thinks you are pressing?... maybe my keyboard is recognized differently???
[email protected], September 11, 2004 0:39
> I use  ":imap ;; <esc>"

Too slow.
If you have questions or remarks about this site, visit the vimonline development pages. Please use this site responsibly.
Questions about Vim should go to [email protected] after searching the archive. Help Bram help Uganda.
Sponsored by Web Concept Group Inc. SourceForge Logo