10

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.

1

2 Answers 2

4

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

9
  • Argh, I blindly copypasted the original answer from Stack Overflow. Wait for it... fixed! Sep 19, 2013 at 21:43
  • 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
    Sep 19, 2013 at 21:45
  • @slm I've fixed both my post and the post on SO :) Sep 19, 2013 at 21:50
  • 1
    Nice, I couldn't find that, good detective work!
    – slm
    Sep 19, 2013 at 21:52
  • 1
    Ok, so the takeaway for me is vim is weird. Gosh, i should've switched to emacs when i had the chance.
    – sandyp
    Sep 20, 2013 at 18:52
3

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

3
  • 1
    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. Sep 19, 2013 at 22:17
  • @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.
    – slm
    Sep 19, 2013 at 22:54
  • The worst is that it's different when replacing. So to change <CR> to newlines you unintuitively use :%s/\r/\r/f
    – Quantum7
    Feb 13, 2019 at 15:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.