Vim Tips Wiki

Indent with tabs, align with spaces

1,610pages on
this wiki
Revision as of 09:45, March 8, 2010 by (Talk)

Tip 1626 Printable Monobook Previous Next

created June 6, 2009 · complexity basic · version 7.0

Tabs vs. spaces for whitespace: which to use? Tabs are good for indentation because their width can be adjusted according to preference (:help 'tabstop'). On the other hand, spaces must be used to ensure text stays lined up across lines if tabstop is changed.

Using the "smart tabs" scheme described below combines the strengths of both.

This tip discusses indenting using tabs and spaces, including methods to allow tabstop adjustment, while maintaining textual alignment. See indenting source code for background information on indenting.

Spaces or tabs?

Programs use whitespace (spaces or tabs) for two purposes:

  • To indent blocks of code (for example, to show the code that is in a loop).
  • To align text (for example, so comments start in the same column).

Some procedures commonly used to write programs are:

  • Use spaces for indentation and alignment.
  • Use tabs for indentation and alignment.
  • Use a mixture of tabs and spaces for indentation and alignment.
  • Use tabs for indentation and spaces for alignment ("smart tabs").

In large projects, using as many tabs as possible can significantly reduce the size of the source code, and in some rare cases (for example, make files), tabs must be used for indentation. Apart from these considerations, whether you use tabs or spaces is generally a personal choice, or is determined by the coding style of the project.

Using spaces for indentation means that the code will look the same on all systems, even if the tabstop option is changed. However, different people like different indent sizes, so a common procedure is to use tabs for indentation so the indent size can easily be adjusted to suit whoever is working on the code. If the tabstop size is changed, it is necessary to use spaces for alignment because that will maintain the alignment with a different tabstop, provided the lines with aligned text use the same number of tabs for indenting.

Here is an example using tabs for indentation and spaces for alignment:

int sample(int a)
|-------int   count;      // variable names are aligned
|-------float average;    // (some people like it that way)
|-------if (a > 0)
|-------|-------return 1;
|-------return 0;

In the above, "|-------" represents a tab using the default :set tabstop=8. Entering :set tabstop=4 makes the code appear as:

int sample(int a)
|----int   count;      // variable names are aligned
|----float average;    // (some people like it this way)
|----if (a > 0)
|----|----return 1;
|----return 0;

This illustrates that using tabs for indentation and spaces for alignment keeps the alignment unchanged when the tabstop is altered (for lines with the same indent).

Using tabs for indentation

If you want to use only tabs for indentation (not spaces), enter the following command (replace 4 with your preferred column width for each indent level):

:set noet ci pi sts=0 sw=4 ts=4

The previous line is an abbreviated equivalent of these commands:

:set noexpandtab
:set copyindent
:set preserveindent
:set softtabstop=0
:set shiftwidth=4
:set tabstop=4

If you want the commands to affect only the current buffer, replace set with setlocal (abbreviated as setl). More information is here.

Smart tabs

The settings above use hard tabs as far as possible. To use tabs in a more "semantic" way – i.e., the number of tabs equals the indentation level – install the Smart Tabs plugin.

The plugin ensures that tabs are only used for indentation, while spaces are used everywhere else. When you press Tab in insert mode, a tab is inserted when indenting, and/or the correct number of spaces when aligning text (see continuation lines). Different people can then use whatever shiftwidth and tabstop they want (provided these two values are equal) – the end result is the same, and is displayed correctly everywhere.

You do not need a "smart" editor to display or edit smartly tabbed files – that's the whole point. Still, if your non-Vim-using collaborators don't agree, pass along these pointers: smart tabs for Emacs, and IntelliJ IDEA has a built-in "smart tabs" option.

Continuation lines

When expressions span multiple lines, you may want to line up the beginning of those lines with the beginning of the expression in the first line:

int f(int x,
      int y) {
    return g(x,

To make Vim format the code in this way, using four-column indentation, enter the following (use setl instead of set to affect only the current buffer):

:set noet sts=0 sw=4 ts=4
:set cindent
:set cinoptions=(0,u0,U0

Using the Smart Tabs plugin, tabs will then only be used for indenting (in the third and fourth lines), while spaces will be used for alignment:

int f(int x,
      int y) {
|---return g(x,
|---         y);

That makes the alignment of "x" and "y" independent of tabstop.

See also


Advertisement | Your ad here

Around Wikia's network

Random Wiki