TAB 键在 emacs 的暂存缓冲区中不起作用

TAB key not working in scratch buffer of emacs

在暂存缓冲区中输入文本时按 TAB 键不会执行任何操作。我希望 TAB 键的行为与它在其他普通缓冲区中的行为完全相同(通过插入一些 X 个空格或插入一个 TAB 字符来向前移动点)。你能帮我实现吗?谢谢你。

我遇到了以下问题,但没有答案,评论中提到的 link 无效。我没有足够的分数来添加评论。

How to enable tab key in scratch buffer of emacs?

如果您可以为上述问题添加答案,请随时关闭当前问题。

TAB 在大多数编程语言模式下的行为不是 "insert a tab",而是 "make sure the code on this line is indented correctly according to the current rules"。暂存缓冲区的默认模式是 lisp-interaction-mode,并且由于您没有编写任何 Lisp 代码,因此不需要缩进,因此 TAB 什么都不做。

您可以通过多种方式更改此设置。您可以将主要模式更改为,例如,text-modefundamental-mode,对于单个会话(使用 M-x text-mode)或永久(通过将 (setq initial-major-mode 'text-mode) 放入您的 .emacs 文件).

或者您可以不理会模式,完全重新绑定 TAB 键。一种方法是

M-: (global-set-key (kbd "TAB") 'self-insert-command)

我敢肯定还有很多其他选择,具体取决于您希望暂存缓冲区发挥作用的具体方式。

如果您只想插入 TAB 字符(即 \t),那么您可以使用 quoted-insert 函数。默认情况下,它绑定到 C-q。它捕获下一个输入字符并逐字插入。所以在你的情况下是 C-q TAB.

我发布了 that comment with the now dead link, so I'll quote from the Wayback Machine copy:

Emacs isn't inserting anything!!

If you feel like I do, you probably are considering this a fault. You keep pressing the TAB key, but nothing happens.

In programming modes, such as when you're editing C or Perl or Lisp source code, the TAB key is bound to special indentation rules. That is, instead of being bound to indent-relative as in text-mode, the TAB key is pre-bound to cc-indent-line or lisp-indent-line (if editing your .emacs file), and so on. In c-mode, pressing the TAB key will move the cursor to the first indentation level, and then may not move the cursor forward after that, no matter how many times you press it.

If this behavior isn't what you want, you can do one of these things:

  • Press Ctrl-q <TAB> to insert a TAB character right now
  • Temporarily reassign the TAB key to self-insert-command while staying in the same editing mode
  • Switch to a different editing mode for this session; the TAB behavior will change with the editing mode
  • Change your .emacs file to permanently change the editing mode for the filetype you're using now

我建议您阅读整个页面,因为它很好地解释了 Emacs 如何处理 TAB 键和制表符与几乎所有其他字符不同。