Vim Tips Wiki
Tags: Visual edit apiedit rollback
(21 intermediate revisions by 15 users not shown)
Line 16: Line 16:
 
*[[VimTip224|Shifting blocks visually]] commands to change indents
 
*[[VimTip224|Shifting blocks visually]] commands to change indents
 
*[[VimTip330|How to stop auto indenting]] how to stop your indents from being changed
 
*[[VimTip330|How to stop auto indenting]] how to stop your indents from being changed
*[[VimTip644|Restoring indent after typing hash]] problems when typing <tt>#</tt>
+
*[[VimTip644|Restoring indent after typing hash]] problems when typing <code>#</code>
 
*[[VimTip906|Toggle auto-indenting for code paste]] to avoid broken indents when pasting in a terminal
 
*[[VimTip906|Toggle auto-indenting for code paste]] to avoid broken indents when pasting in a terminal
   
 
==Setup==
 
==Setup==
 
===Indentation without hard tabs===
 
===Indentation without hard tabs===
For indentation without tabs, the principle is to set <tt>'expandtab'</tt>, and set <tt>'shiftwidth'</tt> and <tt>'softtabstop'</tt> to the same value, while leaving <tt>'tabstop'</tt> at its default value:
+
For indentation without tabs, the principle is to set <code>'expandtab'</code>, and set <code>'shiftwidth'</code> and <code>'softtabstop'</code> to the same value, while leaving <code>'tabstop'</code> at its default value:
 
<pre>
 
<pre>
 
set expandtab
 
set expandtab
Line 29: Line 29:
   
 
These settings will result in spaces being used for all indentation.
 
These settings will result in spaces being used for all indentation.
adfasdfafsdfsdfsfsfsfsfsfsfsfsfsfsfsfsfsfsf
 
   
 
===Indentation purely with hard tabs===
 
===Indentation purely with hard tabs===
For indentation purely with hard tabs, the principle is to set <tt>'tabstop'</tt> and <tt>'shiftwidth'</tt> to the same value, and to leave <tt>'expandtab'</tt> at its default value (<tt>'noexpandtab'</tt>), and leave <tt>'softtabstop'</tt> unset:
+
For indentation purely with hard tabs, the principle is to set <code>'tabstop'</code> and <code>'shiftwidth'</code> to the same value, and to leave <code>'expandtab'</code> at its default value (<code>'noexpandtab'</code>), and leave <code>'softtabstop'</code> unset:
 
<pre>
 
<pre>
 
set shiftwidth=2
 
set shiftwidth=2
Line 41: Line 40:
   
 
===Indentation with mixed tabs and spaces===
 
===Indentation with mixed tabs and spaces===
For indentation with mixed tabs and spaces, the principle is to set <tt>'shiftwidth'</tt> and <tt>'softtabstop'</tt> to the same value, leave <tt>'expandtab'</tt> at its default (<tt>'noexpandtab'</tt>). Usually, <tt>'tabstop'</tt> is left at its default value:
+
For indentation with mixed tabs and spaces, the principle is to set <code>'shiftwidth'</code> and <code>'softtabstop'</code> to the same value, leave <code>'expandtab'</code> at its default (<code>'noexpandtab'</code>). Usually, <code>'tabstop'</code> is left at its default value:
 
<pre>
 
<pre>
 
set shiftwidth=2
 
set shiftwidth=2
Line 50: Line 49:
   
 
===Explanation of the options===
 
===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 [[vimrc]]s out there that will tell you how theirs is the proper setup. More often than not, ''they are misguided''. Let <tt>:help</tt> be your reference, and this page be your guide to Vim's indentation options.
+
Vim's different indentation options are an endless source of grief and confusion for new users. There are plenty of example [[vimrc]]s out there that will tell you how theirs is the proper setup. More often than not, ''they are misguided''. Let <code>:help</code> be your reference, and this page be your guide to Vim's indentation options.
*<tt>{{help|prefix=no|'tabstop'}}</tt> changes the width of the <tt>TAB</tt> character, plain and simple.
+
*<code>{{help|prefix=no|'tabstop'}}</code> changes the width of the <code>TAB</code> character, plain and simple.
*<tt>{{help|prefix=no|'softtabstop'}}</tt> affects what happens when you press the <tt><TAB></tt> or <tt><BS></tt> keys. Its default value is the same as the value of <tt>'tabstop'</tt>, but when using indentation without hard tabs or mixed indentation, you want to set it to the same value as <tt>'shiftwidth'</tt>. If <tt>'expandtab'</tt> is unset, and <tt>'tabstop'</tt> is different from <tt>'softtabstop'</tt>, the <tt><TAB></tt> key will minimize the amount of spaces inserted by using multiples of <tt>TAB</tt> characters. For instance, if <tt>'tabstop'</tt> is 8, and the amount of consecutive space inserted is 20, two <tt>TAB</tt> characters and four spaces will be used.
+
*<code>{{help|prefix=no|'softtabstop'}}</code> affects what happens when you press the <code><TAB></code> or <code><BS></code> keys. Its default value is the same as the value of <code>'tabstop'</code>, but when using indentation without hard tabs or mixed indentation, you want to set it to the same value as <code>'shiftwidth'</code>. If <code>'expandtab'</code> is unset, and <code>'tabstop'</code> is different from <code>'softtabstop'</code>, the <code><TAB></code> key will minimize the amount of spaces inserted by using multiples of <code>TAB</code> characters. For instance, if <code>'tabstop'</code> is 8, and the amount of consecutive space inserted is 20, two <code>TAB</code> characters and four spaces will be used.
*<tt>{{help|prefix=no|'shiftwidth'}}</tt> affects what happens when you press <tt>>></tt>, <tt><<</tt> or <tt>==</tt>. It also affects how automatic indentation works. (See below.)
+
*<code>{{help|prefix=no|'shiftwidth'}}</code> affects what happens when you press <code>>></code>, <code><<</code> or <code>==</code>. It also affects how automatic indentation works. (See below.)
*<tt>{{help|prefix=no|'expandtab'}}</tt> affects what happens when you press the <tt><TAB></tt> key. If <tt>'expandtab'</tt> is set, pressing the <tt><TAB></tt> key will always insert <tt>'softtabstop'</tt> amount of space characters. Otherwise, the amount of spaces inserted is minimized by using <tt>TAB</tt> characters.
+
*<code>{{help|prefix=no|'expandtab'}}</code> affects what happens when you press the <code><TAB></code> key. If <code>'expandtab'</code> is set, pressing the <code><TAB></code> key will always insert <code>'softtabstop'</code> amount of space characters. Otherwise, the amount of spaces inserted is minimized by using <code>TAB</code> characters.
*<tt>{{help|prefix=no|'smarttab'}}</tt> affects how <tt><TAB></tt> key presses are interpreted depending on where the cursor is. If <tt>'smarttab'</tt> is on, a <tt><TAB></tt> key inserts indentation according to <tt>'shiftwidth'</tt> at the beginning of the line, whereas <tt>'tabstop'</tt> and <tt>'softtabstop'</tt> are used elsewhere. There is seldom any need to set this option, unless it is necessary to use hard <tt>TAB</tt> characters in body text or code.
+
*<code>{{help|prefix=no|'smarttab'}}</code> affects how <code><TAB></code> key presses are interpreted depending on where the cursor is. If <code>'smarttab'</code> is on, a <code><TAB></code> key inserts indentation according to <code>'shiftwidth'</code> at the beginning of the line, whereas <code>'tabstop'</code> and <code>'softtabstop'</code> are used elsewhere. There is seldom any need to set this option, unless it is necessary to use hard <code>TAB</code> characters in body text or code.
   
 
===Considerations===
 
===Considerations===
Using a <tt>'tabstop'</tt> value other than the default (8 spaces), will result in your file having a different appearance when using tools such as <tt>cat</tt> (<tt>type</tt> 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).
+
Using a <code>'tabstop'</code> value other than the default (8 spaces), will result in your file having a different appearance when using tools such as <code>cat</code> (<code>type</code> 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==
 
==Methods for automatic indentation==
There are a number of methods enabling automatic indentation in Vim, ranging from fairly "stupid" and unintrusive ones, like <tt>'autoindent'</tt> and <tt>'smartindent'</tt>, to complex ones such as <tt>'cindent'</tt> and custom indentation based on filetype using <tt>'indentexpr'</tt>. The amount of indentation used for one level is controlled by the <tt>'shiftwidth'</tt> option. (See above.)
+
There are a number of methods enabling automatic indentation in Vim, ranging from fairly "stupid" and unintrusive ones, like <code>'autoindent'</code> and <code>'smartindent'</code>, to complex ones such as <code>'cindent'</code> and custom indentation based on filetype using <code>'indentexpr'</code>. The amount of indentation used for one level is controlled by the <code>'shiftwidth'</code> option. (See above.)
   
===<tt>'autoindent'</tt>===
+
===<code>'autoindent'</code>===
<tt>'autoindent'</tt> 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.
+
<code>'autoindent'</code> 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.
   
<tt>'autoindent'</tt> does not interfere with other indentation settings, and some file type based indentation scripts even enable it automatically.
+
<code>'autoindent'</code> does not interfere with other indentation settings, and some file type based indentation scripts even enable it automatically.
   
===<tt>'smartindent'</tt> and <tt>'cindent'</tt>===
+
===<code>'smartindent'</code> and <code>'cindent'</code>===
<tt>'smartindent'</tt> automatically inserts one extra level of indentation in some cases, and works for C-like files. <tt>'cindent'</tt> is more customizable, but also more strict when it comes to syntax.
+
<code>'smartindent'</code> automatically inserts one extra level of indentation in some cases, and works for C-like files. <code>'cindent'</code> is more customizable, but also more strict when it comes to syntax.
   
<tt>'smartindent'</tt> and <tt>'cindent'</tt> might interfere with file type based indentation, and should never be used in conjunction with it.
+
<code>'smartindent'</code> and <code>'cindent'</code> 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 <tt>'cindent'</tt>, and for that reason, there is no need to set <tt>'cindent'</tt> manually for such files. In these cases, the <tt>'cinwords'</tt>, <tt>'cinkeys'</tt> and <tt>'cinoptions'</tt> options still apply.
+
When it comes to C and C++, file type based indentations automatically sets <code>'cindent'</code>, and for that reason, there is no need to set <code>'cindent'</code> manually for such files. In these cases, the <code>'cinwords'</code>, <code>'cinkeys'</code> and <code>'cinoptions'</code> options still apply.
   
Generally, <tt>'smartindent'</tt> or <tt>'cindent'</tt> should only be set manually if you're not satisfied with how file type based indentation works.
+
Generally, <code>'smartindent'</code> or <code>'cindent'</code> should only be set manually if you're not satisfied with how file type based indentation works.
   
===File type based indentation===
+
===File-type based indentation===
This type of indentation is the most flexible, as it allows users to customize indentation per file type. For instance, the indentation scripts for C and C++ file types properly set the <tt>'cindent'</tt> 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 <tt>Makefile</tt>s without interference!
 
   
  +
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:
If you plan on using file type based indentation, don't set <tt>'smartindent'</tt> or <tt>'cindent'</tt>. You may still set <tt>'autoindent'</tt>, since it doesn't interfere.
 
  +
<code> filetype plugin indent on </code>. This command will use indentation scripts located in the <code>indent</code> folder of your vim installation.
   
 
For instance, the indentation scripts for C and C++ file types properly set the <code>'cindent'</code> 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 <code>Makefile</code>s without interference!
The <tt>vimrc_example.vim</tt> that ships with Vim enables filetype based indentation:
 
  +
 
If you plan on using file type based indentation, don't set <code>'smartindent'</code> or <code>'cindent'</code>. You may still set <code>'autoindent'</code>, since it doesn't interfere.
  +
 
The <code>vimrc_example.vim</code> that ships with Vim enables filetype based indentation:
 
<pre>
 
<pre>
 
if has("autocmd")
 
if has("autocmd")
Line 100: Line 103:
 
</pre>
 
</pre>
   
Create file <tt>html.vim</tt> with contents:
+
Create file <code>html.vim</code> with contents:
 
<pre>
 
<pre>
 
setlocal shiftwidth=2
 
setlocal shiftwidth=2
Line 106: Line 109:
 
</pre>
 
</pre>
   
and file <tt>python.vim</tt> with contents:
+
and file <code>python.vim</code> with contents:
 
<pre>
 
<pre>
 
setlocal expandtab
 
setlocal expandtab
Line 113: Line 116:
 
</pre>
 
</pre>
   
The <tt>html.vim</tt> and <tt>python.vim</tt> files should be in this directory (which you may need to create):
+
The <code>html.vim</code> and <code>python.vim</code> files should be in this directory (which you may need to create):
*<tt>~/.vim/after/ftplugin</tt> on Unix-based systems; or
+
*<code>~/.vim/after/ftplugin</code> on Unix-based systems; or
*<tt>$HOME/vimfiles/after/ftplugin</tt> on Windows systems
+
*<code>$HOME/vimfiles/after/ftplugin</code> on Windows systems
   
The standard plugins probably do not change settings such as <tt>shiftwidth</tt>, and in that case the directory <tt>~/.vim/ftplugin</tt> (or <tt>$HOME/vimfiles/ftplugin</tt>) would work as an alternative. However the "after" directory should be used because you intend to override settings from other plugins.
+
The standard plugins probably do not change settings such as <code>shiftwidth</code>, and in that case the directory <code>~/.vim/ftplugin</code> (or <code>$HOME/vimfiles/ftplugin</code>) 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:
 
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:
Line 124: Line 127:
 
autocmd FileType python setlocal expandtab shiftwidth=4 softtabstop=4
 
autocmd FileType python setlocal expandtab shiftwidth=4 softtabstop=4
 
</pre>
 
</pre>
 
 
 
   
 
==References==
 
==References==
Line 167: Line 167:
   
 
==Related plugins==
 
==Related plugins==
* {{script|id=513|text=Indent Finder}} always set the correct indentation for the file you are editing. It requires a Python enabled Vim.
+
*{{script|id=513|text=Indent Finder}} always set the correct indentation for the file you are editing. It requires a Python enabled Vim.
* {{script|id=3096|text=YAIFA}} is a VimL port of {{script|id=513|text=Indent Finder}}, so it doesn't need Python to work.
+
*{{script|id=3096|text=YAIFA}} is a VimL port of {{script|id=513|text=Indent Finder}}, so it doesn't need Python to work.
* {{script|id=1171|text=DetectIndent}} automatically detects indent settings.
+
*{{script|id=1171|text=DetectIndent}} automatically detects indent settings.
* {{script|id=1690|text=IndentConsistencyCop}} checks the whole buffer or a given range of it for indentation consistency.
+
*{{script|id=1690|text=IndentConsistencyCop}} checks the whole buffer or a given range of it for indentation consistency.
* {{script|id=2633|text=Coding_style}} allows setting indent styles per project.
+
*{{script|id=2633|text=Coding_style}} allows setting indent styles per project.
  +
*[https://github.com/sickill/vim-pasta vim-pasta] allows for pasting with automatic adjusting of indentation to destination context.
  +
*{{script|id=4375|text=sleuth}} detects indent from existing files, or from other files of the same type in the same directory for new files.
   
 
==Comments==
 
==Comments==

Revision as of 14:41, 19 February 2015

Tip 83 Printable Monobook Previous Next

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:

Setup

Indentation without hard tabs

For indentation without tabs, the principle is to set 'expandtab', and set 'shiftwidth' and '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 'tabstop' and 'shiftwidth' to the same value, and to leave 'expandtab' at its default value ('noexpandtab'), and leave 'softtabstop' unset:

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 'shiftwidth' and 'softtabstop' to the same value, leave 'expandtab' at its default ('noexpandtab'). Usually, '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 TAB character, plain and simple.
  • 'softtabstop' affects what happens when you press the <TAB> or <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 'shiftwidth'. If 'expandtab' is unset, and 'tabstop' is different from 'softtabstop', the <TAB> key will minimize the amount of spaces inserted by using multiples of TAB characters. For instance, if 'tabstop' is 8, and the amount of consecutive space inserted is 20, two TAB characters and four spaces will be used.
  • 'shiftwidth' affects what happens when you press >>, << or ==. It also affects how automatic indentation works. (See below.)
  • 'expandtab' affects what happens when you press the <TAB> key. If '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 characters.
  • 'smarttab' affects how <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 'tabstop' and 'softtabstop' are used elsewhere. There is seldom any need to set this option, unless it is necessary to use hard TAB characters in body text or code.

Considerations

Using a 'tabstop' value other than the default (8 spaces), will result in your file having a different appearance when using tools such as cat (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 'autoindent' and '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'

'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' and 'cindent'

'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.

'smartindent' and '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 'cinwords', 'cinkeys' and 'cinoptions' options still apply.

Generally, 'smartindent' or '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 'smartindent' or 'cindent'. You may still set 'autoindent', since it doesn't interfere.

The 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

Create file html.vim with contents:

setlocal shiftwidth=2
setlocal tabstop=2

and file python.vim with contents:

setlocal expandtab
setlocal shiftwidth=4
setlocal softtabstop=4

The html.vim and python.vim files should be in this directory (which you may need to create):

  • ~/.vim/after/ftplugin on Unix-based systems; or
  • $HOME/vimfiles/after/ftplugin on Windows systems

The standard plugins probably do not change settings such as shiftwidth, and in that case the directory ~/.vim/ftplugin (or $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

References

See also

 TO DO 

  • Merge some of following tips.

Indenting code

Auto indent

Select a block of lines having the same indent

Other

Related plugins

  • 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.

Comments

The following should be merged to 224 (or maybe this tip), or deleted:

The coding_style plugin allows to set indent styles per project. Maybe it's worth mentionning here? Chikamichi 22:22, September 12, 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)