19

多年来,我的.emacs.el文件中有这些功能:

(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))

这些功能使我可以轻松地在格式之间切换,但我不确定如何配置 Emacs 以默认以一种特定格式编写,而不管我使用的是哪个平台。现在,当我在 Windows 上运行时,Emacs 以 Windows 格式保存;当我在 UNIX/Linux 中运行时,Emacs 以 UNIX 格式保存。

我想指示 Emacs 以 UNIX 格式编写,而不管我在哪个平台上运行。我该怎么做呢?

我是否应该添加一些调用这些函数之一的文本模式挂钩?例如,如果我在 Windows 上,那么dos2unix当我找到文本文件时调用?

4

2 回答 2

26

我的 .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)

我不知道哪个是对的,我只是迷信。

于 2009-11-04T15:27:39.857 回答
14

我对问题和答案投了赞成票,但花了几分钟可能会改进信息,所以我会添加它。

首先,我检查了 user181548 答案中每个变量和函数的文档,方法是(首先剪切并粘贴到 Emacs 中,然后)将光标放在每个变量上,然后分别键入C-h v RETC-h f RET

这表明我可能只需要

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

尝试其他行似乎并没有改变预先存在的缓冲区编码(键入C-h C RET RET检查 ( describe-coding-system) 并g每次刷新),所以我省略了其他行并进行了键绑定以快速更改任何仍然存在的旧文件DOS,也就是说,

(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
        )

出于好奇,为了发现上述函数的第 3 行和第 4 行(set-buffer-file-coding-system 'utf-8-unix),我曾经C-x RET f RET手动更改当前缓冲区的编码,然后M-x command-history RET查看这些键如何转换为代码。

现在也许我的 git commit 会停止抱怨 CR。

于 2014-02-17T19:40:12.007 回答