vim中的缩进

制表符与缩进

  1. tabstop

Number of spaces that a <Tab> in the file counts for.express

简单来说就是一个tab制表位占多少个空格。说白了就是一个“\t”等于多少个空格。默认值为8。并且vim做者说了,通常要设置成8的,否则不少状况下会出问题哦~编程

  1. softtabstop

Number of spaces that a <Tab> counts for while performing editing operations, like inserting a <Tab> or using <BS>. It "feels" like <Tab>s are being inserted, while in fact a mix of spaces and <Tab>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 <Tab>s.vim

初看没看明白…… 再看,app

  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.ide

增长、减小缩进或者是使用了自动缩进时使用此设置的值。默认值为8。 4. expandtabflex

In Insert mode: Use the appropriate number of spaces to insert a <Tab>. 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<Tab>. See also |:retab| and |ins-expandtab|.this

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

  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 <CR> in Insert mode or when using the "o" or "O" command). If you do not type anything on the new line except <BS> or CTRL-D and then type <Esc>, CTRL-O or <CR>, the indent is deleted again.spa

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

  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)
相关文章
相关标签/搜索