Skip to main content
Other Vim Tips

Set Indentation Width to 2 or 4 Spaces (or Tab) in Vim

Prefer tabs to spaces? Here's how to set indentations to use spaces instead of tabs in Vim.

Team LHB

Warp Terminal

Vim is one of the most popular terminal-based text editors for decades.

But no matter how long you have been using Vim, there are always more tips and tricks that you did not know about.

This one is about setting up indentation width in Vim to 2 spaces or 4 spaces. This is particularly helpful if you are a programmer, a Python one especially.

In your vimrc file (located at ~/.vimrc), add the following line to automatically use 2 spaces instead of tab in Vim.

set autoindent expandtab tabstop=2 shiftwidth=2

Don't worry. I'll go through it in detail.

Set automatic indentation

Imagine you are writing a line of code and the next line of code needs to be indented, you press the Enter key to go to the next line but the indentation is not applied automatically.

This can be very annoying. To automatically indent lines, add the following line to your 'vimrc'.

set autoindent

Once you have this saved in your 'vimrc', it will enable automatic indentation in your vim session no matter the programming or scripting language that you use.

Use spaces for indenting

If you want to use spaces for indenting your code, add the following lines to your '.vimrc' file.

set expandtab
set tabstop=<NUM OF SPACES>
set shiftwidth=<NUM OF SPACES>
  • The first line enables expandtab option in Vim. This option makes sure that spaces are used for indenting lines, even when you press the 'Tab' key.
  • The second option tabstop takes a numerical value. Let's say I typed set tabstop=2, this will insert 2 spaces for a line indent.
  • Finally, the third option, shiftwidth manages the indentation when you use the '>>' or '<<' operators to add or remove indentation to an already existing line/block of code.

I would suggest using 2 or 4 for tabstop and shiftwidth values.

I also recommend that you use the same values for tabstop and shiftwidth. Using different values might mess up your indentation.

Here is what indented code looks like in Vim:

Code indented using 4 spaces in Vim
💡
In rare cases, if you need to use the tab character, pressing the 'Tab' key will not work with expandtab enabled. To use the tab character, use the 'Ctrl + V' key combination and then type the tab character.

Vice versa: Use tabs for indentation

Serving to people standing on both sides of 'tabs vs spaces', let's now look at how you can use a tab character for indenting instead of spaces.

Add the following lines to your 'vimrc':

set noexpandtab
set tabstop=4
set shiftwidth=4

The only difference in these 3 lines for your vimrc to use tabs instead of spaces is the usage of noexpandtab instead of expandtab. The noexpandtab option prevents the conversion of tabs to spaces.

Conclusion

All the above works for new files. To convert tabs to spaces in the currently opened file in Vim, enter the Normal mode by pressing Esc key. Now use the retab command by pressing the ':' (colon) character and Vim will convert the existing tabs to spaces.

The war of tabs vs spaces aside, this tutorial aims at helping you set the indentation to whatever your preference might be.

If you are interested in learning more than just the Vim Basics, use this program by Jovica Ilic.

Mastering Vim Quickly - Jovica Ilic
Exiting Mastering Vim Quickly From WTF to OMG in no time
Team LHB