8

Currently, I'm inserting unicode characters (mainly math symbols) using TeX input method. This is cumbersome, since, for each character, I have to do following:

  1. Switch to TeX input method pressing C-\
  2. type latex expression like \Bbb{R} or \Leftrightarrow

There is relatively small subset of unicode characters (10-20) I am inserting all the time.

Q: Is there way to insert unicode characters with few (2-4) keystrokes?

2

3 Answers 3

6

You can use abbrev-mode to create strings that "expand" to the unicode characters you want. For example, the following abbrev table defines abbrevs for the first three lowercase letters of the Greek alphabet:

(define-abbrev-table 'unicode-table
  '(("ualpha" "α")
    ("ubeta"  "β")
    ("ugamma" "γ"))
    "Unicode characters I use all the time.")

You can then tell text-mode (or whatever other modes you want) to inherit your unicode table:

(define-abbrev-table 'text-mode-abbrev-table
  nil
  "Text mode abbrev table."
  :parents (list unicode-table))

Go to a buffer in text mode. Turn on abbrev-mode. Type in ualpha and a non-word character (space, period, etc.), and watch it expand to α.

6

You can define your own key sequences with the elisp functions global-set-key and local-set-key. Thereby, it is recommended to use C-c and a single letter for user defined key sequences. (Otherwise it may be that you override key-sequences bound by other emacs-lisp libraries. Later you get into trouble when you discover that the library version of the key sequence would be very useful for you.)

If you want to use these sequences independent of the major mode you can bind them globally.

For an example you can add

(global-set-key (kbd "C-c a") "⇔")

to your configuration file ~/.emacs.

Alternatively, you can use these keys depending on the major mode.

An example is:

(defun my-latex-hook-function () (local-set-key (kbd "C-c a") "⇔"))
(add-hook 'TeX-mode-hook 'my-latex-hook-function)

This works with auctex. If you are using the native tex-mode or latex-mode it might be that you have to replace TeX-mode-hook by tex-mode-hook.


Note, that there is also insert-char bound to C-x 8 RET. Therewith you can input utf8-characters if you know their code. If you do not know the code you can input them by their description but that becomes cumbersome.


There are also special modes supporting the user with the input of latex symbols, such as company-math-Mode and ac-math-mode.

Both of them are completion modes. company works with mouse menus and ac (autocomplete) works with the tab-key.

But I am not sure about the current status of these packages. You can install these modes per melpa if you want to try them.

4
  • FWIW: Users can use any key sequences they like. The advantage of a key sequence that starts with C-c is that you can be pretty sure it will not conflict with any key bindings provided by Emacs or a library you might use.
    – Drew
    Commented Feb 21, 2016 at 21:44
  • @Drew Yes, I know that. The potential risk of conflicts with library key sequences is the reason for my recommendation.
    – Tobias
    Commented Feb 21, 2016 at 21:52
  • Yes, I figured that. My comment was for the benefit of any readers of your answer who might get a different impression from "you should follow the convention to use C-c and a single letter for user defined key sequences." There is no convention for user-defined keys to use C-c plus a letter. I'm sure we agree - I just wanted this to be clear to readers. If you had said that you recommend that they use C-c plus a letter (bonus points for saying why) then I would not have commented. HTH.
    – Drew
    Commented Feb 21, 2016 at 22:38
  • @Drew That was the reason why I added the link. The linked text makes everything clear. Now, I added also a note in the text.
    – Tobias
    Commented Feb 22, 2016 at 7:27
3

You say: There is relatively small subset of unicode characters (I'm guessing 10-20) I am inserting all the time.

In that case, as @Tobias indicated, you can just bind, for each of them, a simple key sequence to either (a) a command that inserts that character or (b) a single-character string with that character (this acts like a keyboard macro, to insert the character).

Library ucs-cmds.el can help you define such commands. The resulting commands have the same names as the characters. You can use macro ucsc-make-commands to define a whole set of commands that insert a character of a certain kind.

For example, you can put this in your init file to define commands that insert all of the Unicode math symbols:

(require 'ucs-cmds)           ; Load the library
(ucsc-make-commands "^math")  ; Use the macro to create commands

That gives you commands whose names are the same as the math characters. Bind any of these that you use a lot to easy-to-use-keys. For example:

(global-set-key (kbd "C-c S") 'mathematical-bold-capital-sigma)

(Or bind a key to such a command only in Tex mode, using define-key with its keymap.)

With just that simple (ucsc-make-commands "^math") you have available all of the character-inserting commands that are listed below.

There are also other Unicode characters, whose names do not start with math but which are used in mathematics - for example, character PARTIAL DIFFERENTIAL. You can easily create commands that insert any of those characters too. (And note that the characters whose names start with math might well require using a special font - many fonts do not support them.)

You can of course use a tighter regular expression than ^math, if you want to define only characters matching such a pattern. For example, this defines commands for all of the partial-differential characters:

(ucsc-make-commands "^math.*partial differential")

That gives you these commands:

mathematical-bold-italic-partial-differential
mathematical-bold-partial-differential
mathematical-italic-partial-differential
mathematical-sans-serif-bold-italic-partial-differential
mathematical-sans-serif-bold-partial-differential

Note that the Unicode character names separate words with space characters, and the names are uppercase. The corresponding insertion commands use hyphens instead of spaces and lowercase instead of uppercase.

One advantage of having commands named after chars they insert, instead of, say, just binding keys to singleton strings of the characters, is that you can use M-x to enter characters that you use less often, by name using completion. Instead of completing against the entire set of Unicode character names, you complete against the names of character-inserting commands you have defined.


These are the commands that insert characters whose names start with math. They are what you get by evaluating (ucsc-make-commands "^math").

mathematical-bold-capital-a
mathematical-bold-capital-alpha
mathematical-bold-capital-b
mathematical-bold-capital-beta
mathematical-bold-capital-c
mathematical-bold-capital-chi
mathematical-bold-capital-d
mathematical-bold-capital-delta

...

mathematical-bold-digit-eight
mathematical-bold-digit-five
mathematical-bold-digit-four

...

mathematical-bold-epsilon-symbol
mathematical-bold-fraktur-capital-a
mathematical-bold-fraktur-capital-b

...

mathematical-bold-fraktur-small-a
mathematical-bold-fraktur-small-b

...

mathematical-bold-italic-capital-a
mathematical-bold-italic-capital-alpha
mathematical-bold-italic-capital-b
mathematical-bold-italic-capital-beta

...

mathematical-bold-italic-nabla
mathematical-bold-italic-partial-differential
mathematical-bold-italic-phi-symbol
mathematical-bold-italic-pi-symbol
mathematical-bold-italic-rho-symbol
mathematical-bold-italic-small-a
mathematical-bold-italic-small-alpha
mathematical-bold-italic-small-b
mathematical-bold-italic-small-beta

...

mathematical-bold-nabla
mathematical-bold-partial-differential
mathematical-bold-phi-symbol
mathematical-bold-pi-symbol
mathematical-bold-rho-symbol
mathematical-bold-script-capital-a
mathematical-bold-script-capital-b

...

mathematical-bold-script-small-a
mathematical-bold-script-small-b

...

mathematical-bold-small-a
mathematical-bold-small-alpha
mathematical-bold-small-b
mathematical-bold-small-beta

...

mathematical-double-struck-capital-a
mathematical-double-struck-capital-b

...

mathematical-double-struck-digit-eight
mathematical-double-struck-digit-five
mathematical-double-struck-digit-four
mathematical-double-struck-digit-nine
mathematical-double-struck-digit-one
mathematical-double-struck-digit-seven
mathematical-double-struck-digit-six
mathematical-double-struck-digit-three
mathematical-double-struck-digit-two
mathematical-double-struck-digit-zero
mathematical-double-struck-small-a
mathematical-double-struck-small-b

...

mathematical-falling-diagonal
mathematical-fraktur-capital-a
mathematical-fraktur-capital-b

...

mathematical-fraktur-small-a
mathematical-fraktur-small-b

...

mathematical-italic-capital-a
mathematical-italic-capital-alpha
mathematical-italic-capital-b
mathematical-italic-capital-beta

...

mathematical-italic-nabla
mathematical-italic-partial-differential
mathematical-italic-phi-symbol
mathematical-italic-pi-symbol
mathematical-italic-rho-symbol
mathematical-italic-small-a
mathematical-italic-small-alpha

...

mathematical-italic-small-dotless-i
mathematical-italic-small-dotless-j
mathematical-italic-small-e
mathematical-italic-small-epsilon

...

mathematical-italic-small-final-sigma

...

mathematical-left-angle-bracket
mathematical-left-double-angle-bracket
mathematical-left-flattened-parenthesis
mathematical-left-white-square-bracket
mathematical-left-white-tortoise-shell-bracket
mathematical-monospace-capital-a
mathematical-monospace-capital-b

...

mathematical-monospace-digit-eight
mathematical-monospace-digit-five
mathematical-monospace-digit-four

...

mathematical-monospace-small-a
mathematical-monospace-small-b

...

mathematical-right-angle-bracket
mathematical-right-double-angle-bracket
mathematical-right-flattened-parenthesis
mathematical-right-white-square-bracket
mathematical-right-white-tortoise-shell-bracket
mathematical-rising-diagonal
mathematical-sans-serif-bold-capital-a
mathematical-sans-serif-bold-capital-alpha
mathematical-sans-serif-bold-capital-b
mathematical-sans-serif-bold-capital-beta

...

mathematical-sans-serif-bold-digit-eight
mathematical-sans-serif-bold-digit-five
mathematical-sans-serif-bold-digit-four

...

mathematical-sans-serif-bold-italic-capital-a
mathematical-sans-serif-bold-italic-capital-alpha
mathematical-sans-serif-bold-italic-capital-b
mathematical-sans-serif-bold-italic-capital-beta

...

mathematical-sans-serif-bold-italic-nabla
mathematical-sans-serif-bold-italic-partial-differential

...

mathematical-sans-serif-bold-italic-small-a
mathematical-sans-serif-bold-italic-small-alpha
mathematical-sans-serif-bold-italic-small-b
mathematical-sans-serif-bold-italic-small-beta

...

mathematical-sans-serif-bold-italic-small-final-sigma

...

mathematical-sans-serif-bold-nabla
mathematical-sans-serif-bold-partial-differential

...

mathematical-sans-serif-bold-small-a
mathematical-sans-serif-bold-small-alpha
mathematical-sans-serif-bold-small-b
mathematical-sans-serif-bold-small-beta

...

mathematical-sans-serif-bold-small-final-sigma

...

mathematical-sans-serif-capital-a
mathematical-sans-serif-capital-b

...

mathematical-sans-serif-digit-eight
mathematical-sans-serif-digit-five
mathematical-sans-serif-digit-four

...

mathematical-sans-serif-italic-capital-a
mathematical-sans-serif-italic-capital-b

...

mathematical-sans-serif-italic-small-a
mathematical-sans-serif-italic-small-b

...

mathematical-sans-serif-small-a
mathematical-sans-serif-small-b

...

mathematical-script-capital-a

...

mathematical-script-small-a
mathematical-script-small-b

...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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