Wednesday, August 26, 2009

Re: How to replace break; with {break;}

2009/8/25 Tim Chase <vim@tim.thechases.com>:
>
>> First, how do I replace a line that begins with a
>> pattern with a line with only \n ? I tried
>>
>> :1,$ s/^pattern/\n/g
>>
>> but it didn't work.
>
> For some reason, Vim's newline replacement is \r instead of \n
> which I wish I could explain better than "well, that's the way it
> is".  So you can do
>
>   :%s/^pattern/\r
>
> (the /g flag is unneeded because you're anchored to the beginning
> of the line, of which there's only one).  If you want to replace
> the whole line (in the event you have a line that looks like
>
>   break; // stuff here
>
> ), then your pattern has to consume the whole line:
>
>   :%s/^pattern.*/\r
>

\r instead of \n. That's interesting.
Thanks. :-)

>> A similar question is about replacing
>>
>> break;
>>
>> with
>> {
>>     break;
>> }
>
> If you don't need to keep the level of indent, it's pretty easy:
>
>   :%s/break;/{\r\t&\r}/g

Easy.

> (assuming you want a "\t"ab instead of spaces...adjust
> accordingly if you want spaces instead).  If, however, you want
> to keep the indent, it gets a little dirtier:
>
>   :%s/\(\s*\)break;/\1{\r\1\tbreak;\r\1}/g

Auch! :-)

> which captures the whitespace before the "break;" with "\(\s*\)"
> and then uses it to put back in before the "{", the "\tbreak;"
> and the "}".
>

Thanks for your help!

Regards,
S.

--~--~---------~--~----~------------~-------~--~----~
You received this message from the "vim_use" maillist.
For more information, visit http://www.vim.org/maillist.php
-~----------~----~----~----~------~----~------~--~---

No comments: