Vim Tips Wiki
No edit summary
(→‎Comments: questions)
Line 126: Line 126:
 
</pre>
 
</pre>
 
-vimpire
 
-vimpire
  +
:Thanks but I'm a bit confused about some details and some explanation would be good.
  +
:Should the 'gb' in the first line be 'gv'?
  +
:I don't understand what the two 'map' commands operate on. The current line? Previously selected lines?
  +
:What are the commands like '0I' doing? Don't need '0' before 'I'? Use '_' (in normal mode) to jump to first nonblank character.
  +
:What does the 'imap' command operate on? The current line (should be uppercase 'V')? Why not use Ctrl-T or Ctrl-D?
  +
:[[User:JohnBeckett|JohnBeckett]] 11:29, July 3, 2011 (UTC)

Revision as of 11:29, 3 July 2011

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.

Indentation commands

Normal and visual mode commands

The << and >> commands shift lines one 'shiftwidth' to the left and to the right respectively. 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 3>>.... shifts three lines to the right, and the operation is repeated so that the three lines are shifted a total of five 'shiftwidth's.

The == command automatically indents count lines according to your indentation settings. 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 the buffer list (caution, every buffer will be affected):

:bufdo normal gg=G
:wall

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 :> commands take a range and can be appended by additional < and >, respectively, each adding a 'shiftwidth' of 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).

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.

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

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

" 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))

Yes, but I would like to wait a week or two until we have had a chance to fix the related indent articles. It's only after they are all in reasonable shape that we will have a good idea what to call them. JohnBeckett 08:35, September 2, 2009 (UTC)

What is :update doing in the #Normal and visual mode commands section (it occurs twice)? Was it intended to be part of the argdo/bufdo so each buffer is saved? I think it should just be deleted because it is not helpful and distracts from the point. (Yet another TODO: we need a link to a tip explaining that you would need :set hidden, and how to do :wa). JohnBeckett 08:35, September 2, 2009 (UTC)

Ooops, I probably meant to do :wall.
Hmmm. But if you show that, you really should show :set hidden as the first command? If we had a tip that gave a simple statement of the issues, I would recommend linking to that. Given that I don't think we do, why not either add the :set hidden, or delete the :wall on the basis that if someone doesn't know they need the latter, they won't know they need the former. JohnBeckett 11:59, September 2, 2009 (UTC)
You're right. How about changing the command to use :update after each reindentation instead? That should work regardless of options. (Spiiph 12:07, September 2, 2009 (UTC))

I have done a little tweaking. Later, I'll wonder about how all the indent tips appear. I wanted a tip somewhere which clearly shows how easy it is to visually select some lines and use the built-in procedures of < and > and . and u (and so point out that the many old tips on mappings using gv are misguided). That simple info is really all that most readers would ever use. At any rate, there is now some excellent information here. JohnBeckett 08:35, September 2, 2009 (UTC)

Ah, I might have misunderstood your intention when you moved the Indentation commands section here. I thought you wanted a more generic tip. Maybe we should create a whole new tip for that, and leave this one as it is? (Spiiph 10:14, September 2, 2009 (UTC))
My main concern is that the mappings go after the "proper" advice, which is how it appears now. I think it will be about a week before we are in a position to know how all the indent commands are going to end up, but the progress so far has been sensational (they have been an ugly mess for a long time).
I still need to do something with this 200906 proposal: Indent with tabs, align with spaces. I am inclined to merge it somewhere, and delete the three external links under "See also" as not very helpful. The issues are explained in Indenting source code, but perhaps a little extra from merging in this proposal would be useful? JohnBeckett 11:59, September 2, 2009 (UTC)

This is what I ended up with, & it is working awesome~

map <Tab> >gb
map <S-Tab> v<gv<ESC>0I<ESC>l
imap <S-Tab> <ESC>v<gv<ESC>0I
vnoremap <Tab> >gv
vnoremap <S-Tab> <gv

-vimpire

Thanks but I'm a bit confused about some details and some explanation would be good.
Should the 'gb' in the first line be 'gv'?
I don't understand what the two 'map' commands operate on. The current line? Previously selected lines?
What are the commands like '0I' doing? Don't need '0' before 'I'? Use '_' (in normal mode) to jump to first nonblank character.
What does the 'imap' command operate on? The current line (should be uppercase 'V')? Why not use Ctrl-T or Ctrl-D?
JohnBeckett 11:29, July 3, 2011 (UTC)