Emacs rewrites line endings on save (Windows)

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:

Try this

'(setq-default buffer-file-coding-system ‘unix)’

Or if you work in sh-mode for your bash Scripting you could try to add a hook

’ (add-hook 'sh-mode-hook (lambda () (set-buffer-file-coding-system ‘unix)))’

Maybe try moving that config line to the very end of your init file. Or as @Glenneth mentioned you can add that hook for prog-mode and text-mode.

1 Like

Doesn’t do anything, for some reason. So, if I have a default coding system set to unix, as @Glenneth suggested, does that mean that Emacs will be converting CRLF to LF across all files?

My situation is, I’m developing for Unreal Engine, and I can have all sorts of files in a projects: .sh files, server configs, .bat files, Python, lots of C++ and C#. Also others on the team can work with whatever line endings they have set by default by Visual Studio, maybe files will have mixed endings, etc. I want to use Emacs for UE development, that means editing files with mixed encodings, where I simply cannot rewrite endings across file, as changelists I submit will look huge and irrelevant, rendering code reviews practically impossible.

If I could ask Emacs to not do anything I didn’t ask it to, that would’ve solved the issue for me.

I’ve just read about inhibit-eol-conversion:

Emacs recognizes which kind of end-of-line conversion to use based on the contents of the file: if it sees only carriage returns, or only carriage return followed by linefeed sequences, then it chooses the end-of-line conversion accordingly. You can inhibit the automatic use of end-of-line conversion by setting the variable inhibit-eol-conversion to non-nil. If you do that, DOS-style files will be displayed with the ‘^M’ characters visible in the buffer; some people prefer this to the more subtle ‘(DOS)’ end-of-line type indication near the left edge of the mode line (see eol-mnemonic).

This seems to address my issue, but it makes ^M symbols visible, which I is what want to avoid :slight_smile: I dunno, can it be that Emacs just doesn’t support my use case and expects one EOL per file?

1 Like

Nice, glad you have a solution.
You can hide ^M with this Stack Overflow solution.