Tuesday, May 24, 2011

Re: vim can't find carriage returns

On May 24, 2011, at 1:45 PM, Tony Mechelynck wrote:

> On 24/05/11 14:31, Eljay Love-Jensen wrote:
> [...]
>> I'm not sure why I have to search FOR multiple \n and replace WITH the
>> \r. That may be a vi-thing, a Vim-thing, or a quirk / detail of my
>> platform (Windows 7, using the prebuilt gVim, using :set ff=unix line
>> endings). Hmmm, I never thought about that discrepancy before. [...]
>
> It may be vi-compatible, but it's one of those "Vim quirks" which one has to know: in Vim, to replace a line break by itself, you use :s/\n/\r -- i.e., search for \n to find a line break, insert \r to add a line break.

And all these years, I've been using

:s/\n/^M/

Where "^M" is typed "control-v, control-m") Usin

> Don't ask me why.

I can't tell you why "\n" matches newline on the "search" side of the substitute command, but inserts <00> ("null", equivalent to ^@) on the replace side. It is a mystery of life.

Obviously, "\r" means "return", short for "carriage return" but "\n" seems to mean both "newline" and "null" to vim.

Dave

PS: While reading up on this, I discovered something new, at least to me:

Save typing by using \zs and \ze to set the start and end of a pattern.

For example, instead of:

:s/Copyright 2007 All Rights Reserved/Copyright 2008 All Rights Reserved/

Use:

:s/Copyright \zs2007\ze All Rights Reserved/2008/

How cool is that?

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