Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set custom keybinding for specific Emacs mode

Though I know how to set a global key-binding in Emacs, I find it hard to even Google out the code for a local (minor-mode specific) key-binding. For instance, I have this code in my .emacs:

;; PDFLaTeX from AucTeX (global-set-key (kbd "C-c M-p")         (lambda ()           (interactive)           (shell-command (concat "pdflatex " buffer-file-name)))) 

I don't want to set it globally. Is there a function like local-set-key?

like image 559
aL3xa Avatar asked Mar 31 '11 12:03

aL3xa


People also ask

How do I set Keybinds in Emacs?

emacs file. To interactively bind keys for all modes, type M-x global-set-key RET key cmd RET . To bind a key just in the current major mode, type M-x local-set-key RET key cmd RET . See Key Bindings in The GNU Emacs Manual .

How do I set key binding?

To reassign a keySelect the Start button, and then select Microsoft Mouse and Keyboard Center. From the displayed list of key names, select the key that you want to reassign. In the command list of the key that you want to reassign, select a command.

How do you bind a function to a key?

Hold "Ctrl-Alt" and press a key to bind the program to the "Ctrl-Alt-[Key]" hotkey.

How do I unbind a key in Emacs?

Setting the key binding to nil is indeed the way to unbind it. You just need to do that in the right map. Using C-h M-k ( describe-keymap ), from library help-fns+. el will give you a human-readable list of the bindings in a given keymap (bound to a keymap variable, such as global-map .


2 Answers

I use the following:

(add-hook 'LaTeX-mode-hook           (lambda () (local-set-key (kbd "C-0") #'run-latexmk))) 

to have a bind defined for LaTeX mode alone.

like image 181
Dror Avatar answered Sep 17 '22 21:09

Dror


To bind a key in a mode, you need to wait for the mode to be loaded before defining the key. One could require the mode, or use eval-after-load

   (eval-after-load 'latex                      '(define-key LaTeX-mode-map [(tab)] 'outline-cycle)) 

Don't forget either 'eval-after-load is not a macro, so it needs them.

like image 21
Rémi Avatar answered Sep 20 '22 21:09

Rémi