vim中的缩进

制表符与缩进

  1. tabstop

Number of spaces that a in the file counts for.

简单来讲就是一个tab制表位占多少个空格。说白了就是一个“\t”等于多少个空格。默认值为8。而且vim作者说了,一般要设置成8的,不然很多情况下会出问题哦~

  1. softtabstop

Number of spaces that a counts for while performing editing
operations, like inserting a or using . It “feels” like
s are being inserted, while in fact a mix of spaces and s is
used. This is useful to keep the 'ts' setting at its standard value
of 8, while being able to edit like it is set to 'sts'. However,
commands like “x” still work on the actual characters.
When 'sts' is zero, this feature is off.
When 'sts' is negative, the value of 'shiftwidth' is used.
'softtabstop' is set to 0 when the 'paste' option is set.
See also |ins-expandtab|. When 'expandtab' is not set, the number of
spaces is minimized by using s.

初看没看明白……
再看,
1) 这货没有改变制表符“\t”的大小。“\t”仍然是tabstop来控制的。
2) 当你设置了此值,并且按了tab键的时候,实际输出的不是“\t”,而是按照这货的数值来显示空格数量。当tab键按下去产生空格之后,系统还会检测此光标之前输入的空格,如果空格足够多,以至于超过了一个tabstop的长度时,系统自动帮你把一个tabstop内的空格转成“\t”,超过的部分还是空格。
3) 删除。在普通模式,对softtabstop累积产生的“\t”用“x”命令删除时,整体“\t”就被删除了;
在插入模式,对“\t”的删除是先将“\t”按照tabstop的大小转换成空格,再按照softtabstop的大小删除的。
4) 如果softtabstop>tabstop呢?出来的直接就是“\t”+空格的组合。

  1. shiftwidth

Number of spaces to use for each step of (auto)indent. Used for
|'cindent'|, |»|, |«|, etc.
When zero the 'ts' value will be used. Use the |shiftwidth()|
function to get the effective shiftwidth value.

增加、减少缩进或者是使用了自动缩进时使用此设置的值。默认值为8。

  1. expandtab

In Insert mode: Use the appropriate number of spaces to insert a
. Spaces are used in indents with the '>' and '<' commands and
when 'autoindent' is on. To insert a real tab when 'expandtab' is
on, use CTRL-V . See also |:retab| and |ins-expandtab|.

将tab自动转换为设定的空格数目。默认关闭。

  1. 以上如何设置?
    vim说:
There are four main ways to use tabs in Vim:
1. Always keep 'tabstop' at 8, set 'softtabstop' and 'shiftwidth' to 4
   (or 3 or whatever you prefer) and use 'noexpandtab'.  Then Vim
   will use a mix of tabs and spaces, but typing <Tab> and <BS> will
   behave like a tab appears every 4 (or 3) characters.
2. Set 'tabstop' and 'shiftwidth' to whatever you prefer and use
   'expandtab'.  This way you will always insert spaces.  The
   formatting will never be messed up when 'tabstop' is changed.
3. Set 'tabstop' and 'shiftwidth' to whatever you prefer and use a
   |modeline| to set these values when editing the file again.  Only
   works when using Vim to edit the file.
4. Always set 'tabstop' and 'shiftwidth' to the same value, and
   'noexpandtab'.  This should then work (for initial indents only)
   for any tabstop setting that people use.  It might be nice to have
   tabs after the first non-blank inserted as spaces if you do this
   though.  Otherwise aligned comments will be wrong when 'tabstop' is
   changed.

几种自动缩进的设置

  1. autoindent (ai)

Copy indent from current line when starting a new line (typing
in Insert mode or when using the “o” or “O” command). If you do not
type anything on the new line except or CTRL-D and then type
, CTRL-O or , the indent is deleted again.

简单来讲就是产生的新行的缩进值与上一行保持一致。

  1. smartindent (si)

An indent is automatically inserted:

  • After a line ending in '{'.
  • After a line starting with a keyword from 'cinwords'.
  • Before a line starting with '}' (only with the “O” command).
    When typing '}' as the first character in a new line, that line is
    given the same indent as the matching '{'.

在autoindent的基础上更加smart了:对编程中{}的相关缩进做了较好的支持。

  1. cindent (ci)

Enables automatic C program indenting.

对C程序适配的自动缩进。并且与“cinkeys”、“cinwords”、“cinoptions”配合产生效果。

  1. indentexpr (inde)

Expression which is evaluated to obtain the proper indent for a line.
It is used when a new line is created, for the |=| operator and
in Insert mode as specified with the 'indentkeys' option.
When this option is not empty, it overrules the 'cindent' and
'smartindent' indenting. When 'lisp' is set, this option is
overridden by the Lisp indentation algorithm.

更加个性化的指定缩进的设置。还可以用Lisp的缩进设置哦~

  1. 这几种缩进设置之间的关系

There are in fact four main methods available for indentation, each one
overrides the previous if it is enabled, or non-empty for 'indentexpr':
'autoindent' uses the indent from the previous line.
'smartindent' is like 'autoindent' but also recognizes some C syntax to increase/reduce the indent where appropriate.
'cindent' Works more cleverly than the other two and is configurable to different indenting styles.
'indentexpr' The most flexible of all: Evaluates an expression to compute the indent of a line. When non-empty this method overrides the other ones. See |indent-expression|.

  1. paste
    当开启了以上的那些xxindent之后,从其他窗口粘贴到vim时会因为缩进出现格式的错乱,为了避免这种现象,可以通过开启paste选项使得各种indent失效,保持原文的缩进。完成之后再将其关闭。当paste选项开启时,当切换到插入模式时,下方的状态栏会有提示:insert (paste)

你可能感兴趣的:(vim,缩进,Indent)