Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vim: Indent with tabs, align with spaces

I've already read several questions and answers:

  • Vim: Use tabs for indentation, spaces for alignment with C source files
  • Vim: Align continous lines with spaces

But none of them offers a solution for me.

I really want to apply the "Indent with tabs, align with spaces" principle, but when it comes to auto-indentation, I failed to teach Vim how to do that right.

Consider the code, assuming tabstops == 3, shiftwidth == 3

(>-- means tab, and . (a dot) means space):

{
>--long a = 1,
>-->--..b = 2,
>-->--..c = 3;
}

So, it indents with tabs as much as possible, and then fills the rest with spaces. But it is actually a very bad approach: when someone will read this code with different tab size, the code will be messed up. Here what it will look like with tab size equal to 8 chars:

{
>-------long a = 1,
>------->-------..b = 2,
>------->-------..c = 3;
}

It is horrible. The problem is that Vim doesn't distinguish between indentation and alignment.

To make it look correctly with whatever the tab size is, the code should be indented this way:

{
>--long a = 1,
>--.....b = 2,
>--.....c = 3;
}

Then, this code will look nice whatever that tab size is. For example, 8 chars:

{
>-------long a = 1,
>-------.....b = 2,
>-------.....c = 3;
}

How to achieve this?

like image 474
Dmitry Frank Avatar asked Mar 23 '15 10:03

Dmitry Frank


People also ask

How do I indent with tabs in Vim?

To indent the current line, or a visual block: ctrl-t, ctrl-d - indent current line forward, backwards (insert mode) visual > or < - indent block by sw (repeat with . ) then try hitting the F5 key while in insert mode (or just :set paste ).

How do you set a tab to indent to 4 spaces in Vim?

Within Vim, type a colon and then "set tabstop=4" which will set the tabs to display as four spaces. Hit colon again and type "set expandtab" which will insert spaces for tabs.

How do I change the spacing between tabs in Vim?

To convert tabs to spaces in the currently opened file in Vim, enter the Normal mode by pressing Esc key. Now use the retab command by pressing the ':' (colon) character and Vim will convert the existing tabs to spaces.

Should I use tabs or spaces for indentation?

Conclusion. So, at the end of the day, tabs versus spaces is truly a matter of preference, however the tab is still the character specifically designed for indentation, and using one tab character per indentation level instead of 2 or 4 spaces will use less disk space / memory / compiler resources and the like.


1 Answers

The most powerful way to influence indenting in Vim is via 'indentexpr'. From its :help:

The expression must return the number of spaces worth of indent. It can return "-1" to keep the current indent (this means 'autoindent' is used for the indent).

As this returns the number of spaces, not the rendered indent itself, and Vim only so far supports tab-, space-, or maximal-number-of-tab-followed-by-spaces (called softtabstop), this cannot be done.

So, if you really want to use this indent method (I personally like it for its purity and elegance, too! (but I don't employ it)), you have to turn off auto-indenting and auto-formatting and do the entire stuff manually by yourself, unfortunately.

like image 133
Ingo Karkat Avatar answered Sep 23 '22 09:09

Ingo Karkat