Friday, July 24, 2015

Vim: Rebind Caps Lock And Escape

In this article I discuss how I managed to bind Escape to my Caps Lock key and Caps Lock to Escape, not only for my OpenBox environment, but also for my Virtual Terminal/Console.

I recently decided to finally give Vi/Vim a try. Previously, I thought it was too arcane and tried Emacs instead. I was an avid Emacs user and it became my editor and IDE of choice. So why did I decide to switch?
My decision came down to three (3) things:
1. Why not?
2. Striving to be more productive (i.e. lazier...i.e. less keystrokes)
3. Vi is installed by default on Unix, so an advanced editor is always ready. So are ee, ed and a whole range of others, but I like the challenge and wanted to see what the rage was all about. Yes Emacs came with Mac OS X by default albeit an older version, so I still had to install the latest. On FreeBSD? pkg install emacs is your friend.
While using Emacs on OS X, I could easily remap the keybindings for Caps Lock to Control, in fact, it was a builtin feature of the OS. It's a bit more work to get it done on FreeBSD, so here goes.

OpenBox setup:

This was taken from the vim wikia:

  1. vi ~/.vimkeyswapper – you can name the file anything you want, I just wanted the easy reminder everytime I looked at it. This was easier to understand at a glance over '.xmodmaprc'.
  2. Edit the file to include the following contents:
    ! Swap CAPS LOCK and ESC
    remove Lock = Caps_Lock
    keysym Escape = Caps_Lock
    keysym Caps_Lock = Escape
    add Lock = Caps_Lock
  3. Edit either your ~/.config/openbox/autostart if you are using a login manager or your ~/.xinitrc if, like me you only need to start X when you really need to
    xmodmap ~/.vimkeyswapper
  4. Restart X, or reboot just to make sure it takes effect.

Console setup:

Alright, be careful here. We're going into the file system, which requires root privileges. Ironically, I found the original solution here.
  1. We need to decide which keyboard mapping we are using. So you'll need to install misc/kbdscan if you don't already have it installed.
    $ cd /usr/ports/misc/kbdscan && sudo make install clean
  2. Please note, you have to be in the console to be using this, so you can't be logged into X and use your terminals. So ALT+F1 or ALT+F(n) to whichever console you like and login.
    $ kbdscan
    Press and release Caps Lock and make a note of which number appears, for me it was 58
    Press and release Esc, the number I got was 1.
  3. Okay, you may have to do some digging around to find which files match the numbers you got.
    $ cd /usr/share/syscons/keymaps && ls
    Based on your locale, language or keyboard setup, it may be different for you. The file that matched my bindings were us.iso.kbd. [1]
    $ sudo cp us.iso.kbd us.vim.kbd
  4. Edit the file, so it should look something like this:
    # $FreeBSD: releng/10.1/share/syscons/keymaps/us.iso.kbd 74119 2001-03-11 23:41:19Z ache $
    # alt
    # scan cntrl alt alt cntrl lock
    # code base shift cntrl shift alt shift cntrl shift state
    # ------------------------------------------------------------------
    000 nop nop nop nop nop nop nop nop O
    001 clock clock clock clock clock clock debug clock O
    ...
    058 esc esc esc esc esc esc esc esc O
    [2]


    I don't know if it made a difference or not, but I realigned the text before saving the file.
  5. Add this to your /etc/rc.conf file: keymap="us.vim.kbd"
  6. Reboot: # reboot OR $ shutdown -r now
  7. And there you have it, if done correctly, your Caps Lock will now be your new Esc key. Happy coding![3]
[1] – On a side note, they already have us.emacs.kbd, no fair to us vim users :-)
[2] – Ellipses denotes more content, I just did this to highlight the relevant changes. Please don't remove anything from this file.
[3] – I am not sure if this affects console only or the X environment as well, making the xmodmap command unneccessary.

No comments:

Post a Comment