How to configure GNU Emacs to write UNIX or DOS formatted files by default?

Greg Mattes picture Greg Mattes · Nov 4, 2009 · Viewed 7.7k times · Source

I've had these functions in my .emacs.el file for years:

(defun dos2unix ()
  "Convert a DOS formatted text buffer to UNIX format"
  (interactive)
  (set-buffer-file-coding-system 'undecided-unix nil))

(defun unix2dos ()
  "Convert a UNIX formatted text buffer to DOS format"
  (interactive)
  (set-buffer-file-coding-system 'undecided-dos nil))

These functions allow me to easily switch between formats, but I'm not sure how to configure Emacs to write in one particular format by default regardless of which platform I'm using. As it is now, when I run on Windows, Emacs saves in Windows format; when I run in UNIX/Linux, Emacs saves in UNIX format.

I'd like to instruct Emacs to write in UNIX format regardless of the platform on which I'm running. How do I do this?

Should I perhaps add some text mode hook that calls one of these functions? For example, if I'm on Windows, then call dos2unix when I find a text file?

Answer

user181548 picture user181548 · Nov 4, 2009

I've got a bunch of these in my .emacs:

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

I don't know which is right, I am just superstitious.