efficiencyIsBliss
efficiencyIsBliss

Reputation: 3093

Define key-bindings in emacs

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!

Upvotes: 4

Views: 8680

Answers (3)

Mirzhan Irkegulov
Mirzhan Irkegulov

Reputation: 18055

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.

Upvotes: 6

carrutherji
carrutherji

Reputation: 1135

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)

Upvotes: 12

aartist
aartist

Reputation: 3236

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.

Upvotes: 3

Related Questions