Login  Register

substitute on odd lines only

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
ranousse@gmx.com
21 posts
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 so complicated after all)

--
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

Re: substitute on odd lines only

Tim Chase
2203 posts
On 07/30/2011 05:22 AM, [hidden email] 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

Re: substitute on odd lines only

ranousse@gmx.com
21 posts
It works fine.
I never thought of using | after :g/pattern/
Thank you.

Le 30/07 - 06:29, Tim Chase wrote :

> On 07/30/2011 05:22 AM, [hidden email] 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

Re: substitute on odd lines only

Ben Fritz
3523 posts


On Jul 30, 7:06 am, [hidden email] wrote:
> It works fine.
> I never thought of using | after :g/pattern/
> Thank you.
>

Actually, you're using the | after the "if". :g is one of the commands
that treats | as part of its argument.

--
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