问题
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.
回答1:
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 ">"))
回答2:
You do not need to bind a special key (e.g., C-,) to a special command (e.g., (lambda () (interactive) (insert ">"))) that inserts a given character (e.g., >).
Emacs already provides a general key, bound to a general command, that inserts any character: the key C-q.
All you ever need to do, to insert any character that corresponds to a keyboard key (and other characters as well), is to hit C-q and then hit that key.
So the answer for you is to just do this:
C-q >to insert>C-q <to insert<
来源:https://stackoverflow.com/questions/34834684/emacs-bind-key-to-the-insertion-of-another