Indent with tabs, align with spaces

From Vim Tips Wiki

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.

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:

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. The Smart Tabs plugin is just an added convenience. If your non-Vim-using collaborators remain unconvinced, pass them these pointers, whichever is appropriate: smart tabs for Emacs, and IntelliJ IDEA has a built-in "smart tabs" option.

This edit added copyindent and preserveindent to the #Using tabs for indentation section. I do not understand the benefit of these in that section which asserts that only tabs will be used for indentation. These options should be mentioned somewhere (here or in Indenting source code), but possibly not in that section. Any thoughts? JohnBeckett 22:23, March 8, 2010 (UTC)

Those options are useful for preserving manually created indentation, which is useful for using tabs for the indent (automatic) and then manually adding spaces to align stuff. --Fritzophrenic (talk) 16:18, March 4, 2015 (UTC)

Hi, do I need the Smart Tabs plugin to get the behaviour like "Indent_with_tabs,_align_with_spaces"? Because first you say
:set noet ci pi sts=0 sw=4 ts=4
to use only tabs for indentation (not spaces), which implicitly means spaces for aligning, no?
Later you say I need the Smart Tabs plugin because

The plugin ensures that tabs are only used for indentation, while spaces are used everywhere else.

Without Smart Tabs, Vim can insert tabs for indentation for you, but you will need to manually add spaces to do alignment past the indentation. The copyindent/preserveindent settings let Vim automatically copy that manual alignment for the next line, but the space-based alignment is still a manual step. And pressing Tab will always insert a tab with this setup, it will never insert any spaces, so it will take more keypresses to insert large amounts of alignment whitespace. With Smart Tabs, if I understand correctly what the plugin does, the plugin will automatically figure out whether you are adding indentation or alignment whitespace, and insert a space or tab accordingly. --Fritzophrenic (talk) 16:18, March 4, 2015 (UTC)