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

2

19

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?

Filip answered 4/11, 2009 at 15:20 Comment(0)
L
26

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.

Lantha answered 4/11, 2009 at 15:27 Comment(3)
setq-default not set-default is the correct way to change the above variables. Otherwise you'd need to quote the variable name.Scalariform
Corrected the setq-defaultsHypomania
What is the variable default-buffer-file-coding-system? I can not seem to find it in Emacs 27.2.Philippine
R
15

I up-voted question and answer, but spent a couple minutes possibly improving on the info, so I'll add it.

First, I checked documentation on each variable and function in user181548's answer, by (first cutting and pasting into Emacs, then) putting cursor over each, and typing C-h v RET and C-h f RET respectively.

This suggested that I might only need

(prefer-coding-system 'utf-8-unix) 

Experimenting with the other lines didn't seem to change pre-existing buffer encodings (typing C-h C RET RET to check (describe-coding-system) and g each time to refresh), so I omitted the other lines and made a key-binding to quickly change any old files that were still DOS, that is,

(defun set-bfr-to-8-unx ()
  (interactive)
  (set-buffer-file-coding-system
   'utf-8-unix)
  )
(global-set-key (kbd "C-c u") 
        'set-bfr-to-8-unx
        )

For the curious, to discover the 3rd and 4th line of above function, (set-buffer-file-coding-system 'utf-8-unix), I used C-x RET f RET to manually change the current buffer's encoding, then M-x command-history RET to see how those keys translate to code.

Now maybe my git commit's will stop whining about CRs.

Rehabilitation answered 17/2, 2014 at 19:40 Comment(2)
BTW, a keyboard macro may be useful for changing encodings for a set of files (say, from dired). The modern key-bindings for this can be found at gnu.org/software/emacs/manual/html_node/emacs/…. (Instead of C-x ( etc, we can use F3 and F4.)Rehabilitation
Upvoted for removing the redundancy; that one line was all that was needed for utf-8-unix encoding and LF line endings. I thought I'll additionally put (set-language-environment "UTF-8") based on this article, but decided I don't need it. Thanks!Olney

© 2022 - 2024 — McMap. All rights reserved.