Bind/map Character to alt+[some key]?

Posted on

Problem :

OS: Ubuntu

In programming and various terminal programs (Screen, Vim) the [, ], { and } tends to be used a lot. I’m using a Norwegian keyboard where these are placed such that I have to stretch my fingers a bit too long for whats comfortable. To make it easier I though I’d try to make alt+[some key] be one of these characters. Is there a way that I can bind, say alt+æ (Norwegian letter) to ‘{‘ system wide?

Btw, is such thing called binding, mapping or something else? I’m getting a bit confused by the terms… 🙂

Solution :

Unfortunately it isn’t going to be possible using Alt+key. You see typing Alt+key in a command window is really just the same as pressing Escape followed by the key.

However I do have a sort-of solution for you, but it requires a bit of leg work and assumes you are running X. Your screen/vim in a terminal emulator will still work with this though.

I use a Spanish keyboard and the []{} symbols are on the keys to the right of my little finger on the right hand, but require pressing the AltGr key too. That means scrunching up my right hand a bit too much. I changed this using xmodmap.

If you add a file called .Xmodmap to your home directory then it is sourced every time you log in to X. Alternatively you can execute it at any time using xmmodmap ~/.Xmodmap.

Here’s an example on how to set this up. The right brace symbol } is on a key with ç on my keyboard, but I have to press AltGr together with the key to get the right brace. To see what keycode is mapped to this, I use xmodmap -pk:

$ xmodmap -pk | grep brace
51 0x00e7 (ccedilla) 0x00c7 (Ccedilla) 0x00e7 (ccedilla) 
   0x00c7 (Ccedilla) 0x007d (braceright) 0xfe55 (dead_breve)
# split to prevent scroll, this is all on one line

This means that keycode 51 is mapped to these keys with the various modifiers. Here is what man xmodmap has to say:

 The list of keysyms is assigned to the indicated keycode (which may be
 specified in decimal, hex or octal and can be determined by running the xev
 program). Up to eight keysyms may be attached to a key, however the last
 four are not used in any major X server implementation. The first keysym is
 used when no modifier key is pressed in conjunction with this key, the
 second with Shift, the third when the Mode_switch key is used with this
 key and the fourth when both the Mode_switch and Shift keys are used.

So here we have:

  • no modifier = ccedilla ç
  • shift = Ccedilla Ç
  • Mode_switch = ccedilla
  • Mode_switch and shift = Ccedilla
  • AltGr = braceright }

Experimenting, it seems that the fifth entry is actually when AltGr and the key is pressed. So much for documentation, eh? I have no idea what the Mode_switch key is either, it certainly isn’t Alt, Shift or Ctrl.

I wanted to change this most useless of keys to have } by default. The entry in my .Xmodmap is as follows:

keycode 51 = braceright Ccedilla ccedilla Ccedilla braceright dead_breve

I never write ccedillas (except in this post, and here I’ve written it by typing a Ç then in use guw in Vim to make it lower case!) so I don’t miss them. Other keys, such as the key with ] on it were trickier. This key has *+] written on it. I moved the plus onto another key (removing the duplicate upside down exclamation mark ¡), had it spit out ] by default and * with shift. The AltGr behavior I left untouched.

I have similar entries for other keycodes corresponding to the other coders’ keys. Sometimes I think I should relearn a US or UK layout just for hacking. I’d miss the fancy keys in emails though.

Leave a Reply

Your email address will not be published. Required fields are marked *