0
votes

I've read the following articles:

  1. http://vimcasts.org/episodes/tabs-and-spaces/
  2. https://medium.com/@arisweedler/tab-settings-in-vim-1ea0863c5990

To try and understand what things like tabstop, shiftwidth and softtabstop mean. Here's what I get so far:

  1. tabstop sets the way that tab appears on the vim editor. If you have set ts=4, then it will look like you have 4 spaces whenever you press on the tab key on your keyboard. If you have set ts=50, then it will look like each time you press Tab, you get 50 spaces. In the case that you have set expandtab, the characters will be 50 spaces, otherwise you will just have 1 tab character that seems like 50 spaces (so just 1 byte from ASCII?, seems a bit weird).

  2. shiftwidth sets the amount of indentation for things like <<, >>, ==, and also autoindent. So this means that you know, if you have set sw=5, and say ts=4, then when >> is typed, it puts in 1 tab and 1 space and does some math as such.

  3. softtabstop I'm not so sure about. From what I have read, the sources seem to have different explanations.

Source 1 says:

If you prefer to work with tab characters then it is a good idea to ensure that tabstop == softtabstop. This makes it less likely that you’ll end up with a mixture of tabs and spaces for indentation.

If you prefer to work with spaces, then it is preferable to ensure that softtabstop == shiftwidth. This way, you can expect the same number of spaces to be inserted whether you press the tab key in insert mode, or use the indentation commands in normal/visual modes.

Source 2 says:

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?

First of all, I don't really know how to reconcile Source 1 and Source 2. Why does shiftwidth matter (source 1), if we only do it for the tab key and backspace key (source 2). The shiftwidth character should control how much space is added during >>, etc, right? Why does it matter for spaces?

"If you prefer to work with tab characters then it is a good idea to ensure that tabstop == softtabstop.": this sort of makes sense. softtabstop determines how much space each keypress of Tab makes, and if there is discord between tabstop and softtabstop then we could end up using spaces to make things even out.

Lastly, I was trying out some of the examples in Source 2 at the bottom. (Note in my vimrc I have the following lines and only this line affecting whitespace: set tabstop=4 shiftwidth=4 expandtab)

I made the following changes in a new file:

:set ts=5 sts=3
:retab

When I pressed the Tab key, contradictory to what the author of article 2 said, I was getting 4 spaces, not 3. So did he say something wrong? I then set shiftwidth to 3, and NOW I was getting 3 spaces when I pressed the tab key (and also when I did >>). I don't know the meaning of this. Was he wrong?

Based on these observations, can anyone tell me what sts is actually doing? When we have a tab character no matter what the ts is it always 1 byte? Does sw affect Tab key presses? Is there any other misunderstanding I have about these parameters? Vim is hard.

1

1 Answers

0
votes

If you have set ts=50, then it will look like each time you press Tab, you get 50 spaces.

Nope. While inserting new tabs by Vim it's the softtabstop option which comes to play. But sts value is recalculated into ts one. So if you have ts=50 and sts=100 (and also noexpandtab, of course) then you get 2 hard tabs (\x9 byte) in the buffer.

To disable this feature you can set sts=0. In this case a tab will be just a tab / tabstop.