Even More Batteries Included With Emacs

Even More Batteries Included With Emacs

Emacs 进阶:更多“内置”好物

Emacs features have a discoverability problem, and we’re chipping away at it one demo at a time. The years since I wrote the last one of these have yielded more surprising and useful finds, so it’s time again for a “batteries included” report. Emacs 的功能存在“可发现性”问题,我们正通过一个个演示来逐步解决它。距离我上一篇相关文章已经过去几年了,期间我又发现了一些令人惊喜且实用的功能,所以现在是时候再出一份“内置好物”报告了。

Note: This is the third in a series of articles highlighting useful but lesser-known features included in Emacs. Parts 1 & 2: Batteries included with Emacs, More batteries included with emacs. 注:这是系列文章的第三篇,旨在介绍 Emacs 中实用但鲜为人知的功能。第一、二部分:《Emacs 内置好物》及《Emacs 更多内置好物》。

“Lesser-known” is a subjective judgment. Roughly, it means that at the time of writing, I have seen these features mentioned fewer than five times – and often never – in the past two decades of dipping in and out of online Emacs discourse. Some of the features covered in past entries are well known and often recommended today. I claim no credit. “鲜为人知”是一个主观判断。粗略地说,这意味着在撰写本文时,在我过去二十年断断续续参与的 Emacs 在线讨论中,这些功能被提及的次数不到五次,甚至从未被提及。过去文章中提到的一些功能如今已广为人知并常被推荐,我并不居功。

If you’re a new Emacs user, don’t start here. This is not a getting-started guide. You will be better served by grokking basic Emacs concepts and sticking to the most widely recommended packages. Once you’ve experienced the Emacs equivalents of thoughts like “Why didn’t anyone think to put wheels on luggage until 1990?”, this series might be more helpful. My rule of thumb is that if you aren’t yet aware of undo-in-region, there is much low hanging fruit for the picking, and you can come back to this article after that supply has run out! 如果你是 Emacs 新手,请不要从这里开始。这不是入门指南。你最好先理解 Emacs 的基本概念,并坚持使用最广泛推荐的插件。当你体验过类似“为什么直到 1990 年才有人想到给行李箱装上轮子?”这种 Emacs 版的感叹后,本系列文章对你才更有帮助。我的经验法则是:如果你还不知道 undo-in-region,那么还有很多唾手可得的技巧等着你去发掘,等那些用完了再回来看这篇文章吧!

Veteran Emacs users tend to use some relatively niche Emacs features, but in my experience it’s always a different subset for each user. So if you’ve been around the block a few times, I promise there will still be surprises below for you as well! 资深 Emacs 用户往往会使用一些相对小众的功能,但根据我的经验,每个用户使用的子集各不相同。所以,如果你已经是老手了,我保证下面依然会有让你惊喜的内容!

Same rules as before: 规则与往常一样:

  • No packages, stock Emacs only. (不使用第三方插件,仅限原生 Emacs)
  • No steep learning curves. Learn each feature in under five minutes or bust. (没有陡峭的学习曲线。每个功能必须在五分钟内学会,否则就放弃)
  • No gimmicks. No doctor, tetris, snake, dunnet, zone, butterfly… yes, we know about dissociated-press. Let’s move on. (没有噱头。不谈 doctor、tetris、snake、dunnet、zone、butterfly……是的,我们知道 dissociated-press,跳过吧)
  • Just the deltas. No commonly mentioned packages like Flymake, doc-view, outline-minor-mode, gnus or eww. Nothing that Emacs brings up automatically or a nonspecific Google search gets you. (只谈差异化内容。不涉及 Flymake、doc-view、outline-minor-mode、gnus 或 eww 等常见插件。不谈那些 Emacs 自动弹出或通过模糊 Google 搜索就能找到的内容)
  • Assume a modern Emacs, 28.1+. (假设使用现代 Emacs 28.1+ 版本)

Also, if you’re new to Emacs and still reading: 另外,如果你是 Emacs 新手且还在阅读:

Emacs jargonModern parlance
M-xAlt + x
C-xCtrl + x
FrameEmacs window
Windowsplit/pane
BufferContiguous chunk of text/data
PointCursor position in buffer
Active RegionText selection
RegionText selection (not highlighted)
FaceFont, color and display properties

I’m Sorry. Okay? Let’s go: 抱歉,好吗?我们开始吧:

Dictionary on hover (M-x dictionary-tooltip-mode)

悬停查词 (M-x dictionary-tooltip-mode)

Turn on dictionary-tooltip-mode to see word meanings in tooltips when you hover over them: Of course, tooltip-mode will need to be enabled as well, but that’s the default. If you have local dictionaries set up, it will try those first. Note that Emacs’ dictionary can look up contemporary jargon and lingo too, usually via Wiktionary. 开启 dictionary-tooltip-mode,当你将鼠标悬停在单词上时,即可在工具提示中查看词义。当然,前提是必须启用 tooltip-mode(这是默认开启的)。如果你配置了本地词典,它会优先使用本地词典。请注意,Emacs 的词典通常可以通过 Wiktionary 查询当代术语和俚语。

find-file and dired with wildcards

使用通配符进行 find-file 和 dired 操作

A surprisingly little known utility of two of the most used Emacs commands: you can use wildcards when using both find-file and dired interactively. When finding files with find-file (C-x C-f), open multiple files at once with a wildcard like *foo*.txt. When opening a directory with Dired, produce a custom listing of specific files by specifying a filename wildcard. 这是两个最常用的 Emacs 命令中一个鲜为人知的实用功能:在交互式使用 find-filedired 时,你可以使用通配符。使用 find-file (C-x C-f) 查找文件时,可以通过类似 *foo*.txt 的通配符一次打开多个文件。在用 Dired 打开目录时,通过指定文件名通配符,可以生成特定文件的自定义列表。

List all URIs with M-x ffap-menu

使用 M-x ffap-menu 列出所有 URI

You might be familiar with Emacs’ “find-file-at-point” feature, M-x ffap, that checks if the cursor is on a valid file path and offers to open it. This is accompanied by ffap-menu, a less well known but equally handy command. ffap-menu scans the whole buffer for anything that looks like a file path or URL and presents you with all of them. 你可能熟悉 Emacs 的“find-file-at-point”功能 M-x ffap,它会检查光标是否位于有效的文件路径上并提示打开。与之配套的还有一个不太出名但同样好用的命令 ffap-menuffap-menu 会扫描整个缓冲区,找出所有看起来像文件路径或 URL 的内容,并将它们全部呈现给你。

Since it offers a completing-read interface, this opens up a small universe of possibilities: you can export the list of (possibly filtered) completions into a buffer, copy or open all or any subset of them, or otherwise act on them right away with Embark. 由于它提供了 completing-read 接口,这开启了无限可能:你可以将(经过筛选的)补全列表导出到缓冲区,复制或打开其中全部或部分内容,或者使用 Embark 立即对它们进行操作。