49

How can I add a custom compose key sequence?

For example, I would like Compose, |, > to type the symbol.

I have tried adding the following to ~/.XCompose:

# Import default rules from the system Compose file
include "%L"

# Custom definitions
<Multi_key> <bar> <greater> : "▸" U25B8 # Black right-pointing small triangle

but the new sequence is not usable anywhere except for xterm.

Installing ibus-table-compose and then enabling the Compose input method in IBus did not solve the problem; IBus imitated many compose key sequences, but not the one I had created.

ændrük
  • 76,794
  • How do you type the symbol now? You could probably map the command you use to type the symbol to specific keys in keyboard layout. To find the exact keysym of a particular key, you can use xev . But ofcourse, this is just my guess. – Nitin Venkatesh Jun 07 '11 at 15:52
  • 3
    @nitstorm I typed it by pressing AltGr+>, which is a customized mapping that I set by adding the keysym U25B8 to keycode 60 in ~/.xmodmaprc. I'm not asking how to type ▸; I'm asking how to create a compose key sequence for it. – ændrük Jun 07 '11 at 16:05

3 Answers3

24
  1. Create a file named .XCompose in your home directory (~/.XCompose), which contains:

    # import the default Compose file for your locale
    include "%L"
    
    <Multi_key> <bar> <greater>  : "▸"
    

    See man 5 Compose for more info about the format of this file.

  2. Add the following to ~/.profile:

    export GTK_IM_MODULE="xim"
    

    On Ubuntu 18.04 (maybe others), you may need to use the following instead:

    export GTK_IM_MODULE="gtk-im-context-simple"
    
  3. Log off and back on. Or restart X. Or reboot.

xiota
  • 4,849
  • 3
    I noticed that with this solution, I cannot use Ctrl-Shift-u anymore to insert unicode characters by number; so I take it that this switches something general away from the Gnome stuff to the X stuff which is configured by that ~/.XCompose file.

    Is there also a way to modify the Gnome stuff instead? I'd like to keep that Ctrl-Shift-u feature.

    – Alfe Jan 28 '13 at 15:31
  • 2
    @Alfe Using UIM as suggested on the bottom of Ubuntu Forums: .XCompose file not read in 11.04 GNOME works for me with the default settings in Debian jessie/sid (see im-config(8) for customization). That is, .XCompose settings are working both in GTK (GNOME) and Qt (KDE) applications, and .XCompose and Ctrl+Shift+U are both working in GTK applications (like Eclipse). – PointedEars May 10 '13 at 16:22
  • Thank you! So did you just apt-get the uim package or did you have to configure something else? – Alfe May 10 '13 at 19:59
  • 1
    I tried to get this running for some time now, but to no avail. It'd be very helpful to hear what exactly you did besides installing the uim package. At least this could help to be sure that your way doesn't work on Ubuntu 12.04. – Alfe May 10 '13 at 22:32
  • @dan_waterford Hi, I just asked this question here, and had it "possible duplicate"'d to this question. However, I've tried your answer and couldn't get it to work. Any ideas? :) – Owen_AR Nov 15 '13 at 17:42
  • 1
    Note that the default of GTK_IM_MODULE=ibus worked fine for me (using 18.04). – MPi Sep 05 '18 at 15:33
12

Create a file ~/.XCompose (that is a file named .XCompose in your "home" folder) which contains the following:

include "%L"   # import the default Compose file for your locale
<Multi_key> <bar> <greater>     : "▸"

See man 5 Compose for more info about the format of this file.

JanC
  • 19,422
  • 4
    Neat! Is there any way to have this file re-read without restarting X? – Jeremy Kerr Jun 08 '11 at 07:10
  • 1
    This sounds promising, but my ~/.XCompose file doesn't seem to have any effect even after rebooting. Have you gotten this to work? – ændrük Jun 08 '11 at 15:49
  • @Jeremy: I'm not sure, but maybe changing the keyboard layout to another layout and then back might work. – JanC Jun 09 '11 at 12:52
  • 1
    @ændrük: I seem to remember that Gtk/GNOME stupidly messes with the compose key settings (you can test if the new Compose key combination works in e.g. xterm to confirm this is the case). I seem to remember the workaround had something to do with selecting an alternative input method that bypasses the problematic medling; maybe ask a new question about that, because it's not only useful for this but IIRC it also fixes locale-specific Compose maps, etc. ;) – JanC Jun 09 '11 at 13:14
  • Actually, this question is probably relevant. – JanC Jun 09 '11 at 13:36
  • @JanC That's a good suggestion, but I don't think it helps with this problem. I've updated my question to explain. – ændrük Sep 13 '11 at 14:52
  • @ændrük: I see Dan found how to kick Gtk into doing the Right Thing. :) – JanC Nov 01 '11 at 09:47
  • 1
    As of at least 18.04 LTS this answer should now work without needing to change input methods. – ændrük Nov 02 '18 at 16:30
  • @JeremyKerr To make changes effective without restarting X, restart the application where you want to use the new compose sequences. – tanius Oct 02 '20 at 13:50
10

On Ubuntu 14.04 I did the following:

1) Installed uim using the Software Manager, other packages like uim-xim, uim-gtk2, uim-gtk3 and uim-qt are auto installed. See https://launchpad.net/ubuntu/+source/uim.

2) Defined environmental variables by adding the next lines to ~/.profile, this way the custom compose key sequences only apply to the current user:

# Restart the X-server after making alterations using:
# $ sudo restart lightdm
# It seems only GTK_IM_MODULE or QT_IM_MODULE needs to be defined.
export GTK_IM_MODULE="uim"
export QT_IM_MODULE="uim"

The ~/.XCompose-file from the OP should work after restarting 1) Ubuntu or 2) just the X-server by runnung the following command in a terminal:

$ sudo restart lightdm

NB: Restarting only seems necessary after altering the ~/.profile file, alterations to ~/.XCompose will take effect the next time an application (Terminal, Gedit, etc.) starts.

To check whether the environmental variables are set right, enter the following command in your terminal:

$ printenv | grep IM_MODULE

Many thanks to:

About custom compose key sequences:

About custom keyboard mapping:

Example .Xcompose files to mimic Window US International keyboards:

lmeurs
  • 207