There are four things in vim related to tabs:
- tabstop
- expandtab
- shiftwidth
- softtabstop
Refer to useful reference of tab stops in vim
tabstop Set tabstop to tell vim how many columns a tab counts for. Linux kernel code expects each tab to be eight columns wide. Visual
Studio expects each tab to be four columns wide. This is the only
command here that will affect how existing text displays.
expandtab When expandtab is set, hitting Tab in insert mode will produce the appropriate number of spaces.
shiftwidth Set shiftwidth to control how many columns text is indented with the reindent operations (<< and >>) and automatic
C-style indentation.
softtabstop Set softtabstop to control how many columns vim uses when you hit Tab in insert mode. If softtabstop is less than tabstop
and expandtab is not set, vim will use a combination of tabs and
spaces to make up the desired spacing. If softtabstop equals tabstop
and expandtab is not set, vim will always use tabs. When expandtab is
set, vim will always use the appropriate number of spaces.
You can see current settings in vim by using below commands, in command mode:
:set tabstop?
:set expandtab?
:set shiftwidth?
:set softtabstop?
You can set values for current session by using commands below, in command mode:
:set tabstop=4
:set expandtab #if you want to remove expand tab, set noexpandtab
:set shiftwidth=4
:set softtabstop=4
Alternatively, if you want to make the changes permanent, you can make changes to vimrc file:
It is located in the below path:
~/.vimrc on Unix;
$HOME/_vimrc on Windows
For gvim, you can go to Edit -> startup settings to see current vimrc
settings and edit them.
You need to add a line to vimrc like given below:
set tabstop=4 softtabstop=4 shiftwidth=4 expandtab