4

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.

puppon -su
  • 43
  • 1
  • 3
  • 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 04 '18 at 20:48

2 Answers2

2
;; make unix lineendings default
(setq default-buffer-file-coding-system 'utf-8-unix)
Maxim Kim
  • 1,516
  • 9
  • 17
2

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".

Kenyon
  • 121
  • 1
  • What is the difference between `default-buffer-file-coding-system` and `buffer-file-coding-system`? – jdhao Sep 28 '21 at 07:07