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?
Depending on your answer, one of the following sets of
settings should work for you:
For tab characters that appear 4-spaces-wide:
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:
set tabstop=8 softtabstop=0 expandtab shiftwidth=4 smarttab
To make the above settings permanent add
these lines to your 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.
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
. 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.
expandtab
Enabling this will make the tab key (in insert mode) insert spaces instead of
tab characters. This also affects the behavior of the retab
command.
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 (i.e. the
only preceding characters are whitespace).
For more details on any of these see :help 'optionname'
in vim (e.g. :help 'tabstop'
)
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…