Avoid the escape key
From Vim Tips Wiki
[edit] Duplicate tip
This tip is very similar to the following:
These tips need to be merged – see the merge guidelines.
Tip 285 Previous Tip • Next Tip
Created: July 22, 2002 Complexity: basic Author: David A. Rogers Minimum version: 5.7 Karma: 599/236 Imported from: Tip#285
Vim 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 Esc is equivalent to Ctrl-[ (control key plus 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 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
For Unix, see VimTip166.
[edit] Related tips
- Tip 75 Remap CAPSLOCK key in Windows 2000 Professional and NT4.0
- Tip 136 Remapping Alt, Ctrl and Caps in Win2k
- Tip 166 Mapping caps lock to esc in XWindows
- Tip 285 Avoid the escape key (this tip)
- Tip 314 Insert and back
- Tip 327 Key mappings
- Tip 342 Remap the Escape key
- Tip 353 Swap caps-lock and control keys using MS Windows Registry
- Tip 377 Microsoft Natural Multimedia Keyboard Scancodes
- Tip 459 Use Ctrl-O instead of Esc in insert mode mappings
- Tip 770 Toggle normal-insert modes without Escape
- Tip 775 Remapping Caps to Shift
- Tip 783 Use the return key to get out of insert mode
- Tip 1083 Map CapsLock key to Esc
- Tip 1117 Use tab instead of escape
- Tip 1183 Insert-mode only Caps Lock
- Tip 1275 Toggle between insert and normal mode with Esc
- Tip 1324 Alternative to hitting Esc
TO DO
Some of the above tips should be merged. Need one tip for using the operating system to remap the keyboard (perhaps one for each OS), and another for using Vim to map some key to Escape.
[edit] Comments
Simple map to use backtick as Esc, when in insert mode:
:imap ` <Esc>
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.
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.
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)
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). :-)
This is not a good tip if you have e.g. norwegian keyboard layout. ctrl [ for me is pressing CTRL+Alt Gr + 9
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
Since on the french canadian keyboard the key combination Ctrl-[ is performed with Ctrl+AltCar+"the cedilla key" it's not very convenient.
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.
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.
The above-mentioned "Scancode Map" registry key for Windows 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
: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.
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.
> I cannot get 'map! <S-space> <esc>' to work for me.
Since you are in INSERT mode when you want to press escape, it should be:
imap <S-space> <esc>
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.
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
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.
Try:
map! <S-Enter> <Esc>
instead of <S-Space>
works a lot better and won't get pressed accidentally.
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.
imap <Nul> <C-p>
This is really works. But what is <Nul>? Is it keyboard or X11 defines it?
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/
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.
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.
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.
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).
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'
The following .Xmodmap swaps the CapsLock and Esc Keys:
remove Lock = Caps_Lock add Lock = Escape keysym Caps_Lock = Escape keysym Escape = Caps_Lock
Yet another way to remap the Caps Lock key to be Escape (i.e. helpful if you don't have root priv).
% xmodmap -pk | grep -i caps 66 0xffe5 (Caps_Lock) % xmodmap -e 'keycode 66 = Escape'
For Windows users:
Remap whatever you want any way you want: Use Autohotkey (www.autohotkey.com, an open source project)! You can map single keys to key combinations, you can map key combinations to single keys, you can even map double key strokes to single keys (e.g. double CAPS to ESC). But it's still much more. It's an extended, extremely powerful scripting language. There are scripts that allow the "command mode" style navigation everywhere in windows! Plus you can always write scripts yourself.
Why not use:
map! ii <esc>
This allows the use of the same key to both enter and exit insert mode. It's also extra handy for dvorak users, since we can mash the i with the left index finger. And outside of roman numerals, how often does anyone need to type two consecutive i's?
I think the Ctrl+[ idea is great, never thought of that.
Just like delta suggested I use AutoHotkey. The following script will map CapsLock to Ctrl in PuTTY. Every other app will use CapsLock like normal.
classname = ""
keystate = ""
*CapsLock::
WinGetClass, classname, A
if (classname = "PuTTY")
send,{Ctrl down}
else
GetKeyState, keystate, CapsLock, T
if (keystate = "D")
SetCapsLockState, Off
else
SetCapsLockState, On
return
*CapsLock up::
WinGetClass, classname, A
if (classname = "PuTTY")
send,{Ctrl up}
return
You need version 1.0.28+ of AutoHotkey to support the "CapsLock up" syntax. I prefer this over losing the CapsLock key alltogether. Also this method is easily adaptable and I was already using AutoHotkey anyway. I hope this is not considered off-topic; I figured this out so I could use it in vi.
If you have :set insertmode, then CTRL-L moves you from INSERT mode to NORMAL mode
mapping ii to <ESC> is cool! :)
I have quite long fingers, therefore the upper keys (like numbers, even function keys) are somewhat easier to reach as the lower keys (like ctrl). so mapping <F5> to <ESC> is very helpfull to me (maybe <F9> too, for the right hand).
in .vimrc:
imap <F5> <ESC>
The answer is
--contents of file .Xmodmap remove Lock = Caps_Lock remove Mod1 = Alt_R remove Mod5 = Scroll_Lock keycode 43 = h H Left keycode 44 = j J Down keycode 45 = k K Up keycode 46 = l L Right keycode 56 = b B BackSpace keycode 64 = Alt_L keycode 66 = Mode_switch keycode 113 = Caps_Lock add Lock = Caps_Lock add Mod1 = 0x007D 0x009C Alt_L Alt_L add Mod4 = 0x007F 0x0080 add Mod5 = Mode_switch ISO_Level3_Shift
xmodmap .Xmodmap
Now in insert mode I use CAPS-{hjkl} to move around and CAPS-b to do a delete (BTW it works for all the X apps ALSO while writting this message) If someone would make a key below the spacebar to map to ESC I wouldn't have to move my fingers anymore.
to do the same thing in console mode use this map
--contents of file map keycode 58 = Alt keycode 125 = Caps_Lock alt keycode 35 = Left alt keycode 36 = Down alt keycode 37 = Up alt keycode 38 = Right alt keycode 48 = Delete
loadkey map
The problem as usual is in RTFM from xmodmap documention
The list of keysyms is assigned to the indicated keycode (which may be specified in decimal, hex or octal and can be determined by running the xev program). Up to eight keysyms may be attached to a key, however the last four are not used in any major X server implementation. The first keysym is used when no modifier key is pressed in conjunction with this key, the second with Shift, HERE--> the third when the Mode_switch key is used with this key and the fourth when both the Mode_switch and Shift keys are used <---
I didn't read all the comments, but in my enviro I have caps lock remapped to escape. This is a bit faster than caps-lock being a Ctrl key. :)
My home directory contains a .xmodmaprc file which contains the two lines:
clear lock keycode 66 = Escape
And after that you can run xmodmap ~/.xmodmaprc I'm unsure how to do this in text mode however.
I am forced to use Windows at work, I tried www.autohotkey.com (the open source project for remapping keys) and it rocks!
To make Capslock work just like Escape, just add this to their default script:
Capslock::Esc
I think it should be pointed out that xmodmap only works for people running X. It's not a solution for users of any other windowing system. And besides, I just want an easier keystroke than <Esc> for going into normal mode. I don't want to remap the CapsLock key for the entire operating system.
As for the shift+space problem, PuTTY seems to refuse to send the shift key along with space (or with enter, for that matter). I don't know why, but I'm hoping that "imap ii <Esc>" will suffice. As someone noted, shift+space does work in Gvim (even on Windows XP :) ) Unfortunately, that doesn't allow me to edit files on the server where I work, which is where I do most of my vimming.
> This is not a good tip if you have e.g. norwegian
> keyboard layout. ctrl [ for me is pressing CTRL+Alt Gr + 9
> [...]
> Since on the french canadian keyboard the key combination
> Ctrl-[ is performed with CTRL+AltCar+"the cedilla key" it's
> not very convenient.
I don't know about norwegian or french keyboard layout, but I just found out this: On german keyboard layout the [ is also AltGr + 9, but ANYWAY: using CTRL+ the key where [ is on english keyboard layout has the same effect! Although there is another character mapped to the key right to 'p' than [ (which is [ in english keyboard layout) this shortcut has the effect of ESC!!
It works here in windows on german keyboard layout. Does it work for norwegian keyboard layout as well? Does it work in Linux?
I have been using
imap <S-space> <esc>
for a while. But this doesn't work in Mac OSX. Instead I found
imap <D-space> <esc>
works even better in Mac! <D-space> means the "apple" key and the space key. Have fun!
I tried to find a similar way in Cygwin, but no luck.
