博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
emacs 快捷键设置 基础知识篇
阅读量:6292 次
发布时间:2019-06-22

本文共 10252 字,大约阅读时间需要 34 分钟。

原文地址:

中文简单参考:

官方命令参考地址:

Emacs: How to Define Keyboard Shortcuts

Xah Lee, 2005, …, 2009, 2011-02

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

The act of creating a keyboard shortcut is called keybinding. For example, if you want 【Ctrl+z】 to be undo, then, place this code (global-set-key (kbd "C-z") 'undo) in your emacs init 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 type 【Alt+x eval-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.

Examples

Here are sample code you need to place in your emacs init file for defining various key press combinations.

The Meta key is a key on some keyboards in the 1980s. (for photos, see:) The Meta key doesn't exist in today's keyboards, but emacs remap it to PC keyboard's Alt key by default. When you see Meta mentioned in emacs, just think of it as Alt.

In the following, the “backward-char” is a example command. Replace it with the command name you want.

Single Modifier Key

Example of shortcuts with a single modifier key:

(global-set-key (kbd "M-a") 'backward-char) ; Meta+a(global-set-key (kbd "C-a") 'backward-char) ; Ctrl+a

Function keys and Special keys

Example of shortcuts with a special key:

(global-set-key (kbd "
") 'backward-char) ; F2 key(global-set-key (kbd "
") 'backward-char) ; the “2” key on the number keypad(global-set-key (kbd "
") 'backward-char) ; the Ins key(global-set-key (kbd "
") 'backward-char) ; the Del key(global-set-key (kbd "
") 'backward-char)(global-set-key (kbd "
") 'backward-char)(global-set-key (kbd "
") 'backward-char) ; page down key(global-set-key (kbd "
") 'backward-char) ; page up key (global-set-key (kbd "
") 'backward-char) ; ←(global-set-key (kbd "
") 'backward-char) ; →(global-set-key (kbd "
") 'backward-char) ; ↑(global-set-key (kbd "
") 'backward-char) ; ↓(global-set-key (kbd "RET") 'backward-char) ; the Enter/Return key(global-set-key (kbd "SPC") 'backward-char) ; the Space bar key

Modifier + Special Key

Example of shortcuts with a modifier key and a special key:

(global-set-key (kbd "M-
") 'backward-char) ; Meta+F2(global-set-key (kbd "C-
") 'backward-char) ; Ctrl+F2(global-set-key (kbd "S-
") 'backward-char) ; Shift+F2(global-set-key (kbd "M-
") 'backward-char) ; Meta+↑(global-set-key (kbd "C-
") 'backward-char) ; Ctrl+↑(global-set-key (kbd "S-
") 'backward-char) ; Shift+↑

Two Modifier Keys

Example of shortcuts with 2 modifier keys pressed simultaneously, plus a letter key:

(global-set-key (kbd "M-A") 'backward-char) ; Meta+Shift+a(global-set-key (kbd "C-A") 'backward-char) ; Ctrl+Shift+a(global-set-key (kbd "C-M-a") 'backward-char) ; Ctrl+Meta+a

Example of 2 modifier keys with a digit key:

(global-set-key (kbd "M-@") 'backward-char)       ; Meta+Shift+2 or Meta+@(global-set-key (kbd "C-@") 'backward-char)       ; Ctrl+Shift+2 or Ctrl+@(global-set-key (kbd "C-M-2") 'backward-char)     ; Ctrl+Meta+2(global-set-key (kbd "C-S-
") 'backward-char); Ctrl+Shift+“numberic pad 2”

Three Modifier Keys

Example of 3 modifier keys:

(global-set-key (kbd "C-M-S-a") 'backward-char)   ; Ctrl+Meta+Shift+a(global-set-key (kbd "C-M-!") 'backward-char)     ; Ctrl+Meta+Shift+1 or Ctrl+Meta+!(global-set-key (kbd "C-M-\"") 'backward-char)    ; Ctrl+Meta+Shift+' or Ctrl+Meta+"(global-set-key (kbd "C-M-S-
") 'backward-char); Ctrl+Meta+Shift+↑

Key Sequence

Example of shortcuts with a sequence of keystrokes:

(global-set-key (kbd "C-c a") 'backward-char)  ; Ctrl+c a(global-set-key (kbd "C-c SPC") 'backward-char)  ; Ctrl+c Space(global-set-key (kbd "C-c 
") 'backward-char) ; Ctrl+c f2(global-set-key (kbd "C-c
") 'backward-char) ; Ctrl+c ↑(global-set-key (kbd "C-c C-c
") 'backward-char); Ctrl+c Ctrl+c ↑

No Modifiers

A shortcut can be created without any modifier keys.

(global-set-key (kbd "2") 'backward-char)(global-set-key (kbd "a") 'backward-char)(global-set-key (kbd "é") 'backward-char)(global-set-key (kbd "α") 'backward-char)(global-set-key (kbd "π") 'backward-char)(global-set-key (kbd "(") 'backward-char)(global-set-key (kbd "你") 'backward-char)

Keys To Avoid

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

  • Keys involving the question mark symbol “?”. (due to emacs technical implementation quirk)
  • The Esc key or 【Ctrl+[】. (The Esc key is tied to 【Ctrl+[】 and MetaEsc 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. On  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  , the key space for users are the function keys F5 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 Keys
Keys Comment
F5F6F7F8F9F11,F12 Excellent
F1F2F3F4F10 Good if you don't use their .
Ctrl+F1】 to 【Ctrl+F12 Excellent
Alt+F1】 to 【Alt+F12 Excellent
Shift+F1】 to 【Shift+F12 Excellent
Ctrl+0】 to 【Ctrl+9】, 【Alt+0】 to 【Alt+9 Good. By default, they call “digit-argument”, which is not often used.
Keys on number pad, with or without a modifier Very useful, but depending on which emacs distribution you are using, and on what operating system you are running, 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 be considered as 【Meta+@】. So, in emacs, you might be thinking that both of these code: (kbd "M-S-2") and (kbd "M-@") will work. Actually, only the latter will work.

When writing a keybinding definition, for a key combination that is 【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:

GOOD BAD Keystroke
(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 call “describe-key” 【Ctrl+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-:") 'backward-char) ; Ctrl+Shift+; or Ctrl+:(global-set-key (kbd "C-\"") 'backward-char) ; 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-
") 'backward-char) ; Meta+Shift+F1(global-set-key (kbd "C-S-
") 'backward-char) ; Ctrl+Shift+“numberic pad 2”(global-set-key (kbd "C-M-
") 'backward-char) ; 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, type “C-M-a”, not “M-C-a”.

Super & Hyper Keys

Emacs supports extra modifier keys called Super & Hyper. On a PC keyboard, you can set the Win key or Menu key to them, or Apple keyboard's ⌥ Opt or⌘ Cmd key. See: .

Common Questions

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?

Type 【Ctrl+h k】 (describe-key), then type the key combination. Emacs will then show the function that key press is bound to.

To see a list of ALL current keybindings, type 【Ctrl+h b】 (describe-bindings).

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

Call “describe-key” 【Ctrl+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 by “describe-key” is guaranteed to work. For details of emacs's keystroke syntax variation, see:.

How to have a keyboard shortcut set only when a particular mode is active?

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

How to change major mode or minor mode's keys?

See: .

How to swap Caps Lock and Control key?

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

Practical Examples

See: .

References and Further Readings

转载地址:http://zbcta.baihongyu.com/

你可能感兴趣的文章
CentOS7 yum 安装git
查看>>
启动日志中频繁出现以下信息
查看>>
httpd – 对Apache的DFOREGROUND感到困惑
查看>>
分布式锁的一点理解
查看>>
idea的maven项目,install下载重复下载本地库中已有的jar包,而且下载后jar包都是lastupdated问题...
查看>>
2019测试指南-web应用程序安全测试(二)指纹Web服务器
查看>>
树莓派3链接wifi
查看>>
js面向对象编程
查看>>
Ruby中类 模块 单例方法 总结
查看>>
jQuery的validate插件
查看>>
5-4 8 管道符 作业控制 shell变量 环境变量配置
查看>>
Enumberable
查看>>
开发者论坛一周精粹(第五十四期) 求购备案服务号1枚!
查看>>
validate表单验证及自定义方法
查看>>
javascript 中出现missing ) after argument list的错误
查看>>
使用Swagger2构建强大的RESTful API文档(2)(二十三)
查看>>
Docker容器启动报WARNING: IPv4 forwarding is disabled. Networking will not work
查看>>
(转)第三方支付参与者
查看>>
程序员修炼之道读后感2
查看>>
DWR实现服务器向客户端推送消息
查看>>