This tip shows some methods for mapping keys within the operating system to make life easier in Vim. In particular, it can be convenient to use the CapsLock key for Escape (so you don't have to move your hand to reach the Esc key).

If you want to use the capslock as escape on the console as opposed to just in X, try:

loadkeys ~/keymap

where ~/keymap contains:

keycode 58 = Escape

Or, if you want to completely swap them:

keycode 1 = Caps_Lock
keycode 58 = Escape

See 'man 5 keymaps', and verify with 'dumpkeys' that keycode 1 is Escape and keycode 58 is Caps_Lock beforehand.

Be careful, because if you swap the keys, they will be swapped every time the script is called. It is safer to set them equal to the original keycodes, change the Caps Lock mapping, then switch the keycodes (in the following example, the keycode values are 8 higher):

This can be put directly into the Xmodmap (for example in /etc/X11/Xmodmap), then called from the bashrc. Just to be sure, execute the bashrc twice and make sure the Escape key is working as Caps Lock, not the other way around.

I used the following code, and it works for me (none the others did)

xmodmap -e 'clear Lock' -e 'keycode 0x42 = Escape'

Must have for nerdy touch typists with german keyboards and us keyboard keymappings:
the key between z/y and left shift (doesn't exist on us keyboards) is superfluous when using an american keymap.
=> the best place to put escape and you can leave control on capslock!

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

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

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.

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.

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.

Probably the best thing is to put one of these commands in .xsession or equivalent file. Red Hat Linux and Fedora will execute by default .Xkbmap and .Xmodmap. In such a case it is enough to put xmodmap commands in .Xmodmap file, and only -option ctrl:swapcaps in .Xkbmap. Of course you need only one of these two files.

Tired of hunting down <Esc> at upper-left of your keyboard, while using a keyboard with useless Windows keys?

Well, remap them -- use xmodmap. I'm using Debian and my xmodmap config file is in /etc/X11/xinit/xmodmap. You may have to use xmodmap directly (from ~/.xinitrc in *BSD or whatever).

Here's part of my xmodmap:

keycode 115 = braceleft
keycode 116 = Escape
keycode 117 = braceright

The keys are:

115 - Windows key, between the left-hand Ctrl and Alt keys

116 - Windows key, to the right of the AltGr key

117 - Menu key, to the left of the right-hand Ctrl key

Valid for all environments with X, on *BSD as well as on Linux.

You get the same mappings under ordinary console by modifying the console keymap file (pretty self-explanatory), in my case it's /usr/share/keymaps/i386/qwerty/et.kmap.gz.

I'm using mapping braces to winkeys because of my Estonian keyboard.

I think users with german, slavic (and probably some other) keyboards will have better things to do with the windows key - because we need additional keys for umlauts etc, or other language-specific letters, some of the important signs, namely {[]}\ are the "third-function" of number keys 7 and above. You have to type those with the right hand, while simultaneously pressing "Alt Ctr" located right of the spacebar which at least I never learned to do properly, you can't do that without changing the position of the hand, and I keep hitting the wrong key. I've been raging about this stupid mapping for a long time without it ever occurring to me that there's a free key you could map to Alt Ctr and use it with your left hand.

Why don't you map

left winkey -> {

shift+left winkey -> [

right winkey -> <Esc>

right winmenu -> }

shift+right winmenu ]

> Is there a way to remap capslock for the console, too?

Yes, if you are using KDE3. Just go into the control panel under keyboard and choose to make the CapsLock another control key. Then you can use capslock-[ the way it was originally intended to be used in Vim.

I don't like mapping Caps_Lock to Escape as is done in this tip. I don't want an easier way to hit Escape, I want to prevent unexpected things from happening when I accidentally hit Caps_Lock when I was trying to hit Shift. With Caps_Lock mapped to escape i still got unexpected results. So here is how your bind Caps lock to the Shift key under Xwindows:

Shift and Shift at the same place isn't very useful. One thing that is though is to remap Caps-Lock to Ctrl. Its safe to hit accidentally and it provides a very good place to do Ctrl-W or Ctrl-X Ctrl-L etc.

That's basically how the Happy Hacking keyboard is implemented too, and it's a joy on a normal keyboard.

To actually 'use' the multimedia keys, its useful to be able to map them with nmap or something. The easiest/best way to do this is to find the key's scancode via runing xev and pressing the key, looking at the scancode output, then doing a

xmodmap -e "keycode 139 = F13"

for example, replacing the 139 with whatever your special multimedia key's scancode was, and the F13 with whatever key you want, i used the F keys > 13 since my keyboard doesnt have anything above 12, and you can define upto i think 35 F keys (so i just mapped all my 'multimedia' keys to F13-F29 and then maped those F keys in vim to things i wanted to do, or to my windowmanager to do other stuff)

So thats a method that should work on any keyboard, and doesnt rely on special propriatary(sp) key layout stuff, but it does take a couple steps to get your scancodes n such. Here is what my .xinitrc looks like so i make sure and get all my keys mapped whenever i load X (i just put in the last lines that do anything, your X file may have some funny startup stuff it does)

Note the first two lines are me mapping my Caps Lock to the Escape key, so i dont have to make that horrible reach to the escape key to get outa whatever mode i am in. If you don want that, just dont have the top two lines (i just thought a few people might find that of interest)

Hope that does someone some good. Again, to find out your scancode key, run the program

xev

then start hitting keys, and watch as stuff scrolls by, look for the keypress event as you hit a key, then look for the 'keycode' and thats the number you want!

With regards to xmodmap, you might find it tidier to put all your mappings into (e.g.) ~/.xmodmap and then call "xmodmap ~/.xmodmap" from ~/.xinitrc. Also, for those of you using xdm/kdm/gdm instead of startx/xinit, use ~/.xsession instead of ~/.xinitrc.