Permanent xmodmap in Ubuntu 13.04

Solution 1:

Ubuntu no longer uses xmodmap, but instead uses xkb (as far as I understand, this facilitates per-window keyboard layouts, among other things).

The system-wide map files are in

/usr/share/X11/xkb/symbols/

The maps appears to be loaded hierarchically, with the pc map as the root, and whatever language (e.g. en) as a child of the root.

In my particular case, I physically swapped Page Up with Home and Page Down with End on my keyboard, so I very crudely made changes to the system-wide pc map, as follows:

 key <HOME> {    [  Prior        ]   };
 key <PGUP> {    [  Home         ]   };
 key  <END> {    [  Next         ]   };
 key <PGDN> {    [  End          ]   };

NOTE: To force Xorg to use your new keyboard map, you may have to remove the existing pre-compiled maps (*.xkm) in

/var/lib/xkb/

and then restart Xorg.

There are various resources on xkb, but this one is related to Ubuntu.

Solution 2:

For me, the xkb configuration is too complicated if you want to remap 1 or 2 keys in a way not available in the System Settings. Plus, the solution should remain on the user side, without editing '/usr/share/X11/xkb/symbols/' etc. Force the 'xmodmap ~/.Xmodmap' load using the Startup Applications only works until you suspend, change users or change the keyboard layout. So, as holocronweaver suggests, with code snippets I found, I made a python script to be loaded on session startup, that reloads the '~/.Xmodmap' when needed.

Check the bug report #1243642 (comment #6), and download it here.

Solution 3:

I had this problem in Ubuntu 14.04, and found the solution here. Apparently, Ubuntu is not able to configure the keyboard right away after logging in, but it is a bit later. So if you add a command

sleep 4 && xmodmap ~/.Xmodmap &

to ~/.bash_profile then it should work (it does for me).

EDIT: This seems to work only after logging in, not after starting up. I'll have to investigate this later.

Solution 4:

Convert Caps Lock into Esc for vim use

Here is my variant of a key mapping startup script. It converts Caps Lock into Esc on whatever keyboard for use with vi, vim or gvim.

Unlike the answer of q4w56, this answer does not use any infinite while; do loop. Otherwise, one core of my dual core CPU would hit 42% every 5 seconds, severely reducing my laptop battery charge.

#!/usr/bin/env bash
sleep 4
xmodmap -e "clear Lock"
xmodmap -e "keysym Caps_Lock = Escape"

Save it as a script. Make the file executable with chmod +x. Add the script name and location to Session and Startup → Application Autostart (in Xubuntu).

The keyboard behaviour for this user will remain persistent through reboot and logout, but not suspend. Persistence through suspend can be achieved, but unfortunately only at the system level, not readily at the user level.

EDIT

I moved away from above solution in favour of this solution which offers, apart from the Esc function on CapsLock, also the arrow keys and the keys PgUpHomeEndPgDn through CapsLock+HJKL, respectively CapsLock+UIOP.

Rationale for this keyboard remapping

In today's computing world there is very little use left for the Caps Lock key. In general, one should refrain from using ALL CAPS by virtue of both netiquette and the separation of presentation and content. Moreover, accidentally hitting the Caps Lock key whilst editing text with vim is not always immediately noted, but invariably results in rather annoying vim behaviour further on. Disabling the Caps Lock key all together and repurposing it as an easy to reach Esc key results in a double win for vim use.