Carriage return vs newline in vim
23,610
Solution 1
From vim docs on patterns:
\r matches <CR>
\n matches an end-of-line - When matching in a string instead of buffer text a literal newline character is matched.
Source: https://stackoverflow.com/questions/71417/why-is-r-a-newline-for-vim
Solution 2
As @MartinvonWittich has stated you want to use \r
. You can also use the keyboard combination:
Ctrl + v + M which typically get's display as ^M
when typed.
%s/$/^M^M/
Here's some additionally useful info on vim
with respect to search and replace:
When searching:
-
.
,*
, \,[
,]
,^
, and$
are metacharacters. -
+
,?
, |,{
,}
,(
, and)
must be escaped to use their special function. -
\/
is/
(use backslash + forward slash to search for forward slash) -
\t
is tab,\s
is whitespace -
\n
is newline,\r
is CR (carriage return = Ctrl-M = ^M) -
\{#\}
is used for repetition./foo.\{2\}
will match foo and the two following characters. The\
is not required on the closing } so/foo.\{2}
will do the same thing. -
\(foo\)
makes a backreference to foo. Parenthesis without escapes are literally matched. Here the\
is required for the closing\
).
When replacing:
-
\r
is newline,\n
is a null byte (0x00). -
\&
is ampersand (& is the text that matches the search pattern). -
\1
inserts the text of the first backreference.\2
inserts the second backreference, and so on.
References
Related videos on Youtube
Author by
sandyp
Updated on September 18, 2022Comments
-
sandyp about 1 year
In vim, when I opened a file, I wanted to add two empty lines at the end of each current line. I used the following switch:
%s/$/\n\n/
Apparently, this does not work, but what does work is:
%s/$/\r\r/
I thought \r is a Windows feature? Would anyone be able to explain a bit more.
-
Martin von Wittich about 10 yearsArgh, I blindly copypasted the original answer from Stack Overflow. Wait for it... fixed!
-
slm about 10 yearsYeah I saw this answer over there. Not sure where it was pointing to. If you figure it out I'd suggest editing the SO Q as well.
-
slm about 10 yearsNice, I couldn't find that, good detective work!
-
slm about 10 yearsYou don't have to do strike throughs, the edit history contains the original version.
-
Gilles 'SO- stop being evil' about 10 yearsYour answer would imply that
\n
would match a newline character in a buffer, since it's an LF and not a CR. -
Gilles 'SO- stop being evil' about 10 yearsYou're not really explaining why it's
\r
rather than\n
. I think the answer boils down to “Vim is weird”, but there may be some now-forgotten historical reason. -
slm about 10 years@Gilles - In researching this all information I can find seems circular as to why
\r
is a<CR>
. It just is. But I would agree with your comment that it's weird. -
sandyp about 10 yearsOk, so the takeaway for me is vim is weird. Gosh, i should've switched to emacs when i had the chance.
-
Martin von Wittich about 10 years@SandeepY yeah, at least this is weird, but that doesn't make
vim
a bad editor ;) -
Vaibhav Jha almost 5 yearsThe worst is that it's different when replacing. So to change <CR> to newlines you unintuitively use
:%s/\r/\r/f