I use this piece of code to set the default line ending to linux-style \n

;; https://www.emacswiki.org/emacs/EndOfLineTips

(add-hook 'find-file-hook 'find-file-check-line-endings)

(defun dos-file-endings-p ()
  (string-match "dos" (symbol-name buffer-file-coding-system)))

(defun find-file-check-line-endings ()
  (when (dos-file-endings-p)
    (set-buffer-file-coding-system 'undecided-unix)
    (set-buffer-modified-p nil)))

And this - to set the default encoding to utf-8

;; emacs set default codepage

;; https://stackoverflow.com/questions/1785200/change-emacs-default-coding-system

(prefer-coding-system 'utf-8)
(setq coding-system-for-read 'utf-8)
(setq coding-system-for-write 'utf-8)

But it looks like because of the second one, first isn't working. Now line ending is \r\n on saving, even if I manually change it.

  • Could you clarify why you think you need to do anything at all? If your locale uses utf-8 (which has been the default pretty much everywhere for the last 10 years, AFAIK), then Emacs will use utf-8 by default anyway. – Stefan Sep 4 '18 at 20:48
;; make unix lineendings default
(setq default-buffer-file-coding-system 'utf-8-unix)

For current release of emacs (26.1):

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

Verify with C-h v buffer-file-coding-system. It should say "global value is utf-8-unix".

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.