Vim is very accommodating when it comes to tab Vs. space preferences. As I understand it, the tabstop
setting indicates the width of a tab character. The shiftwidth
setting specifies how many columns to increment/decrement when using the << and >> commands, whereas the softtabstop
setting influences the amount of whitespace to be inserted when you press the Tab key in insert mode. If expandtab
is on, the tab key inserts softtabstop
number of space characters. Whereas with expandtab
switched off, pressing the Tab key inserts a the smallest possible number of tab+space characters that matches softtabstop
. (Please correct me if I'm wrong.)
This final point makes me wonder: is there a practical case where you wouldn't want shiftwidth == tabstop && tabstop == softtabstop
? I can't think of one. As far as I am concerned, it would be most convenient if I could set all 3 of these to the same value, in one single assignment. e.g. calling:
:set stab=4
which would be equivalent to running:
:set tabstop=4 softtabstop=4 shiftwidth=4
Can anyone suggest how this could be done?
UPDATE
Thanks for the replies so far from too much php, hobbs and kaiser.se. Rather than reply to each individually, I'm updating the question here.
Softtabstop with expandtab switched off
I said above that with expandtab switched off, pressing the Tab key inserts a the smallest possible number of tab+space characters that matches softtabstop
. I stand by that, but I think I need to explain what I meant. I shall attempt to do so by way of a few examples. To follow along, run :set list
so that you can see tab characters.
tabstop=4 softtabstop=2 shiftwidth=4 noexpandtab
In insert mode, pressing the tab key inserts 2 space characters. Press the tab key a second time, and instead of inserting two more space characters (for a total of 4 space characters) it replaces the previous 2 spaces with a single tab character. Tabstop is set to 4, so a single tab character has the same width as 4 spaces.
tabstop=4 softtabstop=6 shiftwidth=4 noexpandtab
In insert mode, pressing the tab key inserts 1 tab character plus 2 spaces. The tab character has a width of 4, so the total width is 6, and this is achieved using 3 characters. Pressing the tab key a second time inserts two tab characters, and removes the two spaces that were inserted previously. The total width is 12, and this is achieved using 3 characters.
In both of these examples, Vim inserts the minimum possible number of tab+space characters that matches softtabstop.
If I am working with expandtab switched off, I can't see myself wanting the extra granular control that can be achieved by setting softtabstop to a different value from tabstop. It would still be useful for me to be able to set tabstop
, softtabstop
and shiftwidth
to the same value with a single command.
Does expandtab make softtabstop redundant?
tabstop=4 softtabstop=0 shiftwidth=4 expandtab
In insert mode, pressing the tab key inserts 4 spaces. Pressing the delete key deletes a single space - so you have to backspace 4 times if you hit the tab key by accident.
tabstop=4 softtabstop=4 shiftwidth=4 expandtab
In insert mode, pressing the tab key inserts 4 spaces. Pressing the backspace key deletes 4 spaces.
If I am working with expandtab
switched on, I would prefer the delete key to remove the same amount of whitespace as the tab key inserts. So in this case, too, I feel that it would be useful to be able to assign the same value to tabstop
, softtabstop
and shiftwidth
simultaneously.
A shortcut would still be useful
It's great that Vim provides so much flexibility, but I can't see myself needing it. I just want to be able to choose the width of a tab, and whether it is a 'hard' tab (using a tab character) or a 'soft' tab (made up of spaces). Toggling between hard and soft tabs is easy enough (:set expandtab!
), but I wish it was more straightforward to set the width of tab, without having to fiddle with 3 different parameters.
So my proposed suggestion for something like :set stab=4
still sounds good to me.
See Question&Answers more detail:
os