colemik
colemik

Reputation: 1547

Why does Vim treat \n as a valid EOL character for files in DOS format?

I was testing how Vim works with EOL characters.

If I create a file that looks like this:

enter image description here

and then load the file into Vim, explicitly stating that I want the file to be loaded in the dos fileformat (and not in the unix fileformat), the file looks like it has no errors:

enter image description here

How I would expect the file to be displayed:

enter image description here

Is there a deeper reason for this (i.e. in how Vim stores newline characters internally) or is this just a convenience mechanism in Vim, in case the person that loads the file, specifies the wrong file format? If the former, please say about the newline mechanism a little more in the context of my example.

Upvotes: 1

Views: 175

Answers (1)

doubleDown
doubleDown

Reputation: 8408

I think this is what happens. The file format (can be dos or unix) can be detected or manually set when opening a file. If it is set to dos, Vim removes both \r\n\ or \n from the file and replace them with its internal form of line ending (If it's unix, it replaces \n only, so \r\n will be left as \r which is displayed as ^J).

When you write to file, Vim will replace the internal EOL with the EOL corresponding with your fileformat – CRLF for dos, LF for unix.

Upvotes: 1

Related Questions