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

Share:
23,610

Related videos on Youtube

sandyp
Author by

sandyp

Updated on September 18, 2022

Comments

  • sandyp
    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
    Martin von Wittich about 10 years
    Argh, I blindly copypasted the original answer from Stack Overflow. Wait for it... fixed!
  • slm
    slm about 10 years
    Yeah 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
    slm about 10 years
    Nice, I couldn't find that, good detective work!
  • slm
    slm about 10 years
    You don't have to do strike throughs, the edit history contains the original version.
  • Gilles 'SO- stop being evil'
    Gilles 'SO- stop being evil' about 10 years
    Your 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'
    Gilles 'SO- stop being evil' about 10 years
    You'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
    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
    sandyp about 10 years
    Ok, so the takeaway for me is vim is weird. Gosh, i should've switched to emacs when i had the chance.
  • Martin von Wittich
    Martin von Wittich about 10 years
    @SandeepY yeah, at least this is weird, but that doesn't make vim a bad editor ;)
  • Vaibhav Jha
    Vaibhav Jha almost 5 years
    The worst is that it's different when replacing. So to change <CR> to newlines you unintuitively use :%s/\r/\r/f