Hey guys,

Does anyone know why Emacs changes line endings when saving a file? I’m working on Windows and it overrides LF with CRLF every time, which is very annoying when writing bash scripts, as well when committing stuff in Git (diffs are huge).

I googled a lot, tried to copy Doom’s settings, but it still overrides unix configs I have from LF to CRLF. I also checked Crafted Emacs’ code, but it’s basically the same as Doom’s in regard to the coding system config.

Overall, my config is almost at the point of being good for my use cases, but this is a major show stopper for me.

This is a related part of my config:

(set-language-environment "UTF-8")
(setq default-input-method nil)
(set-selection-coding-system (if (eq system-type 'windows-nt) 'utf-16-le 'utf-8))

From General to Emacs

Hi @ech, welcome!

It might have more to do with git than emacs. Git provides auto.crlf to checkout with CR+LF and commit back with LF. This Github doc does a decent job of explaining it. Hope that works!

Thanks @shom, I’ll check out my git settings, I believe Git install set it to CR+LF by default.

However, it seems Emacs does this outside a Git repo folder. For example, I moved a config file to Desktop, converted line endings to LF, added a new line in Emacs, and saved the buffer – the whole file has been converted to CRLF after save.

1 Like

Ah okay! I don’t have Windows handy to test but does this work for you?

(setq-default buffer-file-coding-system 'utf-8-unix)

Nope, still rewrites everything to CRLF :slight_smile:

I just want Emacs to behave like other text editors, I’m even fine if it introduces mixed line endings, but overriding the whole file is pretty bad for me.

Is the above line the only thing you have in your config or do you have your previous lines as well?
Might be worth trying to launch emacs with just this specific config to isolate the issue:

emacs.exe -q --eval "(setq-default buffer-file-coding-system 'utf-8-unix)"

It does not, indeed, thanks very much! So we isolated the issue…

However, Emacs still saves with CRLF when when I add your suggested setq-default to my init.el:

This doesn’t make sense for me, maybe it’s time to declare config bankruptcy and start over :slight_smile: