Vim Tips Wiki
(13 intermediate revisions by 7 users not shown)
Line 5: Line 5:
 
|created=2002
 
|created=2002
 
|complexity=basic
 
|complexity=basic
|author=niboan
+
|author=
 
|version=6.0
 
|version=6.0
 
|rating=272/109
 
|rating=272/109
Line 11: Line 11:
 
|category2=
 
|category2=
 
}}
 
}}
Programmers often need to adjust indents (amount of whitespace before the text on a line). This tip shows how it's done.
+
Programmers often need to adjust indents (the amount of whitespace before the text on a line). This tip shows how it's done.
   
 
See [[VimTip83|Indenting source code]] for related information, including settings that affect indentation.
 
See [[VimTip83|Indenting source code]] for related information, including settings that affect indentation.
   
==Indentation commands==
+
==Basic commands==
  +
In normal mode, type <code>>></code> to indent the current line, or <code><<</code> to unindent. Each command can be used with a count. The operators <code>></code> and <code><</code> do the same for motions, text objects and visual selections. For all commands, pressing <code>.</code> repeats the operation.
===Normal and visual mode commands===
 
The <tt>&lt;&lt;</tt> and <tt>&gt;&gt;</tt> commands that will shift lines one <tt>'shiftwidth'</tt> to the left and to the right respectively. It can be used with a count.
 
<tt>&lt;</tt> and <tt>&gt;</tt> do the same for motions, text objects and a visual selections.
 
For all commands, the <tt>.</tt> command will repeat the operation on the entire range, enabling command combinations such as <tt>3>>...</tt> to shift three lines four <tt>'shiftwidth'</tt>s to the right.
 
   
  +
For example, typing <code>5>>..</code> shifts five lines to the right, and then repeats the operation twice so that the five lines are shifted three times.
The <tt>==</tt> command automatically indents count lines, according to your indentation settings (see above). The <tt>=</tt> command does the same, but for motions, text objects and a visual selections.
 
   
  +
In insert mode, <code>Ctrl-T</code> indents the current line, and <code>Ctrl-D</code> unindents.
To reindent an entire buffer, use <tt>gg=G</tt>.
 
   
  +
When indenting or unindenting, lines are shifted one <code>'shiftwidth'</code> to the right or left.
To reindent many files, the argument list can be used
 
   
 
===Basic examples===
 
To adjust the indent on three lines:
 
*Put the cursor anywhere in the first line.
 
*Press <code>V</code> then <code>jj</code> to visually select the three lines.
 
*Press <code>></code> to indent (shift text one '<code>shiftwidth</code>' to the right), or press <code><</code> to shift left.
 
*Press <code>.</code> to repeat the indent, or <code>u</code> to undo if you have shifted too far.
 
*Type <code>gv</code> if you want to reselect the lines (not needed).
  +
  +
Alternatively, if you know that you want to adjust three lines, you can simply:
 
*Type <code>3>></code> to shift right or <code>3<<</code> to shift left.
 
Or:
 
*Type <code>>2j</code> to shift right or <code><2j</code> to shift left.
  +
  +
As mentioned above, the <code>></code> and <code><</code> commands combine with arbitrary Vim movements and text objects. For example, <code>>}</code> to indent from the cursor to the next blank line, or <code><aB</code> to un-indent the current C-like {...} "block" structure. See [[indent a code block]].
  +
 
==More commands==
 
In normal mode, type <code>==</code> to automatically indent the current line according to your [[VimTip83|indentation settings]]. This command can be used with a count. The <code>=</code> command does the same, but for motions, text objects and visual selections.
  +
 
To reindent an entire buffer, use <code>gg=G</code>.
  +
 
To reindent many files, the argument list can be used:
 
<pre>
 
<pre>
 
:args *.c
 
:args *.c
 
:argdo normal gg=G
 
:argdo normal gg=G
  +
:wall
:update
 
 
</pre>
 
</pre>
   
or the buffer list, which might be more precarious
+
Or use the buffer list (caution, every buffer will be affected):
 
 
<pre>
 
<pre>
 
:bufdo normal gg=G
 
:bufdo normal gg=G
  +
:wall
:update
 
 
</pre>
 
</pre>
   
  +
In insert mode, <code>0&nbsp;Ctrl-D</code> removes all indentation on the current line, and <code>^&nbsp;Ctrl-D</code> does the same, but restores the original level of indentation for this line on the next line. When using <code>'cindent'</code> or file type based indentation, <code>Ctrl-F</code> reindents the current line, like <code>==</code> in normal mode.
===Insert mode commands===
 
In insert mode, <tt>CTRL-T</tt> inserts one <tt>'shiftwidth'</tt> of indent at the start of the current line, and <tt>CTRL-D</tt> removes one. <tt>0&nbsp;CTRL-D</tt> removes all indentation on the current line, and <tt>^&nbsp;CTRL-D</tt> does the same, but restores the original level of indentation for this line on the next line.
 
   
  +
The <code>:></code> and <code>:<</code> commands take a range, and additional <code>></code> or <code><</code> can be used. For example, <code>:12,20>>></code> indents lines 12 to 20 inclusive three times (adding three times <code>'shiftwidth'</code> of indentation to the specified lines).
When using <tt>'cindent'</tt> or file type based indentation, <tt>CTRL-F</tt> reindents the current line, like <tt>==</tt> in normal mode.
 
   
===Ex commands===
+
===More examples===
 
To set the indent on three lines to 12 spaces (or an equivalent mixture of tabs/spaces, depending on '<code>expandtab</code>'):
The <tt>:&lt;</tt> and <tt>:&gt;</tt> both take a range and can be appended by additional <tt>&lt;</tt> and <tt>&gt;</tt>, respectively, each adding a <tt>'shiftwidth'</tt> of indentation.
 
 
==Indenting examples==
 
To adjust the indent on three lines:
 
 
*Put the cursor anywhere in the first line.
 
*Put the cursor anywhere in the first line.
*Press <tt>V</tt> then <tt>jj</tt> to visually select the three lines.
+
*Press <code>V</code> then <code>jj</code> to visually select the three lines.
 
*Type <code>:le 12</code> then press Enter (abbreviation for <code>:left 12</code>).
*Press <tt>></tt> to indent (shift text one '<tt>shiftwidth</tt>' to the right), or press <tt><</tt> to shift left.
 
*Press <tt>.</tt> to repeat the indent, or <tt>u</tt> to undo if you have shifted too far.
 
*Type <tt>gv</tt> if you want to reselect the lines (not needed).
 
Or:
 
* <tt>3>></tt> to shift right or <tt>3<<</tt> to shift left
 
Or:
 
* <tt>>2j</tt> to shift right or <tt><2j</tt> to shift left
 
To set the indent on three lines to 12 spaces (or an equivalent mixture of tabs/spaces, depending on '<tt>expandtab</tt>'):
 
*Put the cursor anywhere in the first line.
 
*Press <tt>V</tt> then <tt>jj</tt> to visually select the three lines.
 
*Type <tt>:le 12</tt> then press Enter (abbreviation for <tt>:left 12</tt>).
 
   
To remove all indents in a selected region, type <tt>:le</tt> then press Enter.
+
To remove all indents in a selected region, type <code>:le</code> then press Enter.
   
 
To apply automatic indentation (this requires suitable indent rules for your file type):
 
To apply automatic indentation (this requires suitable indent rules for your file type):
*Type <tt>==</tt> to indent the current line.
+
*Type <code>==</code> to indent the current line.
*Type a number then <tt>==</tt> to indent that many lines, starting from the cursor.
+
*Type a number then <code>==</code> to indent that many lines, starting from the cursor.
*Press <tt>V</tt> then move the cursor to select a range of lines, then press <tt>=</tt> to indent the selection.
+
*Press <code>V</code> then move the cursor to select a range of lines, then press <code>=</code> to indent the selection.
 
{{todo}}
 
*Need info on commands like <tt>vaB</tt>, <tt>viB</tt>, <tt>>aB</tt>, <tt>>iB</tt>.
 
*[[VimTip597|597 Indent a code block]] Perhaps merge 597 to here, or use in "see also".
 
   
 
==Mappings==
 
==Mappings==
If you select some lines then press <tt>></tt> to indent the lines, the selection is removed. The indentation can be repeated on the same range using <tt>.</tt>, but if you still want to retain the visual selection after having pressed <tt>></tt> or <tt><</tt>, you can use these mappings
+
If you select some lines then press <code>></code> to indent the lines, the selection is removed. The indentation can be repeated on the same range using <code>.</code>, but if you still want to retain the visual selection after having pressed <code>></code> or <code><</code>, you can use these mappings
 
 
<pre>
 
<pre>
:vnoremap > >gv
+
vnoremap > >gv
:vnoremap < <gv
+
vnoremap < <gv
  +
</pre>
   
  +
Following is a more elaborate set of mappings (mapping Shift-Tab will probably only work on gvim). In normal mode, press Tab or Shift-Tab to adjust the indent on the current line and position the cursor on the first nonblank character; in insert mode, press Shift-Tab to unindent; in visual mode, press Tab or Shift-Tab to adjust the indent on selected lines.
" Alternative using Tab/Shift-Tab (for gvim).
 
  +
<pre>
:vnoremap <Tab> >gv
 
:vnoremap <S-Tab> <gv
+
nnoremap <Tab> >>_
  +
nnoremap <S-Tab> <<_
  +
inoremap <S-Tab> <C-D>
 
vnoremap <Tab> >gv
  +
vnoremap <S-Tab> <gv
 
</pre>
 
</pre>
   
An alternative for anyone using <tt>:behave mswin</tt>, is to select lines by holding down Shift and pressing the cursor down or up arrow keys. However, in select mode, if you press <tt>></tt>, the selected text will be replaced with '>'. Instead, you can use Tab to increase the indent, and Shift-Tab to decrease it, with these mappings:
+
An alternative for anyone using <code>:behave mswin</code>, is to select lines by holding down Shift and pressing the cursor down or up arrow keys. However, in select mode, if you press <code>></code>, the selected text will be replaced with '<code>></code>'. Instead, you can use Tab to increase the indent, and Shift-Tab to decrease it, with these mappings:
 
<pre>
 
<pre>
:vnoremap <Tab> >
+
vnoremap <Tab> >
:vnoremap <S-Tab> <
+
vnoremap <S-Tab> <
 
</pre>
 
</pre>
   
 
In select mode, visual-mode mappings temporarily set visual mode ({{help|Select-mode-mapping}}).Also, the select mode will be retained. You could use the following alternative if you want to exit from select mode after pressing Tab or Shift-Tab:
 
In select mode, visual-mode mappings temporarily set visual mode ({{help|Select-mode-mapping}}).Also, the select mode will be retained. You could use the following alternative if you want to exit from select mode after pressing Tab or Shift-Tab:
 
<pre>
 
<pre>
:vnoremap <Tab> >gV
+
vnoremap <Tab> >gV
:vnoremap <S-Tab> <gV
+
vnoremap <S-Tab> <gV
 
</pre>
 
</pre>
  +
  +
==See also==
 
*[[VimTip597|597 Indent a code block]] indenting a code block (<code>{...}</code>)
   
 
==References==
 
==References==
Line 106: Line 112:
   
 
==Comments==
 
==Comments==
  +
Where <code>:wall</code> is used, should mention <code>:set hidden</code>. We should have a tip on that so we can just link to it. [[User:JohnBeckett|JohnBeckett]] 04:33, June 28, 2012 (UTC)
With the recent additions, this article should be renamed '''Indentation commands'''. ([[User:Spiiph|Spiiph]] 00:31, September 2, 2009 (UTC))
 

Revision as of 23:24, 20 October 2014

Tip 224 Printable Monobook Previous Next

created 2002 · complexity basic · version 6.0


Programmers often need to adjust indents (the amount of whitespace before the text on a line). This tip shows how it's done.

See Indenting source code for related information, including settings that affect indentation.

Basic commands

In normal mode, type >> to indent the current line, or << to unindent. Each command can be used with a count. The operators > and < do the same for motions, text objects and visual selections. For all commands, pressing . repeats the operation.

For example, typing 5>>.. shifts five lines to the right, and then repeats the operation twice so that the five lines are shifted three times.

In insert mode, Ctrl-T indents the current line, and Ctrl-D unindents.

When indenting or unindenting, lines are shifted one 'shiftwidth' to the right or left.

Basic examples

To adjust the indent on three lines:

  • Put the cursor anywhere in the first line.
  • Press V then jj to visually select the three lines.
  • Press > to indent (shift text one 'shiftwidth' to the right), or press < to shift left.
  • Press . to repeat the indent, or u to undo if you have shifted too far.
  • Type gv if you want to reselect the lines (not needed).

Alternatively, if you know that you want to adjust three lines, you can simply:

  • Type 3>> to shift right or 3<< to shift left.

Or:

  • Type >2j to shift right or <2j to shift left.

As mentioned above, the > and < commands combine with arbitrary Vim movements and text objects. For example, >} to indent from the cursor to the next blank line, or <aB to un-indent the current C-like {...} "block" structure. See indent a code block.

More commands

In normal mode, type == to automatically indent the current line according to your indentation settings. This command can be used with a count. The = command does the same, but for motions, text objects and visual selections.

To reindent an entire buffer, use gg=G.

To reindent many files, the argument list can be used:

:args *.c
:argdo normal gg=G
:wall

Or use the buffer list (caution, every buffer will be affected):

:bufdo normal gg=G
:wall

In insert mode, 0 Ctrl-D removes all indentation on the current line, and ^ Ctrl-D does the same, but restores the original level of indentation for this line on the next line. When using 'cindent' or file type based indentation, Ctrl-F reindents the current line, like == in normal mode.

The :> and :< commands take a range, and additional > or < can be used. For example, :12,20>>> indents lines 12 to 20 inclusive three times (adding three times 'shiftwidth' of indentation to the specified lines).

More examples

To set the indent on three lines to 12 spaces (or an equivalent mixture of tabs/spaces, depending on 'expandtab'):

  • Put the cursor anywhere in the first line.
  • Press V then jj to visually select the three lines.
  • Type :le 12 then press Enter (abbreviation for :left 12).

To remove all indents in a selected region, type :le then press Enter.

To apply automatic indentation (this requires suitable indent rules for your file type):

  • Type == to indent the current line.
  • Type a number then == to indent that many lines, starting from the cursor.
  • Press V then move the cursor to select a range of lines, then press = to indent the selection.

Mappings

If you select some lines then press > to indent the lines, the selection is removed. The indentation can be repeated on the same range using ., but if you still want to retain the visual selection after having pressed > or <, you can use these mappings

vnoremap > >gv
vnoremap < <gv

Following is a more elaborate set of mappings (mapping Shift-Tab will probably only work on gvim). In normal mode, press Tab or Shift-Tab to adjust the indent on the current line and position the cursor on the first nonblank character; in insert mode, press Shift-Tab to unindent; in visual mode, press Tab or Shift-Tab to adjust the indent on selected lines.

nnoremap <Tab> >>_
nnoremap <S-Tab> <<_
inoremap <S-Tab> <C-D>
vnoremap <Tab> >gv
vnoremap <S-Tab> <gv

An alternative for anyone using :behave mswin, is to select lines by holding down Shift and pressing the cursor down or up arrow keys. However, in select mode, if you press >, the selected text will be replaced with '>'. Instead, you can use Tab to increase the indent, and Shift-Tab to decrease it, with these mappings:

vnoremap <Tab> >
vnoremap <S-Tab> <

In select mode, visual-mode mappings temporarily set visual mode (:help Select-mode-mapping).Also, the select mode will be retained. You could use the following alternative if you want to exit from select mode after pressing Tab or Shift-Tab:

vnoremap <Tab> >gV
vnoremap <S-Tab> <gV

See also

References

Comments

Where :wall is used, should mention :set hidden. We should have a tip on that so we can just link to it. JohnBeckett 04:33, June 28, 2012 (UTC)