24 Indentation

Table of Contents

1 24 Indentation

Indentation refers to inserting or adjusting whitespace characters (space and/or tab characters) at the beginning of a line of text. This chapter documents indentation commands and options which are common to Text mode and related modes, as well as programming language modes. See Program Indent, for additional documentation about indenting in programming modes.

缩进是指在文本行开始处插入或调整空白字符(空格符或制表符)。本章记录了 Text 模式及其相关模式,以及编程模式下常见的缩进命令和选项。See Program Indent,了解关于编程模式中的缩进的其他文档。

The simplest way to perform indentation is the TAB key. In most major modes, this runs the command indent-for-tab-command. (In C and related modes, TAB runs the command c-indent-line-or-region, which behaves similarly).

执行缩进最简单的方式是使用 TAB 键。在大多数主模式中,这会执行 indent-for-tab-command 命令。(在 C 和相关的模式中,TAB 执行行为类似的 c-indent-line-or-region 命令)。

  • TAB

    Insert whitespace, or indent the current line, in a mode-appropriate way (indent-for-tab-command). If the region is active, indent all the lines within it.


The exact behavior of TAB depends on the major mode. In Text mode and related major modes, TAB normally inserts some combination of space and tab characters to advance point to the next tab stop (see Tab Stops). For this purpose, the position of the first non-whitespace character on the preceding line is treated as an additional tab stop, so you can use TAB to “align” point with the preceding line. If the region is active (see Using Region), TAB acts specially: it indents each line in the region so that its first non-whitespace character is aligned with the preceding line.

TAB 的准备行为依赖于主模式。Text 模式和相关主模式中,TAB 通常插入一些空格符合制表符的组合来将 point 前移到下一个 tab stop(see Tab Stop)。为此,前一行首个非空白字符的位置作为额外的 tab stop,所以可以使用 TAB 来进行对其。如果激活区域(see Using Region),TAB 行为特别:缩进区域中的每一行,这样它的首个非空白字符都会和前一行对齐。

In programming modes, TAB indents the current line of code in a way that makes sense given the code in the preceding lines. If the region is active, all the lines in the region are indented this way. If point was initially within the current line’s indentation, it is repositioned to the first non-whitespace character on the line.

编程模式中,TAB 结合之前的代码行以合理的方式缩进当前行。如果激活区域,区域中的所有行都以这种方式缩进。如果 point 所有行已经完成缩进,就会将它定位到该行首个非空白字符处。

If you just want to insert a tab character in the buffer, type C-q TAB (see Inserting Text).

如果想在 buffer 中插入制表符,输入 C-q TAB(see Inserting Text).

1.1 24.1 Indentation Commands

Apart from the TAB (indent-for-tab-command) command, Emacs provides a variety of commands to perform indentation in other ways.

除了 TAB(indent-for-tab-command)命令,Emasc 还以其他方式提供的各种执行缩进的命令。

  • C-M-o

    Split the current line at point (split-line). The text on the line after point becomes a new line, indented to the same column where point is located. This command first moves point forward over any spaces and tabs. Afterward, point is positioned before the inserted newline.

    在当前 point 分割当前行(split-line)。行上 point 之后的文本变为新行,按照当前 point 所在的列进行缩进。该命令首先将 point 向前移过任何空格符或指标符。之后,point 放在插入新行之前。

  • M-m

    Move (forward or back) to the first non-whitespace character on the current line (back-to-indentation). If there are no non-whitespace characters on the line, move to the end of the line.


  • M-i

    Indent whitespace at point, up to the next tab stop (tab-to-tab-stop). See Tab Stops. 当缩进 point 处的空白符到下个 tab stop(tab-to-tab-stop)。See Tab Stops。

  • M-x indent-relative

    Insert whitespace at point, until point is aligned with the first non-whitespace character on the previous line (actually, the last non-blank line). If point is already farther right than that, run tab-to-tab-stop instead—unless called with a numeric argument, in which case do nothing.

    在 point 处插入空白符,知道 poit 和前一行(实际上是上一个非空白行)的第一个非空白符对齐。如果 point 已经超过了顶起位置,执行 tab-to-tab-stop—带参数执行则什么都不做。

  • M-^

    Merge the previous and the current line (delete-indentation). This “joins” the two lines cleanly, by replacing any indentation at the front of the current line, together with the line boundary, with a single space.


    As a special case (useful for Lisp code), the single space is omitted if the characters to be joined are consecutive opening and closing parentheses, or if the junction follows another newline.

    有一个特例(对 Lisp 代码有用),如果连接的字符是连续的开关括号的话,或者连接后跟一个新行,将忽略中间的单个空格符。

    If there is a fill prefix, M-^ deletes the fill prefix if it appears after the newline that is deleted. See Fill Prefix.

    如果被删除新行后有一个 fill 前缀,M-^将会删除它。See Fill Prefix。

  • C-M-\

    Indent all the lines in the region, as though you had typed TAB at the beginning of each line (indent-region).

    缩进区域中的所有行,如何在每行开始处都按下 TAB。(indent-region)。

    If a numeric argument is supplied, indent every line in the region to that column number.


  • C-x TAB

    This command is used to change the indentation of all lines that begin in the region, moving the affected lines as a “rigid” unit.


    If called with no argument, the command activates a transient mode for adjusting the indentation of the affected lines interactively. While this transient mode is active, typing LEFT or RIGHT indents leftward and rightward, respectively, by one space. You can also type S-LEFT or S-RIGHT to indent leftward or rightward to the next tab stop (see Tab Stops). Typing any other key disables the transient mode, and resumes normal editing.

    不加参数调用,命令激活一个 transient mode 来交互调整受影响的行的缩进。当 transient mode 激活后,使用左右方向键向左或向右分别使用一个空格符缩进到下个 tab Stop(see Tab stop).输入其他键关闭 transient mode,恢复正常编辑。

    If called with a prefix argument n, this command indents the lines forward by n spaces (without enabling the transient mode). Negative values of n indent backward, so you can remove all indentation from the lines in the region using a large negative argument, like this:

    加参数调用,该命令将会向前缩进 n 个空格符(不启用 transient mode)。负值向后缩进,所以可以使用大的负值来删除区域中所有行的缩进。

    C-u -999 C-x TAB

1.2 24.2 Tab Stops

Emacs defines certain column numbers to be tab stops. These are used as stopping points by TAB when inserting whitespace in Text mode and related modes (see Indentation), and by commands like M-i (see Indentation Commands). The variable tab-stop-list controls these positions. The default value is nil, which means a tab stop every 8 columns. The value can also be a list of zero-based column numbers (in increasing order) at which to place tab stops. Emacs extends the list forever by repeating the difference between the last and next-to-last elements.

Emacs 将某些列数定义为 tab stop。当使用命令,如 M-i(See Indentation)在 Text mode 或相关的 modes 插入空白符时候它们被用作停止点。变量 tab-stop-list 控制了这些位置。默认值是 nil,意味着每 8 列为一个 tab stop。该值还可是以 0 开始的递增数字列表,这些数字说明放置 tab stop 的位置。Emacs 通过重复最后两个元素之间的差值来扩展该列表。

Instead of customizing the variable tab-stop-list directly, a convenient way to view and set tab stops is via the command M-x edit-tab-stops. This switches to a buffer containing a description of the tab stop settings, which looks like this:

比直接定制变量 tab-stop-list 更方便的方法是通过 M-x edit-tab-stops 命令来设置 tab stop。这将会切换到设置中,看起来像下面这样

        :       :       :       :       :       :
0         1         2         3         4

To install changes, type C-c C-c

The first line contains a colon at each tab stop. The numbers on the next two lines are present just to indicate where the colons are. If the value of tab-stop-list is nil, as it is by default, no colons are displayed initially.

第一行每个 tab stop 位置有个冒号。下面两个的数字只是为了表明冒号所处的位置。如果 tab-stop-list 值为 nil,也就是默认值,初始显示冒号。

You can edit this buffer to specify different tab stops by placing colons on the desired columns. The buffer uses Overwrite mode (see Minor Modes). Remember that Emacs will extend the list of tab stops forever by repeating the difference between the last two explicit stops that you place. When you are done, type C-c C-c to make the new tab stops take effect. Normally, the new tab stop settings apply to all buffers. However, if you have made the tab-stop-list variable local to the buffer where you called M-x edit-tab-stops (see Locals), then the new tab stop settings apply only to that buffer. To save the tab stop settings for future Emacs sessions, use the Customize interface to save the value of tab-stop-list (see Easy Customization).

在 buffer 中将冒号放在所需要的列上就可以指定不同的 tab stop。该 buffer 使用 Overwrite mode(see Minor Modes)。记住,Emacs 将会通过重复设置的最后两个 stop 之间的差值来永远扩展该列表。设置完后,使用 C-c C-c 来是新的 tab stops 生效。通常新的 tab stop 会应用到所有的 buffer。但是,如果调用 M-x edit-tab-stop(see Locals)将 tab-stop-list 设置为 buffer 局部变量,新的设置就只对该 buffer 有效。如果要为将来的 Emacs 会话保存 tab stop 设置,使用定制接口保存 tab-stop-list 的值。(see Easy Customization)。

Note that the tab stops discussed in this section have nothing to do with how tab characters are displayed in the buffer. Tab characters are always displayed as empty spaces extending to the next display tab stop. See Text Display.

1.3 24.3 Tabs vs. Spaces

Normally, indentation commands insert (or remove) an optimal mix of space characters and tab characters to align to the desired column. Tab characters are displayed as a stretch of empty space extending to the next display tab stop. By default, there is one display tab stop every tab-width columns (the default is 8). See Text Display.

通常,缩进命令插入(或删除)最优数量的空格符和制表符来调整所需要的列。制表符从当前位置到下一个 tab stop 显示为一段空白。默认每 tab-width(默认是 8)列显示为一个 tab stop。See Text Display。

If you prefer, all indentation can be made from spaces only. To request this, set the buffer-local variable indent-tabs-mode to nil. See Locals, for information about setting buffer-local variables. Note, however, that C-q TAB always inserts a tab character, regardless of the value of indent-tabs-mode.

如果愿意,所有缩进都可以由空格符构成。这需要将 buffer-local 变量 indent-tabs-mode 设置为 nil。See Locals,获取设置 buffer-local 变量信息。但请注意,不管 indent-tabs-mode 为何值,C-q TAB 总是插入一个制表符。

One reason to set indent-tabs-mode to nil is that not all editors display tab characters in the same way. Emacs users, too, may have different customized values of tab-width. By using spaces only, you can make sure that your file always looks the same. If you only care about how it looks within Emacs, another way to tackle this problem is to set the tab-width variable in a file-local variable (see File Variables).

将 indent-tabs-mode 设置为 nil 的一个理由是并非所有的编辑器都以相同的方式显示制表符。Emasc 用户也可能定制了不一样的 tab-width 值。如果只使用空格符,就可以确保你的文件看起来总是一样的。如果只关心它在 Emacs 显示成什么样,另一种解决方法是将 tab-width 设置为 file-local 变量。

There are also commands to convert tabs to spaces or vice versa, always preserving the columns of all non-whitespace text. M-x tabify scans the region for sequences of spaces, and converts sequences of at least two spaces to tabs if that can be done without changing indentation. M-x untabify changes all tabs in the region to appropriate numbers of spaces.

还有其他命令可以在进行制表符和空格符的转换,总保留所有非空白文本的列。M-x tabify 扫描区域中所有的空格符序列,在不改变缩进的情况下将至少两个空格符转化为制表符。M-x untabify 将区域中所有的制表符转化为合适的空格符。

1.4 24.4 Convenience Features for Indentation

The variable tab-always-indent tweaks the behavior of the TAB (indent-for-tab-command) command. The default value, t, gives the behavior described in Indentation. If you change the value to the symbol complete, then TAB first tries to indent the current line, and if the line was already indented, it tries to complete the text at point (see Symbol Completion). If the value is nil, then TAB indents the current line only if point is at the left margin or in the line’s indentation; otherwise, it inserts a tab character.

变量 tab-always-indent 调整 TAB 命令(indent-for-tab-command)的行为。默认只是 t,行为和 Indentation 中描述的一样。如果将该值设置为符号 complete,TAB 命令将首先尝试缩进当前行,如果该行已经缩进,将尝试补全光标处的文本。如果该值为 nil,TAB 只有在 point 在该行左边界或该行的缩进中才执行缩进;否则,插入一个制表符。

Electric Indent mode is a global minor mode that automatically indents the line after every RET you type. This mode is enabled by default. To toggle this minor mode, type M-x electric-indent-mode. To toggle the mode in a single buffer, use M-x electric-indent-local-mode.

Electric Indent mode 是全局辅助模式,可以在每次输入 RET 之后进行缩进。默认启动该模式。输入 M-x electric-indent-mode 切换该模式。在单独的 buffer 中切换该模式请使用 M-x electric-indent-local-mode。

Author: lsl

Created: 2016-08-07 Sun 19:12