Ubuntu – Configure Caps Lock as AltGr and Arrows like in vim

keyboard-layoutshortcut-keysvim

1. How do I globally map Caps Lock to AltGr?

I don't need CapsLock and rather would like to have an easy access AltGr Key there

2. I want to map all movement keys to vim-like positions

  • AltGr+F = Backspace
  • AltGr+H = LeftArr
  • AltGr+J = DownArr
  • AltGr+L = RightArr
  • AltGr+K = UpArr
  • AltGr+U = PgUp
  • AltGr+D = PgDown
  • AltGr+S = Enter
  • AltGr+3 = Pos1
  • AltGr+$ = End
  • AltGr+X = Del

So I can easily walk through my code without moving the hand away from the 10-Finger-Position (like in vim editor)

I found this answer: How do I remap the caps lock key to the backspace key?
suggesting using

xmodmap -e "keycode [code] = [new key]"

see: http://wiki.linuxquestions.org/wiki/List_of_Keysyms_Recognised_by_Xmodmap

But I cannot figure out, how to add this to a combination of for example AltGr+J

Another start would be to set the "Alternative Character Key" in unity-control-center->Keyboard->Shortcuts->Typing as Caps Lock

Update:
I found some solutions (see below) but all of them don't work in all applications, I guess it is a global setting called "XFree 4" that is used by some apps.

1. How do I set the key bindings for XFree 4 also?

2. How do I make the changes upgrade-save?

Best Answer

1. add a new XKB partial file

sudo vi /usr/share/X11/xkb/symbols/altgr_vim

with this content:

partial keypad_keys
xkb_symbols "altgr-vim" {
  # replace Caps with AltGr
  key <CAPS> { [ ISO_Level3_Shift            ] };
  # Add vim cursor keys to be accessed with AltGr
  key <AB02> { [ x, X, Delete, Delete        ] };
  key <AC02> { [ s, S, KP_Enter, KP_Enter    ] };
  key <AC03> { [ d, D, Next, Next            ] };
  key <AC04> { [ f, F, BackSpace, BackSpace  ] };
  key <AC06> { [ h, H , Left, Left           ] };
  key <AC07> { [ j, J, Down, Down            ] };
  key <AC08> { [ k, K, Up, Up                ] };
  key <AC09> { [ l, L, Right, Right          ] };
  key <AD07> { [ u, U, Prior, Prior          ] };
  key <AE03> { [ 3, section, Home, Home      ] };
  key <AE04> { [ 4, dollar, End, End         ] };
};

2. include the partial in your language file

for example for german it is /usr/share/X11/xkb/symbols/de (where /de is your language) add this inside the first block (xkb_symbols "basic"):

include "altgr_vim(altgr-vim)"

Note: This part has to be repeated after every distribution update

3. reload the window session

Press ALT+F2, enter "r" to reload the gnome session or just log out and in again. Now the new key compositions will be available

4. solve some problems

  1. Some applications don't accept the settings, for example: sublime which can be replaced by atom.

  2. Some applications need extra settings, e.g. in yakuake the key-bindings are set to "XFree 4", if you set them to "Linux" or "Solaris", then the XKB settings works there also:

remaining problems:

  1. some apps still don't accept those settings
  2. after an upgrade, where the symbols file is updated, you have to repeat step 2
Related Question