Vim: \n vs. \r Vim: \n vs. \r unix unix

Vim: \n vs. \r


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.


Behind the scenes, Vim uses \r (carriage returns) to save End-Of-Lines (regardless of the fileformat, which only matters when the file is being read or written). Vim uses \n to represent NULs. However, you search for EOL as \n, but in the replacement, \n stands for NUL. It's explained in :h sub-replace-sepcial. Searching for \r will find carriage returns that weren't part of the fileformat's EOL. There is a long explanation at :h file-formats.