|
|
| Line 13: |
Line 13: |
| |
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. |
|
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 re-indent 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 <tt>=</tt> is certainly not limited to the entire file. {{help|prefix=no|id===}} will re-indent just the current line, or the <tt>=</tt> operator can be combined with {{help|prefix=no|text-objects}} for very powerful results. |
+ |
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]]: |
|
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]]: |
| Line 26: |
Line 26: |
| |
Now, just press <F7> whenever you want to format your file. |
|
Now, just press <F7> whenever you want to format your file. |
| |
|
|
|
| − |
'''Note''', the <tt>=</tt> 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. |
+ |
'''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== |
|
==See also== |
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.
References
Edit