Take the 2-minute tour ×

Notice the line endings from git diff:

-   IP_ADDR: ''
+   IP_ADDR: ''^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?

share|improve this question

2 Answers 2

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.

share|improve this answer

This is evidence that we are all living in the Matrix. If this were really the 21'st century, then we would not still be fighting with different line endings.

Vim does a pretty good job of doing The Right Thing with line endings. The details are explained under :help 'ffs'. Of course, vim cannot read your mind: if your file has inconsistent line endings, then vim may not do what you want.

I suggest opening the file in vim and then

:e! ++ff=unix

This will reload the file from disk, forcing vim to use unix-style line endings. Then you should see exactly which lines, any, have CRLF endings, because they will end with raw ^M characters.

While I love git, I do not know and trust it as well as I do vim. I think that some people recommend "set it and forget it" configurations for git's crlf settings that can lead to confusion. I prefer to avoid the setting that @Ingo Karkat mentioned in his answer. I want git to check out the same file that was checked in, and let me (and vim) take care of the line endings.

share|improve this answer

Your Answer


