Vim Tips Wiki
(move in "Indentation commands" from 83)
Line 104: Line 104:
   
 
==Comments==
 
==Comments==
  +
:With the recent additions, this article should be renamed '''Indentation commands'''. ([[User:Spiiph|Spiiph]] 00:31, September 2, 2009 (UTC))

Revision as of 00:31, 2 September 2009

Tip 224 Printable Monobook Previous Next

created 2002 · complexity basic · author niboan · version 6.0


Programmers often need to adjust indents (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.

Indenting 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).

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.

 TO DO 

  • Need info on commands like vaB, viB, >aB, >iB.
  • 597 Indent a code block Perhaps merge 597 to here, or use in "see also".

Indentation commands

Normal and visual mode commands

The << and >> commands that will shift lines one 'shiftwidth' to the left and to the right respectively. It can be used with a count. < and > do the same for motions, text objects and a visual selections. For all commands, the . command will repeat the operation on the entire range, enabling command combinations such as 3>>... to shift three lines four 'shiftwidth's to the right.

The == command automatically indents count lines, according to your indentation settings (see above). The = command does the same, but for motions, text objects and a 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
:update

or the buffer list, which might be more precarious

:bufdo normal gg=G
:update

Insert mode commands

In insert mode, CTRL-T inserts one 'shiftwidth' of indent at the start of the current line, and CTRL-D removes one. 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.

Ex commands

The :< and :> both take a range and can be appended by additional < and >, respectively, each adding a 'shiftwidth' of indentation.

Mappings

If you select some lines then press > to indent the lines, the selection is removed. It's best to use the techniques shown in the previous section, but if you want, you can use some mappings.

To retain the visual selection after pressing > or < (press these keys again to repeat the indent, or press Esc to cancel):

:vnoremap > >gv
:vnoremap < <gv

" Alternative using Tab/Shift-Tab (for gvim).
: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

References

Comments

With the recent additions, this article should be renamed Indentation commands. (Spiiph 00:31, September 2, 2009 (UTC))