Aamir
Aamir

Reputation: 493

On Mac, line endings are automatically being set to Windows line endings, how do I change that?

Every time I git commit anything I get a warning that my files have windows line endings and that they will be changed to non-Windows line endings. I don't know why this is even happening in the first place, it seems every file I write in now has windows line endings.
This happens regardless of the editor, it occurs in both Vim and VS Code.
How do I change this back to use Unix line endings?

Upvotes: 5

Views: 5132

Answers (1)

Rob
Rob

Reputation: 437917

Both VIM and VS Code recognize whether the file was using macOS/Unix line endings (LF, e.g. 0a only) or Windows file endings (CRLF, e.g. 0d followed by 0a) and preserves that. You can use the terminal hexdump -C command and see if your lines are separated by 0d 0a or only 0a.

Most likely, when the files were first created, they were saved with Windows newline sequence, and VIM and VS Code are merely preserving that setting.

  • In VIM, when you open/save the file, if the file is currently using the Windows CRLF, you'll see a little [dos] in the status message at the bottom. If it already is in Unix format, you won't see the [dos] there.

    enter image description here

    In VIM, if it is a [dos] file, you can use the :set fileformat=unix to convert it to a Unix end-of-line character, and then save the converted file. See Vim convert file from DOS to UNIX.

  • In VS Code, you can set the eol setting, but that only affects new files. Again, existing files will preserve whatever end-of-line setting the file originally was using.

    But, if you open a file and look on the status bar, you will see a LF or CRLF towards the right. You can click on that to toggle back and forth.

    enter image description here

  • For information about how this is handled on the git side, see Configuring Git to handle line endings. I suspect that you might want to stick with the existing behavior (updating to Unix end-of-line character), but you will see your options clearly laid out in that GitHub doc. But if you really want to keep the CRLF in your files, you can configure your repo accordingly.

Depending upon how many files you have, you might want to find a tool to convert from Windows/DOS end-of-line CRLF sequences to Unix/macOS LF characters. A quick StackOverflow or web search will undoubtedly give you lots of suggestions.

Or, it sounds like your repo has already been updated to Unix-style LF characters, so you might just want to pull and use that.

Upvotes: 9

Related Questions