VIM Key Bindings In Ubuntu Using Xmodmap

By , 27 July 2011

VIM Key Bindings In Ubuntu Using Xmodmap

I really did deserve to be slapped for cleaning my laptop keyboard using water. My punishment was a bunch of dead keys, including the left arrow key. This is really annoying, and as it happens they simply don't stock replacement keyboards for my laptop in Lima. I searched everywhere.

With no other real option than to remap my broken keys I stumbled across something really cool in Ubuntu / Linux. Xmodmap is a utility I had used before for remapping dead keys, but I wasn't aware of the Mode_switch modifier which basically acts as an additional Control / Shift button.

The magic happens when you map Mode_switch to the otherwise useless Caps Lock key. Voila! Instantly you have a whole new set of shortcuts available using Caps Lock that you can use for ALL your applications. I've set up VIM style navigation keys, and shortcuts for hard to reach common keys like escape, backspace, delete, page up and page down.

VIM Key Bindings In Ubuntu Using Xmodmap

It's freaking awesome. I haven't even removed my hands from touch typing position to type this entire article into the browser even though I've been making errors and scrolling around the article to edit it.

VIM navigation uses h l j and k respectively for left, right, up and down. It's fantastic for programming but also great for web browsing, word-processing and spreadsheets. I never realised how often I reached for the mouse just to scroll through a document.

Having backspace on your fingertips is a huge time saver too, and as a bonus, I've also mapped Caps-G the a mouse click so I don't have to remove my thumb from the trackpad when I use it.

Here is the xmodmap script I'm using. To run the script just save it and execute "xmodmap <filename>". You need to enable keyboard control of the mouse in your settings for the mouse-click shortcut to work.

! use caps lock as additional modifier
clear Lock
keycode 66  = Mode_switch

! application shortcuts
keycode 54 = c C Escape
keycode 58 = m M Return
keycode 40 = d D Delete
keycode 41 = f F BackSpace
keycode 42 = g G Pointer_Button1

! navigation shortcuts
keycode 29 = y Y Home
keycode 30 = u U Next
keycode 31 = i I Prior
keycode 32 = o O End

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

Basically, the three settings for each key are the unmodified, Shift-modified and Caps-modified (i.e. Mode_switch) output for each key. In the case that your keycodes are different to mine, you can use "xev" to test your keyboard's keycodes.

Have fun!


About Roger Keays

VIM Key Bindings In Ubuntu Using Xmodmap

Roger Keays is an artist, an engineer, and a student of life. Since he left Australia in 2009, he has been living as a digital nomad in over 40 different countries around the world. Roger is addicted to surfing. His other interests are music, psychology, languages, and finding good food.

Leave a Comment

Please visit to add your comments.

Comment posted by: , 5 years ago

Glad you found the article useful. Don't forget to share to your favourite social network ;)

Comment posted by: Chris Bennett, 5 years ago

Hey, I just stumbled on this tip looking for vim keys for when using a spreadsheet (gnumeric).  Turns out your tip is even more useful since it's app-agnostic... I'm very happy with the new setup.  Thanks for publishing.