Customizing your keyboard with xmodmap in Elementary OS / Ubuntu

Important

For Elementary OS users Don't

rm /var/lib/xkb/*.xkm

And then restart and expect the keyboard cache files to come back like they do in Ubuntu! You will lose all function of your keyboard!

Like most posts I read on remapping your keyboard, they say to modify your keyboard file in

/usr/share/X11/xkb/symbols/us

Then delete the files in:

rm /var/lib/xkb/*.xkm 

Don't do this! You will not be able to login.

To fix this I had to boot to a live USB and pull the cache files from that session and copy them from that, in doing so I lost all function of my Japanese.

You can do things this way, if you do, make sure you do it like this.

Modify your language file here (example for us settings):

/usr/share/X11/xkb/symbols/us

Delete your keyboard cache files here:

rm /var/lib/xkb/*.xkm 

Important before your restart

setxkbmap -layout us 

Then you can restart.

This isn't the way I decided to go. I decided to use my .bashrc file. This way the key settings are with my user session and easy to revert or change. Also super easy to just add or adjust for the future.

Edit your .bashrc file in your home directory. (ctrl+h to see the hidden files)

The settings I wanted at this time (might change).

  • Left control key becomes the modifier key
  • Caps lock key becomes left control key
  • Right control key becomes caps lock key
  • j+mod key becomes left
  • k+mod key becomes down
  • l+mod key becomes right
  • i+mod key becomes up
  • h+mod key becomes Home
  • n+mod key becomes End
  • u+mod key becomes Page Up
  • o+mod key becomes Page Down

To do this I use xmodmap and add the code to the .bashrc file:

#remove the caps lock
xmodmap -e 'remove Lock = Caps_Lock'
#remove control from left control
xmodmap -e 'remove Control = Control_L'
#set caps lock to = left control
xmodmap -e 'keysym Caps_Lock = Control_L'
#add the control function to left control which is now caps lock
xmodmap -e 'add Control = Control_L'
#remove control from right control
xmodmap -e 'remove Control = Control_R'
#set right control as caps lock
xmodmap -e 'keysym Control_R = Caps_Lock'
#add the lock function to caps lock which is now right control
xmodmap -e 'add Lock = Caps_Lock'

#set the left control to be the mod switch
xmodmap -e 'keycode 37 = Mode_switch'
#set modkey+i to be up
xmodmap -e 'keycode 31 = i I Up'
#set modkey+j to be left
xmodmap -e 'keycode 44 = j J Left'
#set the modkey+k to be down
xmodmap -e 'keycode 45 = k K Down'
#set the modkey+l to be right
xmodmap -e 'keycode 46 = l L Right'
#set the modkey+h to be home
xmodmap -e 'keycode 43 = h H Home'
#set the modkey+n to be end
xmodmap -e 'keycode 57 = n N End'
#set the modkey+u to be page up
xmodmap -e 'keycode 30 = u U Prior'
#set the modkey+o to be page down
xmodmap -e 'keycode 32 = o O Next'

Restart or logout and you should be good to go!

Now if you would like to adjust this to be something else, you can find the keycode by doing this:

Install xmacro:

sudo apt-get install xmacro

Then run

xmacrorec :0.0

Press the key you would like to see and you will see something like this:

The chosen quit-key has the keycode: 44

Important, to escape this you must then hit this key again.

To understand this more, I found a really good post here: http://superuser.com/a/457694

Then to match this setting in Sublime text because these settings will not apply inside Sublime, they run their own keyboard bindings, do this.

Go to Preference -> Key Bindings - User

Then I use this code:

[
{
"keys": ["cntr+j"], "command": "move", "args": { "by": "characters", "forward": false }
},
{
  "keys": ["cntr+k"], "command": "move", "args": { "by": "lines", "forward": true }
},
{
  "keys": ["cntr+i"], "command": "move", "args": { "by": "lines", "forward": false }
},
{
  "keys": ["cntr+l"], "command": "move", "args": { "by": "characters", "forward": true }
},
{
  "keys": ["cntr+o"], "command": "move_to", "args": { "to": "eol" }
},
{
  "keys": ["cntr+u"], "command": "move_to", "args": { "to": "bol" }
},
{
  "keys": ["cntr+h"], "command": "move_to", "args": {"to": "eol", "extend": true}
},
{
  "keys": ["cntr+n"], "command": "move_to", "args": {"to": "bol", "extend": true}
},
]

This got me all up and going just how I like.

Hope this helps someone else!