It depends on what you mean.
(这取决于你的意思。)
Do you want actual tab characters in your file to appear 4 spaces wide, or by "tab" do you actually mean an indent , generated by pressing the tab key, which would result in the file literally containing (up to) 4 space characters for each "tab" you type? (您是否希望文件中的实际制表符显示为 4个空格宽,或者“tab”是否实际上是指通过按Tab键生成的缩进 ,这将导致文件字面上包含(最多)4个空格字符你输入的每个“标签”?)
Depending on your answer, one of the following sets of settings should work for you:
(根据您的答案,以下设置之一应该适合您:)
For tab characters that appear 4-spaces-wide :
(对于出现4个空格宽的制表符 :)
set tabstop=4
If you're using actual tab character in your source code you probably also want these settings (these are actually the defaults, but you may want to set them defensively):
(如果您在源代码中使用实际制表符,则可能还需要这些设置(这些实际上是默认设置,但您可能希望以防御方式设置它们):)
set softtabstop=0 noexpandtab
Finally, if you want an indent to correspond to a single tab, you should also use:
(最后,如果您希望缩进对应于单个选项卡,您还应该使用:)
set shiftwidth=4
For indents that consist of 4 space characters but are entered with the tab key:
(对于包含4个空格字符但使用Tab键输入的缩进 :)
set tabstop=8 softtabstop=0 expandtab shiftwidth=4 smarttab
To make the above settings permanent add these lines to your vimrc .
(要使上述设置永久添加到您的vimrc 。)
In case you need to make adjustments, or would simply like to understand what these options all mean, here's a breakdown of what each option means:
(如果您需要进行调整,或者只是想了解这些选项的含义,这里是每个选项的含义细分:)
tabstop
The width of a hard tabstop measured in "spaces" -- effectively the (maximum) width of an actual tab character.
(在“空格”中测量的硬tabstop的宽度 - 实际上是实际制表符的(最大)宽度。)
shiftwidth
The size of an "indent".
(“缩进”的大小。)
It's also measured in spaces, so if your code base indents with tab characters then you want shiftwidth
to equal the number of tab characters times tabstop
. (它也是在空格中测量的,所以如果您的代码库用制表符缩进,那么您希望shiftwidth
等于制表符的数量乘以tabstop
。)
This is also used by things like the =
, >
and <
commands. (这也用于=
, >
和<
命令之类的东西。)
softtabstop
Setting this to a non-zero value other than tabstop
will make the tab key (in insert mode) insert a combination of spaces (and possibly tabs) to simulate tab stops at this width.
(将此设置为tabstop
以外的非零值将使Tab键(在插入模式下)插入空格组合(以及可能的选项卡)以模拟此宽度处的制表位。)
expandtab
Enabling this will make the tab key (in insert mode) insert spaces instead of tab characters.
(启用此选项将使Tab键(在插入模式下)插入空格而不是制表符。)
This also affects the behavior of the retab
command. (这也会影响retab
命令的行为。)
smarttab
Enabling this will make the tab key (in insert mode) insert spaces or tabs to go to the next indent of the next tabstop when the cursor is at the beginning of a line (ie the only preceding characters are whitespace).
(启用此选项将使tab键(在插入模式下)插入空格或制表符,以便当光标位于行的开头时(即前面的唯一字符是空格),转到下一个tabstop的下一个缩进。)
For more details on any of these see :help ' optionname '
in vim (eg :help 'tabstop'
)
(有关其中任何一个的更多详细信息,请参阅:help ' optionname '
vim中:help ' optionname '
(例如:help 'tabstop'
))
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…