Vim: \ n vs \ r

I did not use vim on a Unix system, but as I recall, there was no \ r, it was always \ n.

I use gVim under windows, and when I search for newline characters, I use \ n. A search \ r returns nothing. But when I replace characters, I have to use \ r. \ N give me ^ @

Can someone explain what is going on here?

+19
windows unix vim
Dec 08 '08 at 19:55
source share
8 answers

Search up:

:set fileformat=unix :set fileformat=dos 

This can be used on any platform to switch to another encoding.

+17
Dec 08 '08 at 20:24
source share
— -

It sounds like you are asking two things. One problem is \r vs. \n others have reviewed.

Another problem is \n on the right side of the lookup. If you look :hs/\n , it says that \n in the replacement part of the substitution insert is a <NUL> / <NL> , not a new line.

If you execute :%s/\n/\n/ and save and open the file in a hex editor, all ^@ characters will be ASCII 0 (NUL characters). Why do Vim developers use \n on the left for the end of the line, and \n on the right for NUL - outside of me. But this particular behavior has nothing to do with Windows and Unix.

+25
Dec 08 '08 at 20:37
source share

Behind the scenes, Vim uses \ r (carriage return) to save End-Of-Lines (regardless of file format, which only matters when reading or writing a file). Vim uses \ n to represent NUL. However, you search for EOL as \ n, but when replaced, \ n means NUL. This is explained in: h replacement-replacement. A search \ r will find carriage returns that were not part of the EOL file. There is a long explanation in file formats: h.

+6
Jan 04 '09 at 21:18
source share

vim does some mess with carriage returns (\ r) and new characters (\ n). For example, if you are on Unix and vi shows lines ending in "^ M" because they are Windows text files, an easy way to get rid of them is to enter

 :%s/^V^M/^V^M/g 

It doesn't seem like he should do anything, but he does.

+3
Dec 08 '08 at 20:21
source share
 :%s/^V^M/^V^M/g 

Similarly, the following does the same (I think), and is easier to print.

 :%s/\r/\r/g 
+3
Dec 08 '08 at 21:29
source share

I think the problem may be that Windows and Unix are making newlines. The format for Unix is ​​\ n (string), but for Windows it is \ r \ n (carriage return, line feed).

+2
Dec 08 '08 at 20:06
source share

On unix, inside vim, ^V + <enter> gives me ^M , which is the \r character.

Also, you cannot find \r inside vim unless you tell him to edit the file in binary mode, that is, do not use the default automatic mode when it automatically determines the end of a line. (He should print [dos] in status.)

+1
Dec 08 '08 at 20:01
source share

On Windows, if you open the file in text mode, \ n is interpreted as newlines and line feeds. This usually does not apply to * nix systems.

0
Dec 08 '08 at 20:20
source share



All Articles