How to use spaces instead of tabs in vim?


I program in python with VIM 8, and I have this doubt, I read that in PEP8 it is recommended that the indentations for python must be of 4 SPACES, in my .vimrc I have this configured by a recommendation of a Youtube video:

set shiftwidth=4
set softtabstop=4
set expandtab
set autoindent

Is this configuration correct so that each time you press the TAB key the 4 spaces are automatically made?

I have seen other configurations like, tabstop, smarttab and others but it is not very clear to me which one I should use for micaso.

Thanks for the help.

My .vimrc on github: link

asked by set cain 21.08.2018 в 02:29

2 answers


Let's see:

  • shiftwidth : It is to determine the indentation spaces in the case of the code, if you are also going to use the tab to establish the indentation. Remember also, that generally (not necessarily for code), you have the tabstop setting to determine the length of tab .

  • expandtab : This setting is what tells Vim to "expand" every tab in spaces.

  • softtabstop : It makes sense if you do not expand the tabs to spaces with set noexpandtab (not your case) and if in addition the value of this configuration differs from the tabstop . Makes it Vim who decides how to combine spaces and tabs to reach the figure of softtabstop '.

  • autoindent : It will simply keep the current indentation on the next line you type

What you're looking for really only need:

set shiftwidth=4
set expandtab
set autoindent
answered by 21.08.2018 в 04:51

I found another question similar to yours in the English version.

here the most voted answer

add this in your .vimrc

filetype plugin indent on
" show existing tab with 4 spaces width
set tabstop=4
" when indenting with '>', use 4 spaces width
set shiftwidth=4
" On pressing tab, insert 4 spaces
set expandtab

more info in:


Good luck, R6

answered by 22.08.2018 в 23:04