Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: e30f345e57
Fetching contributors…

Cannot retrieve contributors at this time

91 lines (70 sloc) 3.768 kb
<h3 id="indenting-lines">Indenting Lines</h3>
<h4>Problem</h4>
<p>You want to use whitespace (spaces or tabs) to indent lines from the left
margin.</p>
<p>For example, you may want to start your paragraphs with an indented first
line. Or, if you're writing program source code, you may want to visually
represent the structure of your program by using indentation to show
nesting.</p>
<h4>Solution</h4>
<p>To start a line indented, just press the tab key once for each level of
indentation. </p>
<p>To indent existing lines, highlight them <a
href="#visually-selecting-text">visually</a> and press <kbd>&gt;</kbd> to
indent or <kbd>&lt;</kbd> to unindent. </p>
<p>If you're in <i>Insert</i> or <i>Replace</i> mode you can use
<kbd><kbd>&lt;Ctrl&gt;</kbd>-<kbd>&lt;Shift&gt;</kbd>-<kbd>t</kbd></kbd> to
indent (mnemonic: <b>t</b>ab), and
<kbd><kbd>&lt;Ctrl&gt;</kbd>-<kbd>&lt;Shift&gt;</kbd>-<kbd>d</kbd></kbd> to
unident (mnemonic: <b>d</b>e-tab/indent).</p>
<p>More powerful are <a href="#selecting-text-with-motions">motions</a>
combined with indent/unident commands. The syntax is
<kbd><kbd>&gt;<var>motion</var></kbd></kbd> to indent the text described by
<var>motion</var>, and <kbd><kbd>&lt;<var>motion</var></kbd></kbd> to unident
it. For example, <kbd><kbd>&gt;ap</kbd></kbd> indents the current paragraph.
</p>
<h4>Description</h4>
<p>For many users, this solution will be sufficient. However, programmers
regularly need more control over indentation because it's so important to
their work.</p>
<blockquote class="tip">
<p>The <tt>:set list</tt> command makes literal tabs visible. It displays
them as <tt>^I</tt>, and uses a similar notation for other non-printable
characters. The <tt>$</tt> symbol is used to show the end of lines.</p>
</blockquote>
<p>A contentious issue among programmers involves how the tab key should work.
There are two main schools of thought:</p>
<ul>
<li>Literal tabs - Each press of the tab key should insert a literal tab
character (padding with spaces if necessary). To achieve this: <tt>:set
tabstop=8</tt> (the default), <tt>:set softtabstop=8</tt>, <tt>:set
shiftwidth=8</tt>, and <tt>:set noexpandtab</tt>. Tabs are now eight
columns wide; each indentation level is a single tab.</li>
<li>Convert tabs to spaces - Each press of the tab key should insert a certain
number of spaces. The settings you need are <tt>:set tabstop=8</tt>,
<tt>:set shiftwidth=4</tt>, and <tt>:set expandtab</tt>. Tabs are now
replaced with 4 spaces. Real tabs are displayed 8 character wide, spaced
tabs 4 characters wide.</li>
</ul>
<p><tt>shiftwidth</tt> controls how many spaces are inserted when using the
<kbd><kbd>&gt;&gt;</kbd></kbd>/<kbd><kbd>&lt;&lt;</kbd></kbd> technique
described above, or the automatic indenting used with source code.</p>
<p><tt>softtabstop</tt> specifies how many columns Vim uses when
<kbd><kbd>Tab</kbd></kbd> is hit in Insert mode. If it's less than
<tt>tabstop</tt>, and Vim's not expanding tabs (<tt>:set
noexpandtab</tt>), Vim indents with tabs, padding with spaces where
necessary.</p>
<p>(It can be seen, then, that you'll typically want to make
<tt>softtabstop</tt> and <tt>shiftwidth</tt> equal, for reasons of
consistency and sanity.)</p>
<p>The boolean <tt>expandtab</tt> option replaces tabs with spaces if true;
leaves them alone if false.</p>
<p>These settings are not retroactive. To make an existing file honour your
indentation preferences use the <tt>:retab!</tt> command.</p>
<h4>See Also</h4>
<ul>
<li><a href="http://www.jwz.org/doc/tabs-vs-spaces.html">Tabs versus Spaces:
An Eternal Holy War</a> by Jamie Zawinski.</li>
<li><a href="http://www.tedlogan.com/techblog3.html">Secrets of Tabs in
Vim</a> by Ted Logan.</li>
</ul>
Jump to Line
Something went wrong with that request. Please try again.