I haven't used vim in a Unix system in a while, but as I recall there was no \r, it was always \n.
I'm using gVim under windows and when I search for new line characters I use \n. Searching for \r returns nothing. But when I replace the characters I have to use \r's. \n's give me ^@
Can anyone explain what's going on here?
The /r stands for return or carriage return which owes it's history to the typewriter. A carriage return moved your carriage all the way to the right so you were typing at the start of the line. The /n stands for new line, again, from typewriter days you moved down to a new line.
\n means new line. It means that the cursor must go to the next line. \r means carriage return. It means that the cursor should go back to the beginning of the line.
r (replace) never enters insert mode at all. Instead, it expects another character, which it will then use to replace the character currently under the cursor.
Ctrl - V tells vi that the next character typed should be inserted literally and ctrl - m is the keystroke for a carriage return.
Looks like you're asking two things. One issue is \r
vs. \n
which others have covered.
The other issue is \n
on the right side of a substitution. If you look at :h s/\n
, it says that \n
in the replacement part of a substitution inserts a <NUL>
/ <NL>
, NOT a newline.
If you do a :%s/\n/\n/
and save and open the file in a hex editor, all the ^@
characters are ASCII 0's (NUL characters). Why the Vim devs use \n
on the left for end-of-line and \n
on the right for NUL is beyond me. But this particular behavior has nothing to do with Windows vs. Unix.
Lookup up:
:set fileformat=unix :set fileformat=dos
This can be used on either platform to switch to the other encoding.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With