Search code examples
emacselisp

emacs bind key to the insertion of another


I have paredit-forward and paredit-backward bound to > and < respectively.

This means if I want to type "something->something-else" I instead type "something-" the cursor is teleported to another part of the screen, and finish typing with "something-else".

My solution to this is to use C-. and C-, to insert them. I tried this:

(define-key key-translation-map (kbd "C-.") (kbd ">"))
(define-key key-translation-map (kbd "C-,") (kbd "<"))

The previous command results in the another paredit-forward keybind because I am creating a keybind chain like so:

C-. → > → paredit-forward

Instead of

C-. → > → the "greater than" key is inserted into whatever text box I am in.

which is what I am looking for.

Thanks.


Solution

  • Keys are bound to commands. Commands are usually interactive functions, but can also be keyboard macros (in either string or vector format). Executing a keyboard macro causes Emacs to do the things which the macro's key sequences would cause to be done.

    (kbd ">") results in the keyboard macro ">"; so you have told Emacs that when C-. is typed, it should do the things which are done when > is typed.

    Normally (in most buffers) > would be bound to self-insert-command, and therefore the keyboard macro (kbd ">") would simply insert a > character, but you've modified that binding.

    I believe you want to bind C-. to a command which inserts a > character. Such a command is:

    (lambda () (interactive) (insert ">"))