Vim Tips Wiki
(Adjust previous/next navigation)
(Change <tt> to <code>, perhaps also minor tweak.)
 
(2 intermediate revisions by 2 users not shown)
Line 3: Line 3:
 
|previous=1114
 
|previous=1114
 
|next=1119
 
|next=1119
|created=January 31, 2006
+
|created=2006
 
|complexity=intermediate
 
|complexity=intermediate
 
|author=hari_vim
 
|author=hari_vim
Line 11: Line 11:
 
|category2=
 
|category2=
 
}}
 
}}
You can use the <tt>g//</tt> global command to repeat an Ex command on each block of lines in a file. This technique is useful because you don't need a macro, and the command is retained in history for reuse, possibly after editing.
+
You can use the <code>g//</code> global command to repeat an Ex command on each block of lines in a file. This technique is useful because you don't need a macro, and the command is retained in history for reuse, possibly after editing.
   
 
For example, suppose you want to sort each block of text in a file, and you have a blank line before and after each block (including one before the first block, and one after the last block).
 
For example, suppose you want to sort each block of text in a file, and you have a blank line before and after each block (including one before the first block, and one after the last block).
Line 21: Line 21:
 
</pre>
 
</pre>
   
This applies the <tt>:sort</tt> command to a block of lines defined by a range. The first line in the range is the blank line next after the cursor, and the last line is just before (<tt>-1</tt>) the blank line after that. You must use <tt>;</tt> (not <tt>,</tt>). See {{help|:;}}.
+
This applies the <code>:sort</code> command to a block of lines defined by a range. The first line in the range is the blank line next after the cursor, and the last line is just before (<code>-1</code>) the blank line after that. You must use <code>;</code> (not <code>,</code>). See {{help|:;}}.
   
 
The following uses the global command to sort each block in the file:
 
The following uses the global command to sort each block in the file:
Line 29: Line 29:
 
</pre>
 
</pre>
   
The pattern <tt>/^\s*$/</tt> is used to find blank lines, including those consisting of only whitespace (<tt>\s</tt>). The second pattern (<tt>//</tt>) is empty, so the first pattern is repeated (search for next blank line).
+
The pattern <code>/^\s*$/</code> is used to find blank lines, including those consisting of only whitespace (<code>\s</code>). The second pattern (<code>//</code>) is empty, so the first pattern is repeated (search for next blank line).
   
 
==Comments==
 
==Comments==
For the address part you can use <tt>'}</tt>. <tt>'{</tt> and <tt>'}</tt> find paragraph boundaries.
+
For the address part you can use <code>'}</code>. <code>'{</code> and <code>'}</code> find paragraph boundaries.
   
 
It would be nice to have shortcuts for these patterns:
 
It would be nice to have shortcuts for these patterns:

Latest revision as of 06:08, 13 July 2012

Tip 1118 Printable Monobook Previous Next

created 2006 · complexity intermediate · author hari_vim · version 6.0


You can use the g// global command to repeat an Ex command on each block of lines in a file. This technique is useful because you don't need a macro, and the command is retained in history for reuse, possibly after editing.

For example, suppose you want to sort each block of text in a file, and you have a blank line before and after each block (including one before the first block, and one after the last block).

You can sort a single block after the cursor position using the command:

:/^$/;/^$/-1sort

This applies the :sort command to a block of lines defined by a range. The first line in the range is the blank line next after the cursor, and the last line is just before (-1) the blank line after that. You must use ; (not ,). See :help :;.

The following uses the global command to sort each block in the file:

:g/^\s*$/;//-1sort

The pattern /^\s*$/ is used to find blank lines, including those consisting of only whitespace (\s). The second pattern (//) is empty, so the first pattern is repeated (search for next blank line).

Comments[]

For the address part you can use '}. '{ and '} find paragraph boundaries.

It would be nice to have shortcuts for these patterns:

\(\%^\|^\n\)\@<=\(.\)\@= : paragraph start
\(.\)\@<=\(\%$\|\n$\)\@= : pragraph end

In normal mode, you can use "Vip:".