views:

2385

answers:

13

Escape is almost as important as the enter key, used universally for "cancel". In vim, it's arguably more important than the space bar, making its location highly suboptimal.

What key remappings -- either globally (for the whole operating system) or just within vim -- or other solutions do folks have for this problem?

I'll include things I've tried as separate answers.

ADDED: I guess it sounds crazy, maybe especially to people with bigger hands than me or something, but, yeah, it's much easier to hit TAB or CAPSLOCK in succession with other keys than ESC. For non-vim users who don't believe this could be an issue, here's an experiment: Type out a sentence and see how long it takes. Now type it again but with ESC in place of every SPACE. That's how much worse vim is without some kind of remapping.

Speaking of which, if I had a keyboard with a split space bar, remapping the left half of the space bar to ESC might be ideal (I guess I'd have to try it to know though).

Note that one reason many of us like vim is that you can edit and move around without ever taking your hands away from the home row (like for the arrow keys). The placement of ESC somewhat undermines that advantage.

+3  A: 

One solution is to swap ESC and CAPSLOCK. Unfortunately, Macs don't have a right CTRL key so in that case you really want to remap CAPSLOCK to CTRL. But if you have a keyboard with two CTRL keys, I recommend the following:

How to remap keys to swap ESC and CAPSLOCK:

For X Windows (X11):

Figure out which keycodes are actually mapped:
  xmodmap -pke | egrep -i "escape|caps_lock"
This will print out something like:
  keycode 66 = Caps_Lock
  keycode  9 = Escape
Based on that, swap them:
  xmodmap -e "clear lock"
  xmodmap -e "keycode 66 = Escape"
  xmodmap -e "keycode 9 = Caps_Lock"
  xmodmap -e "add Lock = Caps_Lock"
Add those four xmodmap commands to your ~/.xinitrc (or the global
  xinitrc) to set up your keys whenever you log in to X.

To do the same for the console, see: http://www.faqs.org/docs/Linux-HOWTO/Keyboard-and-Console-HOWTO.html

To do the same under windows:

Get this file: http://yootles.com/outbox/remap.txt. Go to start menu, run, and type:

regedit path\to\remap.txt
dreeves
+7  A: 

I got used to use Ctrl+[ instead of Esc, I find good enough for me. However it's also good to have an actual use of the Capslock key...

CMS
And what uses of the capslock key are those? =] I've been thinking about switching to ^[ myself. However, I fear I'll be pressing ^P (autocomplete) instead, annoying my editor. Have mistypes been an issue with ^[?
strager
Well, I was talking about mapping Esc to Capslock, but I'm not sure if I'll get used to it, since I use Vim (and other vi-like-input tools) in different machines... that's why I like the good old ^[
CMS
The home keys are F and J - there's little difference between ESC and CTRL from the F key and you introduce two keystrokes for one. This is a bizarre answer to a bizarre question.
paxdiablo
Presumably he is more use to returning to home after a CTRL chord. Too much Emacs in his youth perhaps. :-)
jmucchiello
@paxdiablo I don't know what kind of keyboard you have, but on mine it takes considerably more effort to reach <kbd>ESC</kbd> than it does to reach <kbd>CTRL</kbd>. When you're touch typing anyway.
Arda Xi
A: 

Putting the following in your .vimrc makes TAB work like ESC:

imap <tab> <esc>

This is fine as long as you never need to insert literal tabs in insert mode. Better would be to actually swap TAB and ESC in vim, but I don't know how to do that.

dreeves
You can insert TAB literals by typing ^I (control-I) or ^V^I or ^V<tab>.
strager
Mate, how big's your keyboard? It's 2cm from tab to esc on mine and, if you're typing properly, it should be minimal time difference between them. In addition, are you entering and exiting insert mode so often that it matters? You should probably try to learn some vi commands other than x and i :-).
paxdiablo
@Pax, I highly agree with you, but in general the TAB key is larger. Then again, the ESC key is usually isolated, so it's not easy to miss it hand hit another key on accident. CAPSLOCK is a better idea, though. Modifying your keyboard is the best option, maybe? =]
strager
+1  A: 

Remapping escape to SHIFT+SPACE

map! <S-space> <esc>

But why? Remapping makes you get used to non standard behavior which can be surprising when you are on production systems (or worse - on production systems at client site!). Not a good idea IMHO.

Learning
True, but that's a trade-off that makes sense for some people. And you can usually copy over your .vimrc to any new environment you use.
dreeves
Shift-space does not work in vim, only in gvim.
Zathrus
+9  A: 

For VIM (at least; didn't try the original vi), you don't need ESC; you can use CTRL-C to exit insert modes.

Mikeage
Hmm, I never knew that. Thanks! =]
strager
And ctrl-c is better than esc how exactly?
paxdiablo
@Pax, It feels more "standard." Other programs quit and to back to the shell when you hit ^C. Vim quits back to its own command prompt-like mode. Also, I find it easier typing ^C than ESC (not the characters here ;P).
strager
The question was about having to reach up to hit ESC. If you remap Ctrl to CapsLock, then it's _really_ easy. Even if not, Ctrl-C is less of a disruption than ESC.
Mikeage
Note that leaving insert mode via Ctrl-C is NOT the same as hitting ESC. In particular, some autocommands will not fire and iabbrev's will not complete.
Zathrus
Good catch. +1. See comments of http://stackoverflow.com/questions/487737. 11 down, 4 to go (1 per day)
VonC
Zathrus -- can you clarify? I've never noticed this...
Mikeage
+3  A: 

Yeah, <C-[> is actually a very good idea. Especially when you have your Ctrl mapped to CapsLock, so your fingers are on the home row all the time.

Georgi Kirilov
Unless you are stuck with an international keyboard .. <ctrl-[> is a <ctrl-fn-!> for me.
lexu
I have Ctrl mapped to CapsLock too and I find <C-[> still rather awkward. I'm going to trying <C-c> as suggested in another answer.
msutherl
+11  A: 

I remaped "jj" to escape. It's very nice once you get used to it (if you're using QWERTY):

ino jj <esc>
cno jj <c-c>

For visual mode, I just use "v" to toggle it on & off:

vno v <esc>
Michael
Sounds very interesting. I'll try that out when I'm in a programming mood.
strager
Another nice thing about this is that it is very simple to implement if you move from machine to machine.
The Talking Walnut
What if you want to type a word containing 'jj'?
weakish
I've use ';;' before. That worked pretty well for me. Though I believe it may be a comment character for some language ...
Hamish Downer
+2  A: 

In windows, you can use this nifty app: Key tweak to remap keys for all apps. I created a custom dvorak / vim set, and everything just flows from my fingers. The downside is that nobody else can now use my keyboard :)

Marcin
job security, not a downside :)
Adriano Varoli Piazza
+1  A: 

I have small hands and this bothers me a lot as well. I switched to a Happy Hacking Keyboard which has the ESC key right above the tab key. I love the small size, but my co-workers hate typing on it.

gdziengel
+1  A: 

I've swapped Caps Lock and Esc at the OS level.

+6  A: 

With the standard Ubuntu desktop, go through the menus: System -> Preferences -> Keyboard -> Layouts tab. Then hit the "Layout Options" button, click on the triangle next to "Caps Lock key behaviour" and select "Swap ESC and CapsLock".

Makes vim so much nicer :)

Hamish Downer
This should work for every Gnome desktop and KDE offers the same feature somewhere, it should be easy enough to find. This is the first thing I change whenever I install a new system.
DasIch
+1  A: 

The solution I've been using for years is to map Caps Lock to Escape, i.e. to make Caps Lock behave as an Escape key. I've been following these directions. Basically, follow these steps:

  1. Create a caps.reg file.
  2. Stick the following into it.

    REGEDIT4

    [HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Keyboard Layout]

    "Scancode Map"=hex:00,00,00,00,00,00,00,00,03,00,00,00,3a,00,46,00,01,00,3a,00,00,00,00,00

  3. Merge that file into the registry.

  4. Reboot. (At least I've always had to reboot on Windows XP for the change to take effect.)
Leif Wickland
A: 

why not try imap jj . That way you don't have to move your fingers from the keys ;-)