Vim: tabs vs. spaces

Where I uncover my shallow understanding of Vim configuration and I encourage reader for joint adventure in intricacies of tabulation and indentation.

Sillicon Valley

The problem seemed not so important to me until I started analysing how I should set up my vim to work with files from really large open source project.

When I cooperate in professional projects with others, I have seperate coding environments, lastly with “expanding” to four spaces.

When I work alone, I use two spaces width for indentation, pressing tab is expanded automatically to spaces.

Coincidentally my first OS project I contributed to had the same setting as my own. I’m referring to exercism.org

How to configure it in Vim?

 set tabstop =2
 set softtabstop =2
 set shiftwidth =2
 set expandtab
 set autoindent

Above are my typical lines in the ~/.vimrc file.
tabstop option denotes how many spaces single tab character represents in the text.
It seems easy to interpret but don’t be misled. I also had gotten such impression by quickly skimming the documentation. If you wish, click the link under the option’s name now and read its description thoroughly.
softtabstop option is set to 0 by default and then only tabstop counts.
However, not knowing precisely how it worked, I set it as tabstop because it worked for indentation and that’s all I wanted.
Third option working together with the previous ones and important for indentation is expandtab.
It’s of on/off type, unset by set noexpandtab. When on, it causes every tab press to insert spaces instead. How many spaces? As many as tabstop value, unless softtabstop is not zero than as many as softtabstop.

Exactly. These options cooperate for common result and I didn’t even mention shiftwidth and *indent options yet!

Summarizing this part. Configuration above is defensive and stems from the need of indent of two spaces per level. It also complicates the cooperation with other developers who use different settings, particularly with tabs involved.

In the next post I’ll try experiment with such configurations and I would gladly read comments on that, as I’m curious how others choose their settings.