0

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.

Community
  • 1
  • 1
wlnirvana
  • 1,811
  • 20
  • 36
  • Actually it would have been better to ask this here: http://vi.stackexchange.com/ – agold Dec 16 '15 at 16:27
  • Wow, I didn't know there is a vi stackexchange before. Is it possible to migrate this post there? – wlnirvana Dec 16 '15 at 17:12
  • I don't think it is [necessary](http://meta.stackoverflow.com/a/251654/1771479), since the question is not completely off-topic on StackOverflow. – agold Dec 16 '15 at 17:22

2 Answers2

1
  1. softtabstop is meant to be used that way, it will make your tabs appear a certain length even though they really are the length of tabstop

    'softtabstop' 'sts' number (default 0)

    ...

    This is useful to keep the 'ts' setting at its standard value of 8, while being able to edit like it is set to 'sts'. However, commands like "x" still work on the actual characters.

  2. As for the weird behaviour of inserting spaces at the beginning, I'm convinced it has to do with smarttab which is on by default and shifts instead of inserting a tab at the beginning of a line.

    'smarttab' 'sta' boolean (default on) global

    When on, a < Tab> in front of a line inserts blanks according to 'shiftwidth'. 'tabstop' or 'softtabstop' is used in other places. A will delete a 'shiftwidth' worth of space at the start of the line.

IGI-111
  • 11
  • 3
0

IGI-111 answered your first question correctly, albeit maybe not clearly enough.

  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?

Keep in mind that TAB doesn't insert a fixed number of blanks, but advances the position to the next multiple of a certain number (if numbered from zero).

What do you mean by "even though they really are the length of tabstop"?

He must mean that TAB characters, when printed to your terminal, would cause the cursor position to advance to the next multiple of tabstop (8), while pressing Tab in vim advances to the next multiple of softtabstop (4). If this next multiple of softtabstop isn't a multiple of tabstop, vim has to use spaces (0x20).

  1. Why the two <TAB> hits in the first line and fifth lines have different results? (one is 09 and the other is 20202020)

That's because in the first line you hit Tab after you typed 1tab, i. e. you were at position 4 and to advance to position 8, which is a multiple of tabstop, a TAB (0x09) did the job, while in the fifth line you hit Tab at the beginning position 0 and to advance to position 4, which is not a multiple of tabstop, spaces are needed.

Armali
  • 18,255
  • 14
  • 57
  • 171