1

I would like to be able to keybind « and » to easy key bindings. At the moment in Emacs the only way to write these latin quotation marks is this:

C-x 8 < and C-x 8 >

I tried the following, but apparently the quotation mark is interpreted as a macro:

(global-set-key (kbd "C-M-b") "«")

I also tried the following:

(define-key org-mode-map (kbd "C-M v") (lambda () (insert "«")))

...unsuccessfully.

8
  • 1
    Just a warning: redefining a key in the global key map should be done with some care. You may think that C-M-b (which is by default bound to backward-sexp) is not useful (e.g. you may not be a programmer now), but if you do become a programmer later, it will cause problems. It's better to define it in a mode specific key map as you do in your second method: at least that limits the key definition to that mode only.
    – NickD
    Commented Oct 14, 2022 at 14:36
  • 1
    You might also look at the conventions that Emacs has in place, to make keys available to the user that will not conflict with those of Emacs itself or any third-party packages (assuming that they follow the conventions too of course).
    – NickD
    Commented Oct 14, 2022 at 14:37
  • 3
    For simple unicode chars like «, you may be better off leaving Emacs alone and using input facilities provided by your desktop environment. E.g. I use Gnome and I have defined the right-ALT key to be the Compose key, I can enter the opening guillemet with Compose <<. This works in any program, not just in Emacs and allows you to leave your keymaps in blessed peace.
    – NickD
    Commented Oct 14, 2022 at 15:57
  • 3
    Does this answer your question? Quickly inserting a single Greek letter Commented Oct 15, 2022 at 18:30
  • @NickD I didn't understand what you meant with leaving Emacs alone? Not using Emacs?
    – Adelita
    Commented Oct 18, 2022 at 19:46

2 Answers 2

6

I tried the following, but apparently the quotation mark is interpreted as a macro: [...]

You’re almost there, « and » are non-ASCII characters with codes above 127, so you have to use vectors instead:

(global-set-key (kbd "C-M-b") (kbd "«"))
;; likewise for »
3
  • 3
    +1. Good answer. It might help to point to some doc where non-ASCII > 127 is described, and where there's an explanation of why it's problematic here.
    – Drew
    Commented Oct 14, 2022 at 17:13
  • @Drew The docs for insert-char talks about restrictions on non-ASCII characters a little bit.
    – g-gundam
    Commented Nov 14, 2022 at 6:13
  • @g-gundam: Maybe so. It would still be good to point to that or similar info in the answer. Comments can be deleted at any time - Q&A need to stand on their own.
    – Drew
    Commented Nov 14, 2022 at 15:31
0

The transcript below defines a keyboard macro, names it and insert it in to the current buffer. It subsequently it binds the keys C-M-b to the newly defined command.

The above procedure would have created

(defalias 'my-quote
   (kmacro "C-x 8 <"))

for you.

(I am running emacs from git repo. So, it uses kmacro. In your case, it may or may not be different. The procedure is the same)

Now my-quote is like any other command. And bind it like you would any other command.

(global-set-key (kbd "C-M-b") 'my-quote)
 C-x (           ;; kmacro-start-macro
 C-x 8 <         ;; self-insert-command
 C-x )           ;; kmacro-end-macro
 M-x             ;; execute-extended-command
 n           ;; self-insert-command
 a           ;; self-insert-command
 m           ;; self-insert-command
 e           ;; self-insert-command
 -           ;; self-insert-command
 l           ;; self-insert-command
 a           ;; self-insert-command
 s           ;; self-insert-command
 t           ;; self-insert-command
 -           ;; self-insert-command
 k           ;; self-insert-command
 b           ;; self-insert-command
 d           ;; self-insert-command
 <tab>           ;; minibuffer-complete
 <return>        ;; minibuffer-complete-and-exit
 m           ;; self-insert-command
 y           ;; self-insert-command
 -           ;; self-insert-command
 q           ;; self-insert-command
 u           ;; self-insert-command
 e           ;; self-insert-command
 <backspace>         ;; delete-backward-char
 o           ;; self-insert-command
 t           ;; self-insert-command
 e           ;; self-insert-command
 <return>        ;; exit-minibuffer
 M-x M-x         ;; execute-extended-command
 i           ;; self-insert-command
 n           ;; self-insert-command
 s           ;; self-insert-command
 e           ;; self-insert-command
 r           ;; self-insert-command
 t           ;; self-insert-command
 -           ;; self-insert-command
 k           ;; self-insert-command
 b           ;; self-insert-command
 d           ;; self-insert-command
 -           ;; self-insert-command
 m           ;; self-insert-command
 a           ;; self-insert-command
 c           ;; self-insert-command
 r           ;; self-insert-command
 o           ;; self-insert-command
 <return>        ;; minibuffer-complete-and-exit
 m           ;; self-insert-command
 y           ;; self-insert-command
 -           ;; self-insert-command
 q           ;; self-insert-command
 u           ;; self-insert-command
 o           ;; self-insert-command
 t           ;; self-insert-command
 e           ;; self-insert-command
 <return>        ;; minibuffer-complete-and-exit
 M-x             ;; execute-extended-command
 g           ;; self-insert-command
 l           ;; self-insert-command
 o           ;; self-insert-command
 b           ;; self-insert-command
 a           ;; self-insert-command
 l           ;; self-insert-command
 -           ;; self-insert-command
 s           ;; self-insert-command
 e           ;; self-insert-command
 t           ;; self-insert-command
 -           ;; self-insert-command
 k           ;; self-insert-command
 e           ;; self-insert-command
 y           ;; self-insert-command
 <return>        ;; minibuffer-complete-and-exit
 C-M-b m         ;; self-insert-command
 y           ;; self-insert-command
 -           ;; self-insert-command
 q           ;; self-insert-command
 u           ;; self-insert-command
 o           ;; self-insert-command
 t           ;; self-insert-command
 e           ;; self-insert-command
 <return>        ;; minibuffer-complete-and-exit
 <escape> <escape> > ;; end-of-buffer
 C-M-b           ;; my-quote
 ;; self-insert-command
 C-h l ;; view-lossage

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.