4
votes

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!

3

3 Answers

11
votes

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)
6
votes

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
votes

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.