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 addthese 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 oftab 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 togo to the next indentof the next tabstop when the cursor is at the beginning of a line (i.e. theonly preceding characters are whitespace).
For more details on any of these see :help 'optionname' in vim (e.g. :help 'tabstop')
set tabstop=4 " The width of a TAB is set to 4." Still it is a \t. It is just that" Vim will interpret it to be having" a width of 4.
set shiftwidth=4 " Indents will have a width of 4
set softtabstop=4 " Sets the number of columns for a TAB
set expandtab " Expand TABs to spaces
function! UseTabs()set tabstop=4 " Size of a hard tabstop (ts).set shiftwidth=4 " Size of an indentation (sw).set noexpandtab " Always uses tabs instead of space characters (noet).set autoindent " Copy indent from current line when starting a new line (ai).endfunction
function! UseSpaces()set tabstop=2 " Size of a hard tabstop (ts).set shiftwidth=2 " Size of an indentation (sw).set expandtab " Always uses spaces instead of tab characters (et).set softtabstop=0 " Number of spaces a <Tab> counts for. When 0, featuer is off (sts).set autoindent " Copy indent from current line when starting a new line.set smarttab " Inserts blanks on a <Tab> key (as per sw, ts and sts).endfunction
set number " show line numberset tabstop=2 " set display width of tab; 1 tab = x space withset expandtab " transform tab to x space (x is tabstop)set autoindent " auto indent; new line with number of space at the beginning same as previousset shiftwidth=2 " number of space append to lines when type >>