38

I've been aware of these tab related settings in Vim:

  • ts
  • sw
  • expandtab
  • smarttab
  • sts

My normal setting is set ts=4 sw=4 expandtab smarttab.

But according to sts documentation, if expandtab is not set, it will try to use a combination of spaces and tabs to make up of the desired number of blanks for a tab size.

I can't really think of a useful scenario for this setting: Why would someone wants to have a fixed number of tabs and spaces that may just mess things up? The only advantage of doing this that I can think of is to reduce the number of characters and thus reduce the file size, which seems weird to me.

0

4 Answers 4

27

As the softtabstop documention mentions, it's useful if you want to keep the default tab stop size of 8, but edit a file as if the tab stop size was some other value. For example, if you wanted an indentation level of 4 while editing code, but some comments had tab-indented text such as a table that depended on a tab stop of 8, you could set sts to 4.

One difference I notice between your tab settings (set ts=4 sw=4 expandtab smarttab) and the use of sts (e.g. set ts=8 sts=4) is when backspacing over spaces in the middle of a line of text. For example, consider the following line of text, where · indicates a space:

some·text·······more·text

In insert mode, move the cursor just before the m of more text. With your settings, pressing Backspace deletes a single space. But while using sts=4, Backspace deletes all the way to the previous tab stop, as it would behave if the spaces were a tab character instead.

You can set sts=4 in addition to your settings and get the mid-line backspace behavior while not mixing tabs and spaces. Tabs will be expanded to four spaces, but you can backspace over a range of spaces in the middle of a line.

22

The value of 'softtabstop' equals how many columns (=spaces) the cursor moves right when you press <Tab>, and how many columns it moves left when you press <BS> (backspace) to erase a tab.

Thus, if 'tabstop' is 8, and 'softtabstop' is 4, all displayed tabs are 8 columns wide, but every press of the <Tab> key just gets you half a tab (=4 spaces) forward at a time:

softtabstop=4

(red: tabs, green: spaces)

As you can see, whenever the spaces add up to the size of 'tabstop', they are turned into a single tab character. (This can be prevented by setting 'expandtab'.)

A value of 0 disables 'softtabstop', so pressing <Tab> then gets you a regular tab forward:

softtabstop=0

Finally, a negative value means fall back to the value of 'shiftwidth'. Therefore, the cursor here advances two spaces at a time:

softtabstop=-1

1
  • 5
    Welcome to Vi and Vim! It might be worth sharing what you did to get the highlights/characters
    – D. Ben Knoble
    Nov 13, 2020 at 16:09
7

See the example below,

// Start vim without loading your vimrc. set only tabstop=8 softtabstop=4.
// This makes <Tab> in insert mode equals to 4 <Space> length at max.

// In insert mode, type 12, one <Tab>, 5. We get insertion below,
12··5
// Quit insert mode. Move cursor back, we find 2 <Space> inserted.

// In insert mode, type 12, two <Tab>, 9. We get insertion below,
12······9
// Quit insert mode. Move cursor back, you find a <Tab> inserted.

// In insert mode, type 12, three <Tab>, 3. We get insertion below,
12··········3
// Move the cursor back, you find a <Tab> and 4 <Space> inserted.

// We can even set sotftabstop=12, but this time we only need type one <Tab>, then 3.
12··········3
// Move the cursor back, you find a <Tab> and 4 <Sapce> inserted.

So tabstop is about how wide a Tab is defined, while softtabstop is about how far cursor moves while typing Tab. When they are not set to be the same value, it means that if you hit the Tab keystroke, it does not imply trivially a Tabcharacter. In whatever cases, It is vim who decide once you quit insert mode. Vim will first try to match the insertion by as many tabstop as it can; if at last it cannot make a full tabstop, vim simply compensates by Space.

To speak a little bit more, if you set expandtab, it is equal to say to vim:

Please do not bother to calculate how many Tab and Sapce is needed. Simply insert Space.

2
  • @Herb Wolfe I wish my answer can help PO figure out what happens when unusual setting is made like:set st=7 sts=13. As vim is not only designed for coding, but originally for text editing.
    – Chen XI
    Mar 19, 2017 at 14:52
  • 2
    Use vim --clean to start vim without loading your vimrc.
    – user17424
    Apr 20, 2021 at 13:10
2

Simple answer:

For example, it's extremly useful for programming, especially in Python, in which tabulation is crucial. When you press backspace with sts=4, it will untab the line, not delete a single space character.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.