Hanfei Sun
Hanfei Sun

Reputation: 47051

Store `local-set-key` in the configuration of emacs

For example, I use the command local-set-key to set the key C-c C-n for flymake-goto-next-error in python-mode.

Instead of writing an expression and wrapping it into python-mode-hook, is there a convenient way to store this keybinding directly? Does anyone have ideas about this?

Upvotes: 1

Views: 1400

Answers (1)

artscan
artscan

Reputation: 2350

Command

(local-set-key (kbd "C-c C-n") 'flymake-goto-next-error)

works in the current buffer's local map. It is correct when local map is python-mode-map.

Convenient minimum lenght command (without using hook)

(define-key python-mode-map (kbd "C-c C-n") 'flymake-goto-next-error)

works when variable python-mode-map has been created.

Variable python-mode-map is created dynamically (in file python-mode.el) after call

(require 'python-mode)

If python-mode hasn't been loaded, command define-key can't directly use python-mode-map. Using hooks is for safety. Your emacs config should be reliable (it shouldn't depend on command's execution order if possible), and such wrapped (with hooks) commands prevent wrong situation: setting keybind in mode-map without setup mode.

Upvotes: 2

Related Questions