Question

I'm working in Ubuntu, but since the standard way of inserting unicode characters (Ctrl+Shift+U and, after that, the unicode code) doesn't work inside emacs, I've, in my .emacs, some keystrokes for different unicode symbols which I use frequently, for example:

(global-set-key (kbd "C-c b") "☛")

and every symbol works fine, except the symbol §, which is replaced by a simple dash ("-") when I use the corresponding keystroke:

(global-set-key (kbd "C-c y") "§")

The question is, what does it make this symbol different for other symbols and how can I solve my problem?

Was it helpful?

Solution

global-set-key usually expects a function, so this should work:

(global-set-key (kbd "C-c y") (lambda () (interactive) (insert "§")))

But you're better off using the excellent insert-char function:

(global-set-key (kbd "<f2> u") 'insert-char)

It understands hex Unicode as well as text description (with completion and all). Just press TAB to see the completions.

OTHER TIPS

You can insert unicode chars in emacs by doing C-x8RET<unicode-hex>RET. So to insert § do C-x8RET00A7RET.

You can bind insert-char (the command run by C-x8RET) to a simpler key if you wish, or define a custom function and bind it to a key as follows

(global-set-key (kbd "C-c y") (lambda () (interactive) (insert "§")))

A general solution to the need to insert a number of Unicode chars quickly is to define a command for each, dedicated to inserting it, and bind that command to a simple key sequence.

That is, in effect, what @abo-abo and @Iqbal have offered you, in the form of:

(lambda () (interactive) (insert "§")))

If you have multiple such chars that you want to create such commands for, then library ucs-cmds.el can help -- in two ways:

  • It provides a macro, ucsc-make-commands, that lets you, in one fell swoop, define commands for whole ranges of Unicode chars. For example:

    • (ucsc-make-commands "^cjk") defines an insert-char command for each Chinese, Japanese, and Korean Unicode character.

    • (ucsc-make-commands "^greek [a-z]+ letter") does the same for each Greek letter.

    • (ucsc-make-commands "arabic") does the same for each Arabic character.

  • It provides a replacement command (ucsc-insert) for C-x 8 RET, which acts exactly the same as the vanilla command (insert-char), except that if you give it a negative prefix argument then it not only inserts the char you choose but it also creates an insertion command for it (i.e., the kind of command that the macro creates in bulk).

The names of the commands created by macro ucsc-make-commands and command ucsc-insert are exactly the same as the Unicode names of the chars themselves, except that they are lowercase and have hyphens (-) in place of space chars.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top