With X (Linux), as @Dan Andreatta mentioned, use xmodmap
to alter the key mapping, e.g.
xmodmap -e 'clear Lock' -e 'keycode 0x42 = Escape'
Will map Esc to the CapsLock key.
You could also set this in stone by adding it into an X initialization file, such as .xinitrc
— which is run when using startx
—:
clear lock
keycode 0x42 = Escape
Typically, this is put inside an .Xmodmap
file and then it's run inside .xinitrc
.
Vim's documentation has an entire page dedicated to this topic, and the comments section is especially helful.