Friday, July 24, 2015

Use VIM-like HJKL Instead Of Arrow Keys in Linux and OSX

Okay, I admit it...  I move around the file even while in the insert mode of Vim instead of going back to normal mode.  I don't always stay in insert mode, but if I'm typing and notices a typo a few characters back I will go back to the typo without leaving insert mode.  I do this by mapping the CTRL-[HJKL] to left, down, up, right so at least my fingers stays on the home row and not move to the arrows (I have disabled arrow keys in normal mode when I was training myself on Vim and never re-enabled it).

I found that after spending time in VIM if  I switch to other apps I will start using HJKL for navigation.  Of course that usually doesn't work and I end up using the arrow keys (e.g. when moving down menu options in the browser).  Naturally if I'm using those apps a lot more then when I go back into Vim I'll get lazy and start trying to use the arrow keys.  I decided what I want is to use HJKL even when I'm not in Vim and I wanted it to be consistent across Linux and OSX.

On OSX, this is easy to do.  Simply install Karabiner and select the option to use VI mode using the control key.  Then, in any app, simply use CTRL-[HJLK] for the equivalent behavior as using the arrow keys.  To add more consistency between OSX and Linux, I remapped Caps-Lock to the CTRL key (which is also easier to type on a mabook pro).

On Linux, it's a little more work.  I use Caps_Lock as the Switch_mode (SM) key and then defined that SM-[HJLK] combination to be like the arrow keys.  To do this:

xmodmap - pke > ~/.Xmodmap

Add this line to the top of the .Xmodmap file to disable the Caps_Lock key:

clear Lock

Then set the Caps-Lock key to be the Switch-mode key:

keycode 66 = Mode_switch Caps_Lock

This means to set the caps_lock to be the mode_switch key (and caps-lock when it is Shift-Caps_Lock)

Next, add the Left/Down/Up/Right to the third column of the key code record that maps to the letters, HJKL.

keycode 43 = h H Left
keycode 44 = j J Down
keycode 45 = k K Up
keycode 46 = l L Right

This means that for key 45, print "k" when the key is pressed, "K" when it is SHIFT-k and Up when CapsLock-k is pressed (since CapsLock is now the mode_switch key).

Once you save the file, load it with

xmodmap .Xmodmap

Most current desktops will load .Xmodmap when logging in.
You can find out what keycodes maps to what by using xev.

1 comment:

  1. You'll want to remove that space before 'pke' in the first command. Otherwise, a great article, thanks for the idea. I personally went with IJKL, since I'm not a Vim user.

    ReplyDelete