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

喜夏-厌秋 提交于 2019-11-30 01:54:42

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.

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!