Emacs: How to Define Keyboard Shortcuts

来源:互联网 发布:量子计算机算法股 编辑:程序博客网 时间:2024/05/30 04:37

Emacs: How to Define Keyboard Shortcuts

标记一下该网站:http://xahlee.org/emacs/emacs.html,Xah Emacs Tutorial

By Xah Lee, 2005, …, 2012-04-05

In emacs, you can create any keyboard shortcut to any command. This page shows you how.

For example, if you want 【Ctrl+z】 to be undo, then,place this code(global-set-key (kbd "C-z") 'undo) in your emacsinit file and restart emacs.If you are experimenting, and don't want to restart emacs everytime you try to define a new shortcut, you can select the lisp code then calleval-region.

If you made some bad mistake and need to start emacs without loading your init files, you can start emacs on the command line like this:emacs -Q.

Emacs Keybinding Syntax Examples

See: Emacs Keybinding Syntax Examples.

Common Questions

What's the “Meta” key?

The Meta key is a key on Lisp Machine keyboards in the 1980s. (➲Phote of LISP Keyboards) TheMeta key doesn't exist in today's keyboards, but emacs remap it to PC keyboard'sAlt key by default. When you see “Meta” mentioned in emacs, just think of it as “Alt”.

If you read a lot here, please donate a few bucks, or buy something fromamazon.Thank you for support!

How to unset a keybinding?

To unset a keybinding, use global-unset-key. For example, you have defined a keystroke for undo, and wants to kick the habit of the hitting the default shortcut for undo:

(global-unset-key (kbd "C-_"))

How to find out the current keybinding to a key?

Call describe-keyCtrl+h k】, then type the key combination.Emacs will then show the command that key press is bound to.

To see a list of ALL current keybindings, call describe-bindingsCtrl+h b】.

How to find out the syntax for a particular key combination?

Call describe-keyCtrl+h k】, then press the key combination. Emacs will then display its syntax. For example, suppose you want to know the syntax for the key press of 【Ctrl+Alt+F8】. Call describe-key, then press 【Ctrl+Alt+F8】, then emacs will print“<C-M-f8> is undefined”. That means, you can use(kbd "<C-M-f8>") to represent that key combination in lisp code.

Note: There is a lot syntax variations, but the one printed bydescribe-key is guaranteed to work. For details of emacs's keystroke syntax variation, see:Emacs's Key Notations Explained (/r, ^M, C-m, RET, <return>, M-, meta).

How to change/add keys to a major mode?

Use a hook for the mode. A hook will load your code whenever that mode is activated. Here's a usable example:

; define some keys only when the major mode html-mode is active(add-hook 'html-mode-hook (lambda () (local-set-key (kbd "C-c w") 'bold-word) (local-set-key (kbd "C-c b") 'blue-word) (local-set-key (kbd "C-c p") 'insert-p) (local-set-key (kbd "M-4") 'tag-image) (local-set-key (kbd "M-5") 'wrap-url) ))

(info "(emacs) Hooks")

How to change minor mode's keys?

See: How to Override Minor Mode Keybinding.

How to swap Caps Lock and Control key?

You cannot do it within emacs, because these are at the OS level. See:

  • How to Swap Caps Lock, Alt, Control Keys on Windows
  • Mac OS X: How to Swap Control, Caps Lock, Option, Command Keys
  • Emacs: Why You Should Not Swap {Caps Lock, Control} Keys

Keys to Avoid

Emacs has its quirks. The following keys you should not redefine:

  • The question mark ? or any combination with it. (due to emacs technical implementation quirk)
  • The Esc key or 【Ctrl+[】. (TheEsc key is tied to 【Ctrl+[】 andMeta.Esc by itself has complicated meanings depending when it is pressed and how many times it is pressed.)
  • Ctrl+h】 (This key combo is used for emacs help system and have a special status in emacs's key system. You should not have 【Ctrl+h】 in your own shortcut, because by default that brings up help about the key sequence you've just typed.)
  • Ctrl+m】 or Enter (These are tied together. OnApple Keyboards the main Enter is labeled Return.)
  • Ctrl+i】 or Tab (These are tied together)
  • Ctrl+Shift+‹letter›】. In text terminals, it cannot distinguish shifted and unshifted versions of such combination. Works fine if you always use emacs in a GUI environment.

Good Key Choices

Emacs has some 7 thousand commands. By default, 800 of them has key shortcuts. All the common key spots are used. If you define your own keys without care, you may find that many major mode or minor mode override your keys, because they have priority.

By official emacs documentation(info "Key Binding Conventions"), the key space for users are the function keysF5 to F9, and 【Ctrl+c ‹letter›】. This is very restrictive.

The following keys are good spots for your own definitions, and does not cause any problems in practice.

Good Keys for User Defined KeysKeysCommentF5, F6, F7, F8, F9, F11, F12ExcellentF1, F2, F3, F4, F10Good if you don't use their defaults actions.Ctrl+F1】 to 【Ctrl+F12ExcellentAlt+F1】 to 【Alt+F12ExcellentShift+F1】 to 【Shift+F12ExcellentCtrl+0】 to 【Ctrl+9】,【Alt+0】 to 【Alt+9Good. By default, they call digit-argument, which is not often used.Keys on number pad, with or without a modifierVery useful, but depending on which emacs distro/OS you are using, or terminal vs GUI, binding these keys may not work. Same thing can be said for those Insert, Delete, Home, End, Page Up, Page Down keys.

Syntax Problem with Shift Key Combination

A keypress combination such as 【Meta+Shift+2】 can also beconsidered as 【Meta+@】. So, in emacs, you might be thinking that both ofthese {(kbd "M-S-2"),(kbd "M-@")} works. Actually,only the latter will work.

When writing a keybinding definition, for a key combination thatis 【Meta+Shift+‹key›】, you must use a code without the-S if possible. But for key combination that is 【Ctrl+Shift+‹key›】, you must use the-S. Examples:

GOODBADKeystroke(kbd "M-A")(kbd "M-S-a")Meta+Shift+a(kbd "M-@")(kbd "M-S-2")Meta+Shift+2(kbd "M-:")(kbd "M-S-;")Meta+Shift+;(kbd "C-S-a")(kbd "C-A")Ctrl+Shift+a

A easy way to find out the proper syntax, is to calldescribe-keyCtrl+h k】, then type the keystroke.

Note also, that keys involving 【Ctrl+Shift+‹key›】 cannot be distinguished from 【Ctrl+‹key›】 when emacs runs in a text terminal (telnet/ssh).

Example of shortcut with a punctuation key that are typed with Shift:

(global-set-key (kbd "C-:") 'beep) ; Ctrl+Shift+; or Ctrl+:(global-set-key (kbd "C-\"") 'beep) ; Ctrl+Shift+' or Ctrl+"; note: the question mark “?” cannot be used in shortcut.

Example of 2 modifier keys with a special key:

(global-set-key (kbd "M-S-<f1>") 'beep)   ; Meta+Shift+F1(global-set-key (kbd "C-S-<kp-2>") 'beep) ; Ctrl+Shift+“numberic pad 2”(global-set-key (kbd "C-M-<up>") 'beep)   ; Ctrl+Meta+↑

Order of Modifier Keys in Syntax

When there are more than one modifier keys, such as(kbd "C-M-a")Ctrl+Alt+a】, the order of the modifier in the string does not matter. It is recommended that they be alphabetical. So, writeC-M-a, not M-C-a.

Super & Hyper Keys

Emacs supports extra modifier keys called Super & Hyper.On a PC keyboard, you can set theWin key orMenu key to them, or Apple keyboard's ⌥ Opt or⌘ Cmd key. See:Emacs: How to define Super & Hyper Keys.

Practical Examples

See: Emacs Custom Keybinding to Enhance Productivity.

References and Further Readings

  • (info "(emacs) Key Bindings")
  • (info "(elisp) Keymaps")
  • Emacs's Key Notations Explained (/r, ^M, C-m, RET, <return>, M-, meta)
  • Emacs: Defining Alias to Increase Productivity
  • How to Create Your Own Keybinding in Mac OS X
  • How to Create Your Own Keybinding In Microsoft Windows
  • Emacs's Keyboard Shortcut Layout
  • Why Emacs's Keyboard Shortcuts are Painful
  • How to Avoid the Emacs Pinky Problem
  • ErgoEmacs Keybinding