Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vim: \n vs. \r

Tags:

vim

unix

windows

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?

like image 414
Whaledawg Avatar asked Dec 08 '08 19:12

Whaledawg


People also ask

Are \n and \r the same?

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.

What does \r and \n mean?

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

What does r do in vim?

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.

How do I insert a carriage return in vim?

Ctrl - V tells vi that the next character typed should be inserted literally and ctrl - m is the keystroke for a carriage return.


2 Answers

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.

like image 197
Brian Carper Avatar answered Oct 13 '22 20:10

Brian Carper


Lookup up:

:set fileformat=unix :set fileformat=dos 

This can be used on either platform to switch to the other encoding.

like image 23
Jonathan Leffler Avatar answered Oct 13 '22 18:10

Jonathan Leffler