emacs - 在文本模式下的 Emacs 中设置 4 个空格缩进

标签 emacs indentation

当在主要模式 text-mode 的缓冲区中按 TAB 时,我未能成功让 Emacs 从 8 个空格选项卡切换到 4 个空格选项卡。我已将以下内容添加到我的 .emacs 中:

(setq-default indent-tabs-mode nil)
(setq-default tab-width 4)

;;; And I have tried
(setq indent-tabs-mode nil)
(setq tab-width 4)

无论我如何更改 .emacs 文件(或缓冲区的局部变量),TAB 按钮始终执行相同的操作。

  1. 如果上面没有文字,则缩进 8 个空格
  2. 如果上一行有文本,则缩进到第二个单词的开头

尽管我很喜欢 Emacs,但这却变得很烦人。有没有办法让 Emacs 在上一行没有文本时至少缩进 4 个空格?

最佳答案

简短回答:

关键点是告诉 emacs 在缩进时插入你想要的任何内容,这是通过更改 indent-line-function 来完成的。改成插入一个制表符再把制表符改成4个空格比改成插入4个空格要容易一些。以下配置将解决您的问题:

(setq-default indent-tabs-mode nil)
(setq-default tab-width 4)
(setq indent-line-function 'insert-tab)

说明:

来自Indentation Controlled by Major Mode @ emacs manual :

An important function of each major mode is to customize the key to indent properly for the language being edited.

[...]

The indent-line-function variable is the function to be used by (and various commands, like when calling indent-region) to indent the current line. The command indent-according-to-mode does no more than call this function.

[...]

The default value is indent-relative for many modes.

来自 indent-relative @ emacs 手册:

Indent-relative Space out to under next indent point in previous nonblank line.

[...]

If the previous nonblank line has no indent points beyond the column point starts at, `tab-to-tab-stop' is done instead.

只需将 indent-line-function 的值更改为 insert-tab 函数,并将制表符插入配置为 4 个空格即可。

关于emacs - 在文本模式下的 Emacs 中设置 4 个空格缩进,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69934/

相关文章:

emacs - Elisp:保存一个位置,在它之前插入文本,然后返回到相同的位置

Vim - 像 Emacs 一样缩进

indentation - 如何使用 flex/bison 进行类似 python 的缩进

javascript - WebStorm自动缩进与缩进设置不一致

ruby - block 缩进正则表达式

javascript - Prettier vscode json文件缩进间距问题

emacs - 不要截断 emacs 包列表中的包名称。

regex - Emacs 通过精确的正则表达式匹配而不是部分搜索

php - 将缩进列表解析为 bool 树

emacs - VimGolf 有 Emacs 版本吗?