Vim Text Editor: Use Spaces Instead of Tabs

By default, Vim uses tabs instead of spaces when you press the tab button.  There is an ongoing argument around the internet (which we won’t go into now, but may in the future!) on what you should use, but, either way, you may want/need to change your tabs to spaces.  These are often called “soft tabs” – every time you press the tab key, it inserts a certain amount of spaces.

You can do this quite easily in Vim, by editing your .vimrc file.  You simply add these two lines:

set expandtab
set tabstop=2

If you don’t want to edit your .vimrc file, or just want to make it a temporary change whilst you work on a certain file, you can also change the setting from within vim by prefixing the above commands with a colon:

:set expandtab
:set tabstop=2

This will set the space count to 2, on every tab, which is a common requirement (some guides recommend two spaces so lots of people use it).  Of course, it could easily be 4, 6, 8 etc.. so choose whichever you require.