created 2001 · complexity basic · author Eugene Huang · version 6.0
The indent features of Vim are very helpful for indenting source code. This tip discusses settings that affect indentation. These settings mostly affect the automatic indentation which Vim inserts as you type, but also can be triggered manually with the = operator, so that you can easily Fix indentation in your buffer.
For related information, see:
- Shifting blocks visually commands to change indents
- How to stop auto indenting how to stop your indents from being changed
- Restoring indent after typing hash problems when typing
- Toggle auto-indenting for code paste to avoid broken indents when pasting in a terminal
- 1 Setup
- 2 Methods for automatic indentation
- 3 Different settings for different file types
- 4 References
- 5 See also
- 6 Related plugins
- 7 Comments
Indentation without hard tabs
For indentation without tabs, the principle is to set
'expandtab', and set
'softtabstop' to the same value, while leaving
'tabstop' at its default value:
set expandtab set shiftwidth=2 set softtabstop=2
These settings will result in spaces being used for all indentation.
Indentation purely with hard tabs
For indentation purely with hard tabs, the principle is to set
'shiftwidth' to the same value, and to leave
'expandtab' at its default value (
'noexpandtab'), and leave
set shiftwidth=2 set tabstop=2
These settings will result in hard tabs being used for all indentation.
Indentation with mixed tabs and spaces
For indentation with mixed tabs and spaces, the principle is to set
'softtabstop' to the same value, leave
'expandtab' at its default (
'tabstop' is left at its default value:
set shiftwidth=2 set softtabstop=2
These settings will cause as many hard tabs as possible being used for indentation, and spaces will be used to fill in the remains. If you want to distinguish between "indentation" and "alignment", i.e., the number of hard tabs equals the indentation level, use the Smart Tabs plug-in.
Explanation of the options
Vim's different indentation options are an endless source of grief and confusion for new users. There are plenty of example vimrcs out there that will tell you how theirs is the proper setup. More often than not, they are misguided. Let
:help be your reference, and this page be your guide to Vim's indentation options.
'tabstop'changes the width of the
TABcharacter, plain and simple.
'softtabstop'affects what happens when you press the
<BS>keys. Its default value is the same as the value of
'tabstop', but when using indentation without hard tabs or mixed indentation, you want to set it to the same value as
'expandtab'is unset, and
'tabstop'is different from
<TAB>key will minimize the amount of spaces inserted by using multiples of
TABcharacters. For instance, if
'tabstop'is 8, and the amount of consecutive space inserted is 20, two
TABcharacters and four spaces will be used.
'shiftwidth'affects what happens when you press
==. It also affects how automatic indentation works. (See below.)
'expandtab'affects what happens when you press the
'expandtab'is set, pressing the
<TAB>key will always insert
'softtabstop'amount of space characters. Otherwise, the amount of spaces inserted is minimized by using
<TAB>key presses are interpreted depending on where the cursor is. If
'smarttab'is on, a
<TAB>key inserts indentation according to
'shiftwidth'at the beginning of the line, whereas
'softtabstop'are used elsewhere. There is seldom any need to set this option, unless it is necessary to use hard
TABcharacters in body text or code.
'tabstop' value other than the default (8 spaces), will result in your file having a different appearance when using tools such as
type on Windows), which can't use a custom width tab character. On the other hand, using hard tabs for indentation, allows others to view your code with the amount of indentation they prefer. Which of these considerations should have priority, is a matter of personal preference (and company policy).
Methods for automatic indentation
There are a number of methods enabling automatic indentation in Vim, ranging from fairly "stupid" and unintrusive ones, like
'smartindent', to complex ones such as
'cindent' and custom indentation based on filetype using
'indentexpr'. The amount of indentation used for one level is controlled by the
'shiftwidth' option. (See above.)
'autoindent' does nothing more than copy the indentation from the previous line, when starting a new line. It can be useful for structured text files, or when you want to control most of the indentation manually, without Vim interfering.
'autoindent' does not interfere with other indentation settings, and some file type based indentation scripts even enable it automatically.
'smartindent' automatically inserts one extra level of indentation in some cases, and works for C-like files.
'cindent' is more customizable, but also more strict when it comes to syntax.
'cindent' might interfere with file type based indentation, and should never be used in conjunction with it.
When it comes to C and C++, file type based indentations automatically sets
'cindent', and for that reason, there is no need to set
'cindent' manually for such files. In these cases, the
'cinoptions' options still apply.
'cindent' should only be set manually if you're not satisfied with how file type based indentation works.
File type based indentation
This type of indentation is the most flexible, as it allows users to customize indentation per file type. You enable this type of automatic indentation with the following command:
filetype plugin indent on . This command will use indentation scripts located in the
indent folder of your vim installation.
For instance, the indentation scripts for C and C++ file types properly set the
'cindent' option, and there are very competent indentation scripts for Ruby, Perl and many other languages and file types. File type based indentation even works correctly with
Makefiles without interference!
If you plan on using file type based indentation, don't set
'cindent'. You may still set
'autoindent', since it doesn't interfere.
vimrc_example.vim that ships with Vim enables filetype based indentation:
if has("autocmd") " Enable file type detection. " Use the default filetype settings, so that mail gets 'tw' set to 72, " 'cindent' is on in C files, etc. " Also load indent files, to automatically do language-dependent indenting. filetype plugin indent on " ... endif
Different settings for different file types
You may want indentation for html files to use tabs with 2-columns per indent, while Python files use spaces with 4-columns per indent. To apply suitable settings automatically, first enable file type detection with the following in your vimrc:
filetype plugin indent on
html.vim with contents:
setlocal shiftwidth=2 setlocal tabstop=2
python.vim with contents:
setlocal expandtab setlocal shiftwidth=4 setlocal softtabstop=4
python.vim files should be in this directory (which you may need to create):
~/.vim/after/ftpluginon Unix-based systems; or
$HOME/vimfiles/after/ftpluginon Windows systems
The standard plugins probably do not change settings such as
shiftwidth, and in that case the directory
$HOME/vimfiles/ftplugin) would work as an alternative. However the "after" directory should be used because you intend to override settings from other plugins.
Using the "after" directory as above is recommended, but it is possible to put commands such as the following in your vimrc as an alternative:
autocmd FileType html setlocal shiftwidth=2 tabstop=2 autocmd FileType python setlocal expandtab shiftwidth=4 softtabstop=4
- :help shift-left-right
- :help =
- :help i_CTRL-T
- :help i_CTRL-D
- :help i_CTRL-F
- :help movement
- :help text-objects
- :help visual-mode
- :help cmdline-ranges
- 1231 Set indent parameters for Python files adjust Vim's indent settings to match each file edited (more than Python)
- Merge some of following tips.
- 221 Indenting Java throw statements
- 247 Preexisting code indentation
- 700 Indenting for Java
- 909 Easy indenting in insert and normal mode with no cursor displacement
- 1213 Better indent support for php with html
Select a block of lines having the same indent
- 103 Move to next/previous line with same indentation
- 112 Back and forth between indented lines again
- 477 Put the indentation level on the status line
- 1168 Folding for plain text files based on indentation
- Indent Finder always set the correct indentation for the file you are editing. It requires a Python enabled Vim.
- YAIFA is a VimL port of Indent Finder, so it doesn't need Python to work.
- DetectIndent automatically detects indent settings.
- IndentConsistencyCop checks the whole buffer or a given range of it for indentation consistency.
- Coding_style allows setting indent styles per project.
- vim-pasta allows for pasting with automatic adjusting of indentation to destination context.
- sleuth detects indent from existing files, or from other files of the same type in the same directory for new files.
The following should be merged to 224 (or maybe this tip), or deleted:
- 522 Fix an autoindent error Nice piece of code, but is the content worth keeping? I do not know what scenario it describes or what it achieves. JohnBeckett 10:06, 12 August 2009 (UTC)
- I think that's too involved for an introduction tip like this one. (Spiiph 08:57, October 14, 2009 (UTC))
- I agree that it shouldn't be in the tip proper, but we often have a "related plugins" section where people can add links to plugins related to the tip with a brief description. I have done this, above. We should probably also link to the plugnis that are mentioned in the tip here, for easy reference. --Fritzophrenic 13:44, October 14, 2009 (UTC)