Saturday, May 22, 2010

Re: Replacements with return

On 23/05/10 00:01, leandromartinez98 wrote:
>
> I'm trying do replace strings that contain return characters. For
> example,
> I would like to change:
>
> ----
>
> 0
> 0
> ----
>
>
> to
>
> ----
>
> 1
> 1
>
> ---
>
> I tried many things, as:
>
> :g/\n0\n/s//\n1\n/g
> :g/\n0\n/s//\r1\r/g
> (and all combinations of \r and \n possible)
> and variations of that using ^V+Return, ^V+^M.
>
> None of the combinations above give the correct result. For
> instance, :g/\n0\n/s//\n1\n/g results in:
>
> -----
> ^@1^@0
> -----
>
> And :g/\n0\n/s//\r1\r/g
>
> results in:
>
> ----
>
> 1
> 0
>
> ----
>
> I'm not getting the logic. Is there any symbol which represents
> the return character uniquely and acts in search/replace as a
> normal character?
>
> Thanks.
> Leandro.
>

When searching, or in the "replace what?" part of a substitute, \n
matches a linebreak. When replacing, i.e. in the "replace by" part of a
substitute, \n inserts a null and \r breaks the line. AFAIK there's no
way for a substitute to insert a lone ^M in the middle of a line.

To replace

0
0

by

1
1

only when they happen in pairs (which means that

0
0
0
2

will become

1
1
0
2

because matches don't overlap), you could use

:s/^0\n0$/1\r1/

where ^ at the start of a pattern matches start-of-line with zero width,
$ at the end of a pattern matches end-of-line with zero width,
\n in a pattern matches a line break,
\r in the replace-by string breaks the line.

See
:help pattern-overview
:help sub-replace-special


Best regards,
Tony.
--
Newton's Fourth Law: Every action has an equal and opposite satisfaction.

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