如何配置 GNU Emacs 默认写入 UNIX 或 DOS 格式的文件?

发布于 2024-08-09 18:46:32 字数 678 浏览 4 评论 0原文

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

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

爱你不解释 2024-08-16 18:46:32

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

葬﹪忆之殇 2024-08-16 18:46:32

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

首先,我检查了 user181548 答案中每个变量和函数的文档,方法是(首先剪切并粘贴到 Emacs 中,然后)将光标放在每个变量和函数上,然后输入 Ch v RETCh f RET< /code> 分别。

这表明我可能只需要

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

试验其他行似乎并没有改变预先存在的缓冲区编码(输入 Ch 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
        )

对于好奇的人来说,发现上述函数的第三行和第四行,(set-buffer-file-coding-system 'utf-8-unix),我使用Cx RET f RET手动更改当前缓冲区的编码,然后使用 Mx command-history RET 查看这些键如何转换为代码。

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文