Saturday, July 30, 2011

Re: substitute on odd lines only

On 07/30/2011 05:22 AM, ranousse@gmx.com wrote:
> I want to perform a substitute command on odd lines only.
> Is there some some "range" for this like sed 1~2 in vim?
> Else do you know a better solution than
> :%!sed '1~2s/pattern/replace/'

Not natively, but it's easy to do something of the sort:

:g/^/if line('.')%2|s/foo/bar/g|endif

You can tweak the test for even lines:

:g/^/if !(line('.')%2)|s/foo/bar/g|endif

or for every 3rd line (or Nth):

:g/^/if !(line('.')%3)|s/foo/bar/g|endif

-tim

--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

No comments:

Post a Comment