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)
相關文章
相關標籤/搜索