28

When writing documents occasionally I want to insert Unicode characters into the text. Sometimes I know the character code and sometimes I look it up on the web.

Currently I go to insert mode and then use <Ctrl-V>u{four digit hex number}, which can be very laborious. Is there an easier way of inserting the characters (maybe some form of pasting)?

  • The other question is really a "special case" of this more generic question; the answers will be the same... My comment there also applies: Use you OS' facilities (whatever they may be), or some other OS-wide program (for examplle when I used Windows, I liked to use AllChars which emulates X11's Compose key; it works in all programs). – Martin Tournoij Feb 26 '15 at 21:55
  • Yes that is a special case and the answers are specialized for it. Do you not think there may be a more generalized solution that encompases more things that people did not consider when looking at the special case. – Martin York Feb 26 '15 at 21:57
  • We could also make that question more generic, or mark that one a dupe of this one (even though it was earlier); I don't really care as such. But I don't think that having both questions as it is will help anyone in the long term :-) – Martin Tournoij Feb 26 '15 at 21:59
  • If I could control the world. Maybe we should let it stand for a day or two so if it gathered any unique responses that could distinguish it from the other question. If it remains unanswered or the answers are just duplicates then close/delete it. – Martin York Feb 26 '15 at 22:09
  • All right .. I definitely agree the other question doesn't have great answers, and this question should attract better ones (hopefully) ... Editing the other question (to make it more generic) still leaves it with a bunch of highly upvoted answers that deal only with the em dash, so that's not great either ... So I've retracted my close vote, and as soon as this gets some decent answers I'll vote the other oone a dupe of this one (even though it was earlier)... I of course can't control what other people choose to vote. – Martin Tournoij Feb 26 '15 at 22:17
23

Preface: This has nothing to do with Vim as such, but I will show you what I consider to be the "best" way to insert these characters by far; Vim's digraph system is more or less the same, but it only works with Vim. This will work in all applications.

This will only work for X11 systems (Linux, BSD, etc.). For Windows, there's AllChars. It hasn't been updated in a while, but I can confirm it works well with Windows 7 (used it at my previous job). I believe OS X also has good facilities to do this built in the OS; if you use OS X, I encourage you to check them out, but as I've never really used OS X I can't point you to them.

The below is (part of) a draft weblog article I have in the pipeline. Unfortunately a truly comprehensive guide doesn't exist (yet), and the below doesn't describe all the features it offers (for example, some parts about dead keys are missing) and could be better written in some parts, but I think it's still "useful enough".


A ‘compose sequence’ is pressing the Compose key and then one or more characters to produce some character not found on your keyboard, for example, pressing Compose, immediately followed by " and a might produce an ä.

By default, Compose isn’t bound to any key1; the Right Alt key (aka. Alt Gr) is often used, but you set this to any key you want.

Set it using xmodmap

You can use xmodmap to set this:

$ xmodmap -e 'keysym Alt_R = Multi_key'     # Set it right Alt
$ xmodmap -e 'keysym Caps_Lock = Multi_key' # Set it to Caps Lock
$ xmodmap -e 'keysym F12 = Multi_key'       # You're free to use *any* key, like F12

You probably want to add this to your ~/.Xmodmap file2:

! Set compose key
keysym Alt_R = Multi_key

Set it using XKB

You can also set the compose key as an option to XKB with setxkbmap:

setxkbmap -option compose:ralt  # Right alt
setxkbmap -option compose:caps  # Caps Lock

To make these permanent, add the command to your X startup file, or alternatively, you can also set it in /etc/X11/xorg.conf:

Section "InputDevice"
    Identifier "Keyboard0"
    Driver "kbd"
    Option "XkbOptions" "compose:ralt"
    #Option "XkbOptions" "compose:caps"
EndSection

Or, in a more ‘modern’ style, you can create a file /etc/X11/xorg.conf.d/90-compose.conf:

Section "InputClass"
    Identifier "Set compose key"
    MatchIsKeyboard "on"
    Option "XkbOptions" "compose:ralt"
EndSection

A list of possible values can be found in xkeyboard-config(7), section ‘Position of Compose key’3.

Setting up dead keys

A dead key is chiefly used to add a accent or diacritic to a letter (such as the umlaut, accent grave, etc.), although it can be used to create any character. It works by...TODO '

keycode 133 = dead_greek NoSymbol SuperR

http://zuttobenkyou.wordpress.com/2011/08/24/xorg-using-the-us-international-altgr-intl-variant-keyboard-layout/
http://stackoverflow.com/questions/14922007/how-to-enter-greek-alpha-under-xor
keycode 48 = dead_grave apostrophe


<dead_grave> <space>       : "`"   grave # GRAVE ACCENT
<dead_grave> <dead_grave>  : "`"   grave # GRAVE ACCENT
<dead_grave> <a>           : "À"   agrave # LATIN CAPITAL LETTER A WITH GRAVE

Making a ~/.XCompose file

The default Compose file if ~/.XCompose is missing is /usr/share/X11/locale/$LANG/Compose. Having your own ~/.XCompose overrides the default, but you can still include the default with:

include "%L"

Changes to any Compose file takes effect when you restart an application. You don't need to restart X.

Compose key

A 'compose sequence' is pressing the Compose key and then one or more characters to produce some character, for example:

<Multi_key> <quotedbl> <a> : "ä" adiaeresis

Means that pressing Compose, immediately followed by " and a produces an ä.

<Multi_key> denotes that we're using the Compose key. we then follow this by a list of one or more keys, these have to be keysyms, which are symbolic representations of keys used by X (See the Keysyms section).

Followed by a :, followed by the result.

The result:

<Multi_key> <a>          :  "ä" adiaeresis
<Multi_key> <b> <b>      :  "ä" adiaeresis
<Multi_key> <c> <c> <c>  :  "ä" adiaeresis
<Multi_key> Alt <d>      :  "ä" adiaeresis
<Multi_key> Ctrl <e>     :  "ä" adiaeresis

Note: A Compose file is case-sensitive, so A is not the same as a.

Dead keys

TODO

Make it work in GTK & Qt

Set the environment variables GTK_IM_MODULE & QT_IM_MODULE to xim.

Bourne shell:

# Make compose key work for GTK, Qt
export GTK_IM_MODULE=xim
export QT_IM_MODULE=xim

C shell:

# Make compose key work for GTK, Qt
setenv GTK_IM_MODULE xim
setenv QT_IM_MODULE xim

See Also

My ~/.XCompose

This is the ~/.XCompose that I use; I used a script to generate this, but I accidentally overwrote this when compiling it >_< So I need to rewrite it.

Also take note of this line:

<Multi_key> <i> <b> : "NL65AEGO0721647952"

Pressing Compose ib will insert this string (a random test IBAN number); very useful for testing applications where such a number is required to create some object (Person, Organisation); XCompose can also serve as a "snippet" tool :-)

Footnotes

1: Some UNIX keyboards had a dedicated Compose key (like this SUN), but this is fairly uncommon these days.

2: Depending on your existing setup, this may or may not be read at startup, depending on your config, add the line xmodmap ~/.Xmodmap to either ~/.xinitrc or ~/.xsession; also see the ArchLinux wiki.

3: Reproduced for your benefit:

    Position of Compose key
    +------------------------------------------------------+
    |Option                Description                     |
    +------------------------------------------------------+
    |compose:ralt          Right Alt                       |
    |compose:lwin          Left Win                        |
    |compose:lwin-altgr    3rd level of Left Win           |
    |compose:rwin          Right Win                       |
    |compose:rwin-altgr    3rd level of Right Win          |
    |compose:menu          Menu                            |
    |compose:menu-altgr    3rd level of Menu               |
    |compose:lctrl         Left Ctrl                       |
    |compose:lctrl-altgr   3rd level of Left Ctrl          |
    |compose:rctrl         Right Ctrl                      |
    |compose:rctrl-altgr   3rd level of Right Ctrl         |
    |compose:caps          Caps Lock                       |
    |compose:caps-altgr    3rd level of Caps Lock          |
    |compose:102           <Less/Greater>                  |
    |compose:102-altgr     3rd level of <Less/Greater>     |
    |compose:paus          Pause                           |
    |compose:prsc          PrtSc                           |
    |compose:sclk          Scroll Lock                     |
    +------------------------------------------------------+
  • 1
    For Windows, use Wincompose instead of Allchars. It does exactly what it says on the tin. I used Allchars until I found Wincompose. The limitation of compose is that you can't have a memorable combination to reach all characters, only the ones that you use often or that have a logical decomposition. – Gilles Feb 27 '15 at 0:28
  • @Gilles Thanks; WinCompose looks nice. You're right that you can only enter a fairly limited subset of "most used" characters. For me this is enough >95% of the time. For the rest I use UniView... I'm not aware of anything significantly better... – Martin Tournoij Feb 27 '15 at 0:38
  • For rarely-used characters, I use Shapecatcher (no editor integration that I know of) or I type the character name (C-x 8 RET in an editor that isn't vi, I don't know if Vim has something similar). – Gilles Feb 27 '15 at 2:16
  • 1
    KDE users cannot use this method as KDE relegates responsibility for implementing this feature to Xorg, and Xorg relegates to Qt, and Qt relegates back to Xorg. I discuss this on my page about typing RTL text. – dotancohen Feb 27 '15 at 8:57
  • @dotancohen That looks like a different issue (as in, feature request). – Martin Tournoij Feb 27 '15 at 14:56
20

To insert Unicode characters such as the euro or copyright symbols, or diacritical marks such as the German umlaut or accent grave, digraphs can be used.

For example, in insert mode press Ctrl+k and type the following:

  • Spanish: a' for á, E' for É, n? for ñ,
  • German: a: for ä, ss for ß,
  • other accented letters: a! for à, a> for â for ê, a? for ã,
  • Greek: a* for α (alpha), b* for β (beta), g* for γ (gamma), d* for δ (delta).
  • square numbers: 22 for ², 33 or ³
  • currencies: Eu for (Euro), Pd for £ (Pound),
  • Co for © (Copyright), DG for ° (Degree),
  • OK for , XX for , Sb for (bullet),
  • punctuation marks: -N for (en dash), -M for (em dash)

When the digraph option is set (:set dg), you can also use the following method:

  • aBackspace: for ä, EBackspaceufor , etc.

To list currently defined digraphs type: :digraphs (:dig).

Source: Entering special characters at the Vim Wikia site.


OS X

If you're using OS X, it's very easy to type accented letters anywhere by the following method:

  • Option+e, then a for á, e for é, i for í and similar,
  • Option+n, then a for ã, n for ñ and similar,
  • Option+u, then u for ü,
  • upside-down punctuation marks: Option+1 for ¡, Option+Shift+? for ¿,

See also:

6

My unicode plugin allows for easy input of unicode characters. Specifically it allows

  • :Digraph <name> - Search digraphs for character

  • :UnicodeTable - Displays a characterset table

  • :UnicodeName - Identify character under cursor

  • Completing of characters using their names or values from the unicode table (:h i_CTRL-X_CTRL-Z) or the digarphs (:h i_CTRL-X_CTRL_G)

Read the help for more info on how to use my plugin.

3

In adition to using the predefined digraphs (Run :dig to get a list), you can also define your own digraphs in vim:

:digr e: 235 a: 228

See also: http://vimdoc.sourceforge.net/htmldoc/digraph.html

3

For commonly used characters, :digraph or external means such as the mentioned Compose key are great. For rarely used ones, I define abbreviations, like this:

norea <unique> unicode_smiley 😃
norea <unique> unicode_skull ☠

And then use my SnippetComplete plugin to get a popup menu that list all such abbreviations, e.g. : unicode_<C-x>]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.