How can I map the spanish eñe letter to a key combination? What application is suitable for this purpose (xmodmap, xbindkeys, ...)?

When I press Caps Lock + n, it should type ñ

When I press Caps Lock + N, it should type Ñ

I'd like to do this without switching keyboard layouts; I want to use only the English keyboard layout.

  • Do you want Caps Lock to also have the caps lock effect? Do you also have an AltGr key (i.e. a key that makes some character keys insert a different character, never mind that it's the right Alt key or not)? Commented Apr 29, 2011 at 22:37
  • Caps Lock can lose its effect. Or it can be mapped to Fn key + n or something what is comfortable to press and not colliding (Alt is colliding and Win key is already in use).
    – xralf
    Commented Apr 30, 2011 at 13:25

3 Answers 3


Not exactly what you want, but instead of Caps Lock you can use AltGr:

AltGr + n producing ñ

AltGr + N producing Ñ

To get that with xmodmap you must know the keycode which produces the N. This can be done with:

xmodmap -pke | grep "n N n N"

where you may get something like: keycode 57 = n N n N n N. This means that the keycode 57 represents the n key.

Then, you remap this keycode, to produce the ñ and Ñ with:

xmodmap -e "keycode  57 = n N n N ntilde Ntilde"

(with this command, the fifth position after the = is produced when you type that key in combination with AltGr, and the sixth position when AltGr and Shift are pressed together).

If this woks, you can make it to run automatically after login.

  • Thank you but this doesn't work. When I press AltGr + n it prints :. And it would be better to map it with caps lock or other key because AltGr would conflict in many applications.
    – xralf
    Commented Apr 29, 2011 at 17:27
  • You show 6 values: n N n N n N .. I only get 4 values in the output from xmodmap (Ubuntu) and it works when I set the 3rd and 4th values, but fails on 5th and 6th... You even mention the words "fitth" and "sixth" ... Is there a difference in implementation of xmodmap from one distro to another?
    – Peter.O
    Commented Apr 30, 2011 at 18:56
  • @fred: I use ubuntu in two different machines and in both of them I get six values for that command... I don't know why the difference with you. May be it doesn't depend on the distribution but it does according to other circumstances or settings...
    – nozimica
    Commented Apr 30, 2011 at 22:08
  • @xralf: Are your Alt and AltGr keys identical in behaviour?
    – nozimica
    Commented May 1, 2011 at 1:09

This answer explains how to set this up with xmodmap. Put the configuration snippets below in ~/.Xmodmap, and run xmodmap <~/.Xmodmap to apply them. Depending on your distribution and your setup, ~/.Xmodmap may be loaded automatically when you log in, or you may need to call xmodmap explicitly from ~/.xinitrc or ~/.xsession, or you may need to configure your desktop environment to apply ~/.Xmodmap.

X has a keysym (i.e. an abstract key name) called Mode_switch. On most keyboard mappings for latin-script languages other than US, this keysym is bound to the key labeled AltGr, which replaces the right Alt key. You can choose to map Mode_switch to a different key such as Caps Lock. On a PC keyboard, the keycode (what the hardware sends) for Caps Lock is 66, so change its binding to Mode_switch, and remove the caps lock modifier from it:

keycode 66 = Mode_switch
clear Lock

You also need to associate a modifier with Mode_switch. There are 5 custom modifiers, Mod1 through Mod5; any will do, but there has to be one. Run xmodmap -pm to see what modifiers are in use, and pick one of the 5 that isn't, then add a line like this to your .Xmodmap:

add Mod3 = Mode_switch

In an xmodmap key specification, the character sent by the key with Mode_switch is in the third column after the = sign, and with Mode_switch+Shift in the fourth column. (The first two columns are for the key with no modifiers and with Shift.) You can use a keysym directive to rebind the key that now sends n regardless of its keycode:

keysym n = n N ntilde Ntilde

The names on the right are in fact keysym names. You can find a list of these in /usr/include/X11/keysymdef.h, e.g. the line #define XK_Ntilde 0x00d1 means there's a keysym called Ntilde that corresponds to Unicode character U+00D1. There are characters that don't have a keysym name; you can use the unicode number instead.

keysym 5 = 5 percent U2030 U2031

Note that if you're shifting modifiers around on systems of ~2009–2011 vintage, you might run into an X_SetModifierMapping bug. Often, but not always, using clear Lock will work around the bug.

  • This works perfectly. Thanks... very interesting... keysym n = and keycode 57 =, do the same thing, but I suppose that using keysym is more portable... another mystery demystified... (and it is easy to toggle it via a hotkey, to quickly get access the caps-lock again) ...
    – Peter.O
    Commented Apr 30, 2011 at 18:22
  • This worked only for a while. After a few sentences it swapped n with ntilde. So, I have to press mod_switch + n to type n and n to type ntilde. It happened already three times, so I believe there is something wrong with this mod_switch.
    – xralf
    Commented May 1, 2011 at 16:34
  • @xralf: Did you add clear Lock? (I forgot it in my original answer.) Commented May 1, 2011 at 16:38
  • @Gilles Actually I used rather keycode 94 = Mode_switch without clear lock.
    – xralf
    Commented May 1, 2011 at 16:42
  • @xralf: If you rebound a key that isn't CapsLock, you don't need the clear Lock part. But you need another part I'd forgotten (sorry): there must be a modifier associated with Mode_switch. See my edit. Commented May 1, 2011 at 16:53

If you don't mind having caps lock not work anymore as a 'caps lock' key, you can remap it to an 'altgr-like' status with xmodmap -e 'keysym Caps_Lock = ISO_Level3_Shift'.

  • I've never used an AltGr "enabled" keyboard before, but I think that simply setting Caps-Lock to be AlgGr isn't enough... all relevant keys would need to be set to act differntly when AlgGr is pressed... A standard US keyboard mapping is not aware of anything other than ASCII characters.
    – Peter.O
    Commented Apr 30, 2011 at 18:34
  • Well, you would also have to do the mapping mentioned in the other answer. I thought that would go without saying.
    – Random832
    Commented Apr 30, 2011 at 20:52

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .