History
Article Edit this page Discussion

Repeating an ex command on multiple blocks

From Vim Tips Wiki

(Redirected from VimTip1118)
Jump to: navigation, search

Tip 1118 Previous Next Created: January 31, 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).

[edit] 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:".


Rate this article:

Share this article:

Hubs Highlights International Sites Wikia messages
Entertainment
Gaming
Cartoons & Comics
Science Fiction
Hobbies
Sports
See all...
Grand Theft Auto
Pixar
Legend of Zelda Wiki
Terminator Wiki
Everquest II Wiki
Dark Shadows
German
Spanish
Chinese
Japanese
More...
Wikia is hiring for several open positions


Vote for collaboration of the month.

Send this article to a friend
"Repeating an ex command on multiple blocks"
 
 
Hi!

I thought you'd like this page from Wikia!

http://vim.wikia.com

Come check it out!
Send confirmation