Vim Tips Wiki
(cleanup and details)
(Change <tt> to <code>, perhaps also minor tweak.)
(10 intermediate revisions by 7 users not shown)
Line 1: Line 1:
  +
{{TipImported
It's possible to reformat an entire file, or a section of a file, using vim's built-in <tt>=</tt> filter.
 
  +
|id=977
  +
|previous=976
  +
|next=978
  +
|created=2005
  +
|complexity=basic
  +
|author=
  +
|version=6.0
  +
|rating=90/74
 
|category1=Getting started
  +
|category2=Indenting
  +
}}
  +
It's possible to reformat an entire file, or a section of a file, using Vim's built-in {{help|prefix=no|id==}} filter. Vim veterans often find this operator to be one of the most useful in their ''repertoire'', but so common that it becomes second-nature and is rarely mentioned.
   
In '''''normal mode''''', typing <tt>gg=G</tt> will reindent the entire file. This is a special case; <tt>=</tt> is an operator. Just like <tt>d</tt> or <tt>y</tt>, it will act on any text that you move over with a cursor motion command. In this case, <tt>gg</tt> positions the cursor on the first line, then <tt>=G</tt> re-indents from the current cursor position to the end of the buffer.
+
In normal mode, typing <code>gg=G</code> will reindent the entire file. This is a special case; <code>=</code> is an operator. Just like <code>d</code> or <code>y</code>, it will act on any text that you move over with a cursor motion command. In this case, <code>gg</code> positions the cursor on the first line, then <code>=G</code> re-indents from the current cursor position to the end of the buffer.
   
In '''''visual mode''''', typing <tt>=</tt> will fix indentation of the current section. Thus, an equivalent but less efficient command to accomplish the same as <tt>gg=G</tt> in normal mode, would be <tt>ggVG</tt> to select the entire buffer in visual mode, followed by <tt>=</tt> to reindent the entire selection.
+
In visual mode, typing <code>=</code> will fix indentation of the current section. Thus, an equivalent but less efficient command to accomplish the same as <code>gg=G</code> in normal mode, would be <code>ggVG</code> to select the entire buffer in visual mode, followed by <code>=</code> to re-indent the entire selection.
   
  +
The power of <code>=</code> is certainly not limited to the entire file. {{help|prefix=no|id===}} will re-indent just the current line, or the <code>=</code> operator can be combined with {{help|prefix=no|text-objects}} for very powerful results.
  +
  +
If you often re-indent large areas (like the entire file, or a large text object), it can be useful to map a key to do it for you, using marks to restore your position. To format and return the same line where you were, just add this mapping to your [[vimrc]]:
  +
<pre>
  +
map <F7> mzgg=G`z<CR>
  +
</pre>
  +
  +
Now, just press <F7> whenever you want to format your file.
  +
  +
'''Note''', the <code>=</code> operator works using whatever automatic indentation settings you have turned on. If you don't have this set up, see [[indenting source code]] before using.
  +
  +
==See also==
  +
*{{help|30.3}}
  +
*{{help|indent-expression}}
  +
*{{help|indent.txt}}
  +
*[[Indenting source code#Methods for automatic indentation]]
   
 
==References==
 
==References==
Line 10: Line 38:
   
 
==Comments==
 
==Comments==
 
This is amazingly useful, and almost unforgettable once you know how to do it. But it's almost impossible to find out about this behaviour, I've looked for a long time, so I put this page here with "fix indentation" and "reindent" as two search terms that I used.
 
 
{{todo}}
 
* Merge? Maybe keep as a separate tip for newcomers in [[:Category:Getting started]].
 
* Assumes that some sort of automatic indentation is turned on. Provide some basic info about this or links to other tips/help sections.
 
[[Category:Getting started]]
 

Revision as of 06:01, 13 July 2012

Tip 977 Printable Monobook Previous Next

created 2005 · complexity basic · version 6.0


It's possible to reformat an entire file, or a section of a file, using Vim's built-in = filter. Vim veterans often find this operator to be one of the most useful in their repertoire, but so common that it becomes second-nature and is rarely mentioned.

In normal mode, typing gg=G will reindent the entire file. This is a special case; = is an operator. Just like d or y, it will act on any text that you move over with a cursor motion command. In this case, gg positions the cursor on the first line, then =G re-indents from the current cursor position to the end of the buffer.

In visual mode, typing = will fix indentation of the current section. Thus, an equivalent but less efficient command to accomplish the same as gg=G in normal mode, would be ggVG to select the entire buffer in visual mode, followed by = to re-indent the entire selection.

The power of = is certainly not limited to the entire file. == will re-indent just the current line, or the = operator can be combined with text-objects for very powerful results.

If you often re-indent large areas (like the entire file, or a large text object), it can be useful to map a key to do it for you, using marks to restore your position. To format and return the same line where you were, just add this mapping to your vimrc:

map <F7> mzgg=G`z<CR>

Now, just press <F7> whenever you want to format your file.

Note, the = operator works using whatever automatic indentation settings you have turned on. If you don't have this set up, see indenting source code before using.

See also

References

Comments