如何让emacs自动写入只读文件? [英] How do I get emacs to write to read-only files automatically?

查看:644
本文介绍了如何让emacs自动写入只读文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现自己编辑了很多只读的文件。我通常碰到 C-x C-q 来调用 toggle-read-only 。然后我点击 Cx Cs 保存并获取,

I am finding myself editing a lot of files that are read-only. I usually hit C-x C-q to call toggle-read-only. Then I hit C-x C-s to save and get,

File foo.txt is write-protected; try to save anyway? (y or n)

击中 y ,该文件被保存,并且文件的权限保持为只读。

After hitting y, the file is saved and the permissions on the file remain read-only.

有没有办法缩短此过程,使其简单地保存文件与Cx铯没有提示呢?应该在 before-save-hook after-save-hook之后插入 chmod 还是有更好的方法?

Is there a way to shorten this process and make it so that simply saving a file with C-x C-s does the whole thing without prompting? Should I look into inserting chmod in before-save-hook and after-save-hook or is there a better way?

推荐答案

添加调用 chmod before-save-hook 将是干净的方式来完成这一点。没有任何设置可以更改,以避免权限检查。

Adding a call to chmod in before-save-hook would be clean way to accomplish this. There isn't any setting you can change to avoid the permissions check.

根据后续问题,这听起来像您想要更改文件由你自动写入。这个代码的作法是:

Based on the follow-up question, it sounds like you'd like the files to be changed to writable by you automatically upon opening. This code does the trick:

(defun change-file-permissions-to-writable ()
  "to be run from find-file-hook, change write permissions"
  (when (not (file-writable-p buffer-file-name))
    (chmod buffer-file-name (file-modes-symbolic-to-number "u+w" (nth 8 (file-attributes buffer-file-name))))
    (if (not (file-writable-p buffer-file-name))
        (message "Unable to make file writable."))))

(add-hook 'find-file-hook 'change-file-permissions-to-writable)

注意:当我在Windows机器上测试时,文件权限没有显示,直到我尝试保存缓冲区,但是它按预期工作。我个人对这种定制感到不安,但这是您的Emacs。 :)

Note: When I tested it on my Windows machine, the file permissions didn't show up until I tried to save the buffer, but it worked as expected. I personally feel uneasy about this customization, but it's your Emacs. :)

这篇关于如何让emacs自动写入只读文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆