返回资讯中心
外部精选
软件工程
#开发者实时流

The Emacs Guru Guide to Key Bindings (2018)

Imagine that you hold Control and type your name into Emacs. Can you

Hacker News 最新Tomte2026/10/25 · 11 天前10 分钟阅读

以下正文同步自 Hacker News 最新,版权归原站所有,已转换为易读排版。

Imagine that you hold Control and type your name into Emacs. Can you describe what will happen?

Emacs shortcuts (known as ‘key bindings’) can seem ridiculous to beginners. Some Emacs users even argue you should change them as soon as you start using Emacs.

They are wrong. In this post, I’ll describe the logic behind the Emacs key bindings. Not only will you be closer to passing the guru test, but you might even find you like some of the defaults!

ELISP> (length global-map) 143

Emacs is a modal editor, so most key bindings are mode-specific. However, my current Emacs instance has well over a hundred global shortcuts that work everywhere.

(Keymaps are nested data structures, so this actually undercounts! For example, C-h C-h and C-h f are not counted separately.)

Even that is a drop in the bucket compared with how many commands we could define key bindings for.

ELISP> (let ((total 0)) (mapatoms (lambda (sym) (when (commandp sym) (setq total (1+ total))))) total) 8612

How can we possibly organise all these commands?

Basic commands are often given key bindings based on their name. You’ll encounter all of these important commands in the Emacs tutorial.

Mnemonics are a really effective way of memorising things. If you can remember the name of the command, you can probably remember the key binding too.

Many Emacs movement commands are laid out in a consistent pattern.

Transposing, which swaps text either side of the cursor:

The pattern here is that C-whatever commands are usually small, dumb text operations. M-whatever commands are larger, and usually operate on words.

C-M-whatever commands are slightly magical. These commands understand the code they’re looking at, and operate on whole expressions. Emacs uses the term ‘sexp’ (s-expression), but these commands usually work in any programming language!

What happens when you press C-a? Emacs can tell you. C-h k C-a will show you exactly what command is run.

If you use a command without its key binding, Emacs will helpfully remind you there’s a shortcut available.

You can even do this backwards! If Emacs has done something neat or unexpected, you might wonder what command ran. C-h l will reveal what the command was, and exactly which keys triggered it.

Why are Emacs key bindings different from conventional shortcuts? Why doesn’t C-c copy text to the clipboard, like many other programs?

Emacs uses mnemonics for its clipboard commands: you ‘kill’ and ‘yank’ text, so the key bindings are are C-k and C-y. If you really want, you can use cua-mode so C-x acts as you expect.

The problem is that Emacs commands are too versatile, too general to fit in the usual C-x, C-c, C-v. Emacs has four clipboard commands:

The generality of Emacs means that it’s hard to find a key binding for everything. Key bindings tend to be slightly longer as a result: opening a file is C-x C-f, an additional keystroke over the C-o of other programs.

With all these key bindings already defined, what bindings should you use for your personal favourite commands?

Much like IP addresses 192.168.x.x is reserved for private use, Emacs has keys that are reserved for user configuration. All the sequences C-c LETTER, such as C-c a, are reserved for your usage, as are <F5> through to <F9>.

For example, if you find yourself using imenu a lot, you might bind C-c i:

(global-set-key (kbd "C-c i") #'imenu)

You Make The Rules

This doesn’t mean that you should never modify key bindings. Emacsers create weird and wonderful ways of mapping keys all the time.

Emacs will even try to accommodate this. If you open the tutorial after changing a basic key binding, it will update accordingly!

The secret to mastering Emacs is to remember everything is self-documenting. Learn the help commands to find out which commands have default key bindings. Consider following the existing patterns when you define new key bindings or override existing ones. org-mode, for example, redefines C-M-t to transpose org elements.

Once you understand the patterns, you’ll know when to follow and when to break them. You’ll also be much closer to passing that guru test!

正文由 探How 从来源站点 RSS 同步,内容未经改写;遇到排版缺失或需要图片、视频时请以原文为准。

打开原文
去社区讨论 →