Define key-bindings in emacs

时间秒杀一切 提交于 2019-12-12 11:36:53

问题


I'd like to map a command in emacs to a key-binding. I want the command Control-l to have the same effect as the command Alt-x goto-line followed by a return (since that command first needs a return to be invoked and then a line number).

I modified the init file as follows:

(define-key (M-x goto-line) '\C-l)

but that didn't work. The error was that define-key was being given more than 1 arguments.

Does anyone know how to reset key-bindings in emacs?

Thanks!


回答1:


M-g g is the default shortcut for goto-line. You might want to try that.

To redefine C-l use:

(global-set-key (kbd "C-l") 'goto-line)



回答2:


Easiest way to customize lots of keybindings is to install John Wiegley's bind-key module, which is a part of use-package Lisp package. Solution in your init.el:

(require 'bind-key)
(bind-key "C-l" 'goto-line)

Minor modes keys usually override global keys, so if you don't want such behavior, use function bind-key* instead. The package is on MELPA, if you don't know what is it, quickly learn about Emacs package management (should take you 2 minutes to set up MELPA as your repository).

The main problem with keybindings in Emacs is that minor modes keys often override your custom ones. In vanilla Emacs people workaround by creating a minor mode for your own keybindings. If you really wanna understand how Emacs keys work, read Key Bindings @ Emacs Manual and Keymaps @ Elisp Manual carefully.




回答3:


I have set as (global-set-key (kbd "C-x g") 'goto-line). You can use that or (global-set-key (kbd "C-l") 'goto-line). I would personally do not touch the C-l key from its default behavior.

If you must use M-x define-key, use (define-key global-map (kbd "C-l") 'goto-line). The 1st argument to define-key is a KEYMAP.



来源:https://stackoverflow.com/questions/7837371/define-key-bindings-in-emacs

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!