Is VIM adding the Windows newlines

newlinesunixvim

Notice the line endings from git diff:

-   IP_ADDR: 'http://1.2.3.4:143'
+   IP_ADDR: 'http://2.4.6.8:143'^M

I edited this file by putting the cursor on 1 then pressing ct: and then entering the new IP address. No complete lines were added or removed from the file. I do notice though that the file shows as type dos in VIM.

Why would VIM change the line ending if I didn't explicitly edit that part of the document? Also, seeing how diff shows that there was no ^M in the original line, how else might VIM have decided that this is a dos file?

Best Answer

Vim will detect the original fileformat (among those configured in 'fileformats'), and write with the same one. The only way for Vim to switch (e.g. from Unix to Windows-style) is via an explicit :setlocal fileformat=dos. It's unlikely that you have that in your config; :verbose setl ff? could tell you.

I wouldn't be too concerned about the Git diff ifself (as long as not all lines appear as changed, then you really have a switch of line endings), but rather that what gets committed is alright.

Note that with the Git setting autocrlf = true, Git will convert newlines to the system's standard when checking out files, and to LF newlines when committing. So everything might be just fine, only the Git diff output is confusing you.

Related Question