[英]How do I get emacs to write to read-only files automatically?
I am finding myself editing a lot of files that are read-only. 我发现自己编辑了许多只读的文件。 I usually hit
Cx Cq
to call toggle-read-only
. 我经常点击
Cx Cq
来调用toggle-read-only
。 Then I hit Cx Cs
to save and get, 然后我点击
Cx Cs
保存并获取,
File foo.txt is write-protected; try to save anyway? (y or n)
After hitting y
, the file is saved and the permissions on the file remain read-only. 点击
y
,文件将被保存,文件的权限保持为只读。
Is there a way to shorten this process and make it so that simply saving a file with Cx Cs does the whole thing without prompting? 有没有办法缩短这个过程,并使它只是保存一个Cx Cs文件完成所有事情没有提示? Should I look into inserting
chmod
in before-save-hook
and after-save-hook
or is there a better way? 我应该考虑在
before-save-hook
和after-save-hook
插入chmod
还是有更好的方法?
Adding a call to chmod
in before-save-hook
would be clean way to accomplish this. 在
before-save-hook
添加对chmod
的调用将是实现此目的的简洁方法。 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)
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. 注意:当我在Windows机器上测试它时,文件权限在我尝试保存缓冲区之前没有显示,但它按预期工作。 I personally feel uneasy about this customization, but it's your Emacs.
我个人对这种定制感到不安,但这是你的Emacs。 :)
:)
I agree with Trey that universally doing a chmod on write is risky -- read-only files are read-only for a reason, IMHO. 我同意Trey的看法,普遍认为写一个chmod是有风险的 - 只读文件是只读的有一个原因,恕我直言。 Here's a way to specifically override things on a per-buffer basis.
这是一种在每个缓冲区的基础上专门覆盖事物的方法。 It's not ideal in that it overrides
file-writable-p
for the life of the buffer (or at least until you toggle my-override-mode-on-save
back to nil), but it makes you make a conscious decision on a file-by-file basis (sort-of; it's really a buffer-by-buffer basis, which is fairly similar). 它不是理想的,因为它在缓冲区的生命周期中覆盖了
file-writable-p
(或者至少在你将my-override-mode-on-save
切换回nil之前),但它会让你有意识地决定一个文件-by-file basis(sort-of;它实际上是一个缓冲区的缓冲区,非常相似)。 Of course since you're looking to automatically toggle the read-only flag when the file is visited, you might not be interested in this distinction. 当然,因为您希望在访问文件时自动切换只读标志,所以您可能对这种区别不感兴趣。 Still, here it is;
不过,这里是; enjoy it or ignore it as you will.
享受它或者像你一样忽略它。
(make-variable-buffer-local
(defvar my-override-mode-on-save nil
"Can be set to automatically ignore read-only mode of a file when saving."))
(defadvice file-writable-p (around my-overide-file-writeable-p act)
"override file-writable-p if `my-override-mode-on-save' is set."
(setq ad-return-value (or
my-override-mode-on-save
ad-do-it)))
(defun my-override-toggle-read-only ()
"Toggle buffer's read-only status, keeping `my-override-mode-on-save' in sync."
(interactive)
(setq my-override-mode-on-save (not my-override-mode-on-save))
(toggle-read-only))
PS Thanks to Trey for the ad-return-value
pointer in the other SO question . PS感谢Trey在另一个SO问题中的
ad-return-value
指针。
Since I find it useful to be constantly reminded that I am about to edit a file I do not have permissions to, when I open a file in a buffer I want to force myself to proactively make the buffer writable wit Cx q
. 因为我发现不断提醒我即将编辑一个我没有权限的文件很有用,当我在缓冲区中打开一个文件时,我想强迫自己主动使缓冲区可写为
Cx q
。 Opening it with tramp by hand however is quite tedious so I advise save-buffer
to prompt me for password if it fails to write. 然后手动打开它是非常繁琐的,所以我建议
save-buffer
提示我输入密码,如果它无法写入。 I totally recommend you put this snippet in your .emacs
我完全建议你把这个片段放在你的
.emacs
(defadvice save-buffer (around save-buffer-as-root-around activate)
"Use sudo to save the current buffer."
(interactive "p")
(if (and (buffer-file-name) (not (file-writable-p (buffer-file-name))))
(let ((buffer-file-name (format "/sudo::%s" buffer-file-name)))
ad-do-it)
ad-do-it))
For a reason I could no determine the Trey Jackson solution does not work on my gnu emacs 25.2 under windows: the file-modes-rights-to-number
called from file-modes-rights-to-number
fails. 出于某种原因,我无法确定Trey Jackson 解决方案在我的gnu emacs 25.2下无法正常工作:从
file-modes-rights-to-number
调用的file-modes-rights-to-number
失败。 If someone is stuck with the same problem he could use a less elegant but working solution replace block : 如果有人遇到同样的问题,他可以使用不太优雅但工作正常的解决方案替换块:
(chmod buffer-file-name (file-modes-symbolic-to-number "u+w" (nth 8 (file-attributes buffer-file-name))))
with block : 带块:
(cond ((or (eq system-type 'ms-dos) (eq system-type 'windows-nt))
(progn
(shell-command-to-string (concat "attrib -R " (buffer-file-name (current-buffer))))
(message "Setting file and buffer to writeable (%s style)" system-type)
))
((eq system-type 'gnu/linux)
(progn
(shell-command-to-string (concat "chmod u+w " (buffer-file-name (current-buffer))))
(message "Setting file and buffer to writeable (%s style)" system-type)
))
(t (message "file permission change not handle for OS %s" system-type))
)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.