4

Questions

In the following example:

  1. Why in the first line, a \t is inserted but only 4-col blank is displayed? Shouldn't it be 8 according to tabstop?
  2. Why the two <TAB> hits in the first line and fifth lines have different results? (one is 09 and the other is 20202020)

.vimrc

set noexpandtab
set tabstop=8                             
set shiftwidth=4                                    
set softtabstop=4

Text

I created the following text by first inserting 8 empty lines and then typing at the beginning of each line, so that there is no shiftwidth generated. (each 4-col blank below is inserted by hitting <TAB>):

1tab    done
2tabs        done
3tabs            done
4tabs                done
    1tab
        2tabs
            3tabs
                4tabs

hex representation

Then I did :%!xxd, this is what I got (with some trailing newlines):

0000000: 3174 6162 0964 6f6e 650a 3274 6162 7309  1tab.done.2tabs.                                                                                                        
0000010: 2020 2020 646f 6e65 0a33 7461 6273 0909      done.3tabs..                                                                                                        
0000020: 646f 6e65 0a34 7461 6273 0909 2020 2020  done.4tabs..                                                                                                            
0000030: 646f 6e65 0a20 2020 2031 7461 620a 0932  done.    1tab..2                                                                                                        
0000040: 7461 6273 0a09 2020 2020 3374 6162 730a  tabs..    3tabs.                                                                                                        
0000050: 0909 3474 6162 730a 0a0a 0a0a 0a0a 0a0a  ..4tabs.........

Related discussion

There are some pretty good answers here but I still don't understand what is going on in this particular case.

(PS: this thread was posted here but no comprehensive solution is found so I'm reposting it now.)

Update

The link from @EvergreenTree below is very helpful and almost solved my problem, however it seems not to answer my second question.

Solution (adapted from Christ's answer below)

  1. tabstop=8 does not necessarily mean each \t is displayed as 8 columns, instead it means each \t is displayed as ALIGNED to 8 columns.
  2. Similarly, softtabstop=4 means each <TAB> hit (in contrast to \t for tabstop above) is displayed as aligned to 4 columns.
  3. When noexpandtab is set, <TAB> hit may generate \t, spaces, or a mixture of them, depending on the current position of cursor. More precisely, it will advance to next position aligned with softtabstop. If this position is not aligned with tabstop, spaces will be inserted. If it is, \t will be inserted and redundant former spaces will be removed if needed. (see the video provided by @EvergreenTree below for an example of removing previously existed spaces).
  • 2
    Here is a vimcast on the topic: vimcasts.org/episodes/tabs-and-spaces I think it clarifies what all of the indentation options do. – EvergreenTree Dec 30 '15 at 0:11
  • It's indeed very comprehensive and clear. Thank you very much! – wlnirvana Dec 30 '15 at 1:04
  • 4
    @wlnirvana if you have understood what's going on, would you care to write an answer explaining things? Might be useful for future visitors. – muru Dec 30 '15 at 1:48
  • I was trying to but I suddenly found that my problem is not solved thoroughly... See update above. – wlnirvana Dec 30 '15 at 9:20
  • You can post that as an answer, instead of putting it in the question. – muru Dec 30 '15 at 17:15
1

By setting softtabstop to a value smaller than the tabstop value, Vim must insert spaces, as the tab when entered in insert mode may only be 4 chars wide. In contrast to line 1, the tab will move to column eight, even if there is some text in front of it. This can be seen, by entering o<C-U><C-V><Tab> and then moving back to the first column and starting to insert some chars. Until you move over column 7, the tab after it will always stay.

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.