Thursday, April 10, 2008

Ways to Avoid the Esc Key

I've been running this into the ground, but reaching for the Esc key is inefficient on modern keyboard layouts. I've spent some time recently investigating ways to avoid reaching way the hell over to the top-left hand corner of the keyboard in an effort to maintain focus on the home row. I struck gold with a few of the ideas presented on this Vim tip. Here's my take on what works best.

1) Use Ctrl-[

This is what I've been doing until recently; although it's a bit awkward as far as key chords are concerned. It still beats the hell out of going all the way to Esc, and it's built right into Vi(m), so there aren't any fancy tricks required to make it work.

2) Use Ctrl-c

Another builtin key chord that will take you from insert to normal mode. Depending on your keyboard and personal preference, it may feel better than Ctrl-[.

3) Map a combo inside Vim

I've seen people suggest two combos that were particularly attractive from an efficiency standpoint.

map! ii <Esc> " map ii to Esc
map! ;; <Esc> " map ;; to Esc

With these, you're typing in insert mode, hit `;;' and you're back in normal mode. From a touch-typist point of view `ii' is particularly attractive but has the obvious downside of not allowing you to type a literal "ii" in your document without typing it twice.

4) Remap Caps lock

Caps lock is placed in a very convenient location and is very rarely used by most programmers. Reclaim this valuable real estate by mapping it to Esc as it was on the original HP 9000 ITF HIL keyboards. This is the approach I'm using right now, and I LOVE IT. Remapping the key on X Windows is done as follows.

a) Create a file in your home directory called .Xmodmap

# begin -------------
remove Lock = Caps_Lock
keysym Caps_Lock = Escape
# end ----------------

b) Create a reference to .Xmodmap in your .xinitrc

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

If you don't want to restart X, you can manually run
xmodmap ~/.Xmodmap after you create the first file.

5) Avoid shift-space Mapping

Quite a few people recommend mapping the shift-space key chord to Esc; however, many terminals can't differentiate between shift-space and regular ol' space. Gnome terminal is one of them, so if you run Linux, there's a good chance this mapping won't work for you.

20 comments:

Pento said...

Nice post!
If you not against, I will translate it on russian for my "All about Vim" blog.
Of course with link to your blog and you as author.

Travis Whitton said...

Sure, that would be awesome. If you don't mind, send me a link to the translated post, and I'll link to it.

Pento said...

The link :)
http://allaboutvim.blogspot.com/2008/04/esc-vim.html

jim said...

I mapped my ESC key to ;; but I can't seem to use ;; in macros to get to normal mode. When the macro runs, it just chokes at the part where i pressed ;; in insert mode to get back to normal mode. Can you use mapped keys in macros?

Thanks

Travis Whitton said...

Hey Jim,
I tried to reproduce your problem but I can't. What version of Vim are you using and what platform are you running on. Are you using gvim or regular Vim? Also, do you have anything in your vimrc that would conflict?

jim said...

I ran into this problem with gvim 7.1 on Win xp. I never found out what was causing the problem, but changing map! to imap fixed it. Very odd.

thanks though..

mao said...

AWESOME BLOG. This is exactly what I've been looking for. The damn ESC keys is so awkwardly placed I can't believe VI uses it to switch between command and typing mode. ON my laptop, I literally have to twist my wrist an awkward 45 degrees to reach that key; what can I saw, small ESC key and short fingers don't match well.

Anonymous said...

Check the keyboard on wikipedia article on vi
http://en.wikipedia.org/wiki/Vi

The ADM3A keyboard vi was developed on had Ctrl key in CapsLock place of current keyboards.

itai said...

I mapped my Caps_Lock key to Escape, and the Escape key to KP_Home (keypad home, which I never use, and anyway don't have on my laptop - any other unused key would have done), and then mapped <kHome> in vim Insert mode to <C-o> (Control-o), which allows you to do a single Normal mode command without leaving Insert mode.

How to do this:

.Xmodmap:

   keycode 9 = KP_Home

.vimrc:

   inoremap <kHome> <C-o>

Joel said...

I like the Ctrl-c option because I map Caps-Lock to Ctrl. That way Ctrl-c is no stretch at all, and it means I can still use it for other things like Ctrl-w when using multile windows and the like.

By mapping Esc to Caps-Lock I feel it kind of limits your flexibility.

Anonymous said...

The vim tip you link to has moved here:

http://vim.wikia.com/wiki/VimTip285

Anonymous said...

And here is one you can click on. Tip 285

mikeb said...

Mapping ESC to jk is a pretty good combination, too. I've had it like this for some time and never came to a real world situation where I needed to type those two letters together, plus they're conveniently part of the hjkl nav keys.

I also mapped caps lock to ctrl, and use ctrl-[ often, as well as the old school escape key on certain occasions. Variety is the spice of life! (though the jk mapping is my favorite)

-mike

Dennyboy said...

Great tip! Just wanted to let you know
the link at the beginning of your tip
now goes to:
http://www.vim.org/tips/index.php
where the reader will have to search.
Actual tip is at:
http://vim.wikia.com/wiki/Avoid_the_escape_key

brandon said...

The ii is comfortable and convenient, but there are many (admittedly mostly rare) words in English that contain 'ii'. (And perhaps in other languages they are more common.) Hawaii & Hawaiian are the most obvious and common, but Naziism, Wahabiism, Sufiism, and skiing, are other words anyone should know. For all 432, grep the dictionaries that come with OS X or Linux.

(A convenient way to do this is to create a function and put it in .bash_profile or .zshrc or wherever:
function word () {
grep $1 /usr/share/dict/web2
grep $1 /usr/share/dict/web2a
}
They you can just type 'word ii' at the command line to see all the words.)

The double semi-colon, however, has no chance of conflicting with any word, and according to all conventions of punctuation that I'm familiar with should never pose a problem.

Naveen shukla said...

hello everyone,
I want to know that if my file written in c is not indented.Suppose a program is written like a paragraph without new line character(enter) then how one can indent the whole file in one go by just writting one command.
I found this is the only place where i can get the answer very quick .
Waiting for your reply!

Naveen shukla said...

hello everyone,
I want to know that if my file written in c is not indented.Suppose a program is written like a paragraph without new line character(enter) then how one can indent the whole file in one go by just writting one command.
I found this is the only place where i can get the answer very quick .
Waiting for your reply!

Naveen shukla said...

hello everyone,
I want to know that if my file written in c is not indented.Suppose a program is written like a paragraph without new line character(enter) then how one can indent the whole file in one go by just writting one command.
I found this is the only place where i can get the answer very quick .
Waiting for your reply!

Naveen shukla said...
This comment has been removed by the author.
Anonymous said...

Like a previous anonymous comment, I too have Caps Lock mapped to Control. I like that arrangement a lot more because I use Control quite frequently in Vim but also other applications, particularly Bash.