2

How does one get uniform tabs/spaces to display properly in other programs (like Less)?

For example, say I am modifying my .vimrc in Vim and I've created two columns on the same line (one for rule and one for comment).

In Vim the comments all line up:

set textwidth=80        " set line length
set tabstop=4           " how many cols a tab counts for
set expandtab           " insert spaces for <tab>
set softtabstop=4       " tabs are 4 spaces
set shiftwidth=4        " indent/outdent by 4 cols

Now for the sake of example, say I open it again in Less. The output would be something like:

set textwidth=80        " set line length
set tabstop=4             " how many cols a tab counts for
set expandtab             " insert spaces for <tab>
set softtabstop=4          " tabs are 4 spaces
set shiftwidth=4        " indent/outdent by 4 cols

Am I unknowingly mixing a combination of tabs and spaces? Or does it have to do with my tab settings?

  • How did you align your comments? Did you manually do it pressing <tab> and <space> to align these comments? Did you use something like tabular or vim-easy-align? – Peter Rincker Sep 25 '15 at 15:00
  • No it was all manual alignment with <tab> and possibly <space> – sussibar Sep 25 '15 at 15:02
  • 3
    A "standard" tabstop setting is 8. If you use something else, you risk these sorts of display problems. You can :set list to see the tabs in Vim. You can set tabstop to 8 and softtabstop and shiftwidth to 4 and get the editing behavior you want while preserving compatibility with other programs such as less. – garyjohn Sep 25 '15 at 15:39
  • 1
    The other way: set tab width for less output – muru Sep 25 '15 at 17:04
3

You always want to use spaces for alignments. Vim's <tab> will insert spaces or real tabs depending on the setting of 'expandtab'. You can see the value of 'expandtab' via :set expandtab?.

Personally I would suggest you use an some type of aligning plugin to avoid doing this manually. Both tabular and vim-easy-align are good choices. Example of using tabular:

:Tabularize/"/
| improve this answer | |
  • 3
    You always want to use spaces for alignments. - A more complete answer would begin with a phrase more or less like this: "The world of programmers is divided in two big tribes: tab lovers, and tab haters. They don't go along very well with each other". ;) – lcd047 Sep 25 '15 at 17:55
  • 2
    I agree there are different views on indention, but until variable tab stops or Elastic Tab stops become a thing not just for Vim, but all text editors everywhere. I think the only real option is spaces for alignments. I mean its pretty simple you can mix tab indent + spaces for alignment or spaces everywhere. This will work on all editors no matter the settings or line lengths. But tabs for alignment!? Whoa! Slow down! You are going to get in trouble before you know it. – Peter Rincker Sep 25 '15 at 18:30

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.