vim can't find carriage returns

2,372 views
Skip to first unread message

JP Lew

unread,
May 24, 2011, 2:14:26 AM5/24/11
to vim_use
Hi there:
No matter which document I'm editing, whenever I search for a carriage
return like this:

/\r

I get the following error message:

E486: Pattern not found: \r

It's puzzling because Vim is matching other character classes like \t
and \n without any problems. I know for a fact that my document has
carriage returns in it, because when I open it in another text editor,
like BBEdit for example, the grep search is turning up carriage
returns in all the right places.

Any ideas? Thanks.

John Beckett

unread,
May 24, 2011, 3:26:11 AM5/24/11
to vim...@googlegroups.com
JP Lew wrote:
> No matter which document I'm editing, whenever I search for a
> carriage return like this:
>
> /\r
>
> I get the following error message:
>
> E486: Pattern not found: \r

Vim removes line endings when a file is read, so there may not
be any CR in the buffer that is displayed. Search for \n to find
what Vim has detected as a line ending.

To convert from one line ending type to another, see:
http://vim.wikia.com/wiki/File_format

For testing, you can enter insert mode and type Ctrl-V Enter (or
Ctrl-Q Enter if you have mapped Ctrl-V to paste). That will
insert a CR displayed as ^M (Ctrl-M = 13 = CR). Searching for \r
will find it.

John

Frans Grotepass

unread,
May 24, 2011, 3:34:51 AM5/24/11
to vim...@googlegroups.com

what does your fileformat say when you are in vim? (:set fileformat)
What OS are you using?

Albie Janse van Rensburg

unread,
May 24, 2011, 4:10:56 AM5/24/11
to vim...@googlegroups.com

Are you specifically looking for carriage returns? If you are just
looking for end-of-lines, then the following will serve you better:

/\n

Otherwise, see John Beckitt's sibling post.


JP Lew

unread,
May 24, 2011, 4:17:08 AM5/24/11
to vim...@googlegroups.com
Thank you John, Frans, and Albie!
your answers were timely, educational, and helped me solve my problem—much appreciated. For the record I am using Unix (Mac OS X). 

I've only been using Vim for a week now, but with every passing day am more and more impressed by its power.

My original problem was that I wanted to delete all the blank lines in my document. Previously, in BBEdit I would search for "\r\r" and replace with "\r", so that's why I was searching for carriage returns in the first place.
But I just discovered a much slicker workaround in Vim: :g/^$/d
Awesome!



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


Eljay Love-Jensen

unread,
May 24, 2011, 8:31:36 AM5/24/11
to vim...@googlegroups.com
Hi JP Lew,
 
Your discovery works great:
:g/^$/d
Depending what you consider a "blank line", you may want:
:g/^\s*$/d
 
If you want to delete runs of blank lines over a threshhold, that can be done as well.  One way to do it is a search-and-replace command like:
:%s/\n\{2,\}/\r\r/
 
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.  The help on it...
:help sub-replace-special
...doesn't really explain the discrepancy.  My speculation is that the discrepancy probably has to do with Vim ingesting a text file as strings, and those lines are stripped of the line endings and are '\0' terminated.  Which would make the discrepancy an implementation detail.
 
There are a lot of powerful editors available.  Vim is more than a powerful editor.  Vim is zen editing.  Vim lets me become one with my keyboard and edit my document; all the while the editor is not a distracting interface but rather is unobtrusive.
 
Happy Vim-ming!
Eljay

Frans Grotepass

unread,
May 24, 2011, 9:04:48 AM5/24/11
to vim...@googlegroups.com
On Tue, 24 May 2011 01:17:08 -0700
JP Lew <jple...@gmail.com> wrote:

> I've only been using Vim for a week now, but with every passing day
> am more and more impressed by its power.

And it is only the first week :D Boy, you have no idea how good this
will still get!! The real fun starts after you've assimilated the vim
mindset. That usually requires about a month imho. Then the true power
starts to show itself. I suggest grabbing a good cheat sheet off the
net for some ideas once things start to become fluent. Also check out
some of the .vimrc files used by seasoned vimmers. That should provide
some input for getting some of the more obscure, but very useful
features.

Eljay Love-Jensen

unread,
May 24, 2011, 9:43:49 AM5/24/11
to vim...@googlegroups.com
Hi JP Lew,
 
Frans Grotepass>  I suggest grabbing a good cheat sheet of the net...
 
I have found this tutorial / cheat sheet website to be fantastic:
 
Not only great for getting up to speed in Vim, I still refer to it for the Vim commands I only use infrequently.

Happy Vim-ming,
Eljay

Frans Grotepass

unread,
May 24, 2011, 10:01:30 AM5/24/11
to vim...@googlegroups.com

And once things are up to speed, try
http://www.rayninfo.co.uk/vimtips.html

Jean-Rene David

unread,
May 24, 2011, 10:16:39 AM5/24/11
to vim...@googlegroups.com
* JP Lew [2011.05.24 04:20]:

> My original problem was that I wanted to delete all the blank lines in my
> document.

By the way, you would have gotten immediate answers if you had asked
that to begin with. As a matter of fact it is a FAQ (12.4 on
http://vimdoc.sourceforge.net/htmldoc/vimfaq.html)

You almost always get inferior answers if you presume what the solution
is instead of stating the problem you want to solve. It never hurts to
say what solution you *tried* but stating the problem let's the more
imaginative members of the list really shine. :-)

My two cents,

--
JR

JP Lew

unread,
May 24, 2011, 11:44:52 AM5/24/11
to vim_use
On May 24, 5:31 am, Eljay Love-Jensen <eljay...@gmail.com> wrote:
> There are a lot of powerful editors available.  Vim is more than a powerful
> editor.  Vim is zen editing.  Vim lets me become one with my keyboard and
> edit my document; all the while the editor is not a distracting interface
> but rather is unobtrusive.

haha, "Zen Editing". I look forward to that "state of no mind". As of
now, it's a bit hard to enter into trance with my nose buried in the
help documentation. Although I must admit that the regular discoveries—
woah, I can do THAT!—are quite ecstatic.

> http://www.viemu.com/a_vi_vim_graphical_cheat_sheet_tutorial.html

What a good idea, wish I had seen this earlier.

On May 24, 7:01 am, Frans Grotepass <fmgrotep...@yahoo.co.uk> wrote:
> And once things are up to speed, tryhttp://www.rayninfo.co.uk/vimtips.html

You know something is good when, after being together for 22 years,
things are still ever-fresh. When I think about it, none of my other
relationships have passed that test. :)

On May 24, 7:16 am, Jean-Rene David <jrda...@magma.ca> wrote:
> By the way, you would have gotten immediate answers if you had asked
> that to begin with. As a matter of fact it is a FAQ (12.4 onhttp://vimdoc.sourceforge.net/htmldoc/vimfaq.html)

Thanks JR for the tip, and for referring me to the FAQ, I never knew
that existed either.
You're right though—as a rule, a general question begets a general
answer, a specific question a specific one. I can say I learned a lot
in the process however. :)
Now it's evident to me how alive and enthused the Vim community is. It
seems the only thing exceeding Vim's might is the fanatic loyalty it
inspires in its users. Thanks all, wish me luck.

-JP

Christian Brabandt

unread,
May 24, 2011, 2:15:03 PM5/24/11
to vim...@googlegroups.com
Hi Jean-Rene!

On Di, 24 Mai 2011, Jean-Rene David wrote:

> * JP Lew [2011.05.24 04:20]:
> > My original problem was that I wanted to delete all the blank lines in my
> > document.
>
> By the way, you would have gotten immediate answers if you had asked
> that to begin with. As a matter of fact it is a FAQ (12.4 on
> http://vimdoc.sourceforge.net/htmldoc/vimfaq.html)

By the way, that page is outdated. The current version is available at
http://vimhelp.appspot.com/vim_faq.txt.html

regards,
Christian
--
Gl�ckliche M�dgen in der Ehe lieben schon Romane nicht mehr, weil sie
nichts mehr auf sich beziehen k�nnen.
-- Jean Paul

Tony Mechelynck

unread,
May 24, 2011, 4:45:23 PM5/24/11
to vim...@googlegroups.com, Eljay Love-Jensen
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.

Don't ask me why.


Best regards,
Tony.
--
There once was a member of Mensa
Who was a most excellent fencer.
The sword that he used
Was his -- (line is refused,
And has now been removed by the censor).

Dave Land

unread,
May 24, 2011, 8:08:04 PM5/24/11
to vim...@googlegroups.com
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?

Tony Mechelynck

unread,
May 24, 2011, 9:32:14 PM5/24/11
to vim...@googlegroups.com, Dave Land
On 25/05/11 02:08, Dave Land wrote:
[...]

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

Quite cool, and of course, in this particular case you could even do

:s/Copyright 200\zs7\ze All Rights Reserved/8/

;-)


Best regards,
Tony.
--
Basic, n.:
A programming language. Related to certain social diseases in
that those who have it will not admit it in polite company.

Reply all
Reply to author
Forward
0 new messages