2

Length of tabs after initial char

Start gvim with the --clean option, to get a clean slate, and then do:

:set noet ts=5 sts=3 sw=2

Then enter the following

1234567890

and on the next line, press tab twice. The cursor will end up between the 6 and the seven (I use two bars to make it more obvious):

123456|7890
      |

and if you replace spaces with a dot (to see where they are), doing :%s/ /./g you will get:

123456|7890
     .|

Meaning there will be a tab with the length of five spaces, before that last space, that is now a dot. This is how I expected things to function: you add sts spaces until there is more than ts spaces, at which point you replace ts spaces with an actual tab (and keep any remaining spaces).

But now suppose you add a character before the tab (let us say an a). Pressing tab twice will still move the cursor in the same way, i.e., first after the 3, then after the 6. But if you now do the same substitution, you will get a tab that has length 4! (from indexes 2 to 5)

123456|7890
a    .|

Repeating with the experiment with two starting characters will produce a tab with length 3:

123456|7890
ab   .|

But this seems to only happen with the first substitution: for any subsequent ones, the tab will indeed have length 5.

So my question is: why does vim behave in this manner? In could not find an explanation for this in the docs.

Thank you for your help.

1 Answer 1

2

The 'tabstop' setting in Vim works a bit differently then what you expect.

In classical typewrites at tabstop is a location where the carriage movement is halted. See also the the wikipedia. That means, that the actual character width of a tab can be different, since it depends on from where the tab has been pressed.

So with a tabstop setting of 8, the default, the cursor will be moved to position 8. It does not matter, where exactly the cursor is before that column, meaning depending on the text in this line, the tab might be either 8 chars wide, or 7 or 6, ... or even 1. After column 8, the cursor will be moved to the next column that is a multiple of your tabstop setting (so 8 for the default).

2
  • I just did more experimenting: irrespective of previous text in the current line, even if it is more than tabstop-len chars, pressing tab makes vim go to the next closest multiple of tabstop, and obviously subsequent tabs immediately following will have a length of tabstop chars. Thanks! (Maybe something about this should be added to the fine manual? I reckon' most people won't know about classical typewrites ;-) )
    – wmnorth
    Dec 7, 2018 at 12:12
  • 1
    In other words, tabs were intended for table alignment, not inserting a character of a specific width.
    – D. Ben Knoble
    Dec 7, 2018 at 16:14

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.