vim: tabstop, shiftwidth, softtabstop, expandtab (Explained)

The following is an excerpt by Ari Sweedler regarding .vimrc tab and indention settings:

  1. tabstop — How many columns wide is a tab character worth? Does a \t take up 4 columns? 2 columns? 8 columns? Changing this value will change how tabs are displayed. Must be greater than 0, as tabs having no width is never desired.
  2. shiftwidth — Referred to for “levels of indentation”, where a level of indentation is shiftwidth columns of whitespace. That is, the shift-left-right commands, the formatting commands, and the behavior of vim with cindent or autoindent set is determined by this setting. A shift command says “give me one more (or one less) level of indentation”. Formatting commands are more complicated (:help gq if you’re curious, they basically parse your file and determine how many layers of indentation should be there). So if vim decides that a line needs 3 levels of indentation then it’ll place 3*shiftwidth columns of whitespace.
  3. expandtab — Unlike the other settings, this one is a Boolean, and quite straightforward. It answers the question: should vim place spaces or tab upon receiving a whitespace command or a tab keypress.
  4. softtabstop — Referred to for the tab key and backspace key. How much whitespace should be inserted when the tab key is pressed? And how much whitespace should be removed when the backspace key is pressed?

If you want more detail, I’d highly recommend you click the source link below. Ari goes into a lot more depth, using plain English and examples, to describe the above settings.

SOURCE: Ari Sweedler via Medium