21

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.

20

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.

3

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.

  • Your answer seems rather like a rant, and doesn't appear to answer the question. – Herb Wolfe Mar 19 '17 at 14:28
  • @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 '17 at 14:52
1

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, privacy policy and cookie policy

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