ChatGPT解决这个技术问题 Extra ChatGPT

如何使用 Emacs 更改文件的读/写模式?

如果文件设置为只读模式,我如何在 Emacs 中将其更改为写入模式,反之亦然?


m
mit

Mx 只读模式

在非常旧的 Emacs 版本中,命令是:

Mx 切换只读

在我的 Windows 机器上,这相当于 Alt-x 来调出元提示并键入“只读模式”来调用正确的 elisp 函数。

如果您使用默认键盘绑定,

Cx Cq

(您大声朗读为“Control-X Control-Q”)将具有相同的效果。但是请记住,鉴于 emacs 本质上是无限可重新配置的,您的里程可能会有所不同。

从评论跟进:您应该注意缓冲区的可写状态不会改变文件的可写权限。如果您尝试写入只读文件,您将看到一条确认消息。但是,如果您拥有该文件,则可以在不更改文件权限的情况下写出您的更改。

如果您想快速更改文件,而无需执行添加写入权限、写出更改、删除写入权限等多个步骤,这将非常方便。我倾向于忘记最后一步,留下潜在的关键文件以供以后意外更改。


嗨,正如 jfm3 toggle-read-only 的答案中所述,仅切换缓冲区的只读状态而不是文件的只读状态。如果要更改文件的模式,请使用 dired 或将 chmod +w 作为文件的 shell 命令执行。
没错,这就是您更改模式的方式。但是,如果您在指向您拥有的只读文件的缓冲区上切换只读模式,您将能够编辑它并写出您的更改(当然会有一个确认问题)。
跟进上述评论并在答案中添加文字。
在最近的 Emacs 版本中,toggle-read-only 已被 read-only-mode 取代。
j
jfm3

确保您没有将“文件”与“缓冲区”混淆。您可以使用 C-x C-q (toggle-read-only) 将缓冲区设置为只读并再次返回。如果您有权读取但不能写入文件,则访问文件时获得的缓冲区(C-x C-ffind-file)将自动进入只读模式。如果您想更改文件系统中某个文件的权限,可能在包含该文件的目录上以 dired 开头。 dired 的文档可以在 info 中找到; C-h i (emacs)dired RET


D
Dmytro Kuznetsov

我发现的是M-x set-file-modes filename mode

它在我的 Windows Vista 机器上工作。例如:M-x set-file-modes <RET> ReadOnlyFile.txt <RET> 0666


在我看来,这是对所提出的确切问题的正确答案。
在 elisp 中,这有效:(set-file-modes FILE 438)(其中 438 是 2#0110110110666 的十进制等效值)。
B
Borja Tarraso

正如其他人在那里提到的那样: Mx toggle-read-only 可以工作。

但是,现在已弃用,而 Mx 只读模式是当前的执行方式,即设置为 Cx Cq 键绑定。


T
Tushar

CTRL + X + CTRL + Q


解释? OP 说的是文件,而不是 buf。
V
Vebjorn Ljosa

如果只有缓冲区(而不是文件)是只读的,您可以使用 toggle-read-only,它通常绑定到 C-x C-q

但是,如果文件本身是只读的,您可能会发现以下函数很有用:

(defun set-buffer-file-writable ()
  "Make the file shown in the current buffer writable.
Make the buffer writable as well."
  (interactive)
  (unix-output "chmod" "+w" (buffer-file-name))
  (toggle-read-only nil)
  (message (trim-right '(?\n) (unix-output "ls" "-l" (buffer-file-name)))))

该函数取决于 unix-outputtrim-right

(defun unix-output (command &rest args)
  "Run a unix command and, if it returns 0, return the output as a string.
Otherwise, signal an error.  The error message is the first line of the output."
  (let ((output-buffer (generate-new-buffer "*stdout*")))
    (unwind-protect
     (let ((return-value (apply 'call-process command nil
                    output-buffer nil args)))
       (set-buffer output-buffer)
       (save-excursion 
         (unless (= return-value 0)
           (goto-char (point-min))
           (end-of-line)
           (if (= (point-min) (point))
           (error "Command failed: %s%s" command
              (with-output-to-string
                  (dolist (arg args)
                (princ " ")
                (princ arg))))
           (error "%s" (buffer-substring-no-properties (point-min) 
                                   (point)))))
         (buffer-substring-no-properties (point-min) (point-max))))
      (kill-buffer output-buffer))))

(defun trim-right (bag string &optional start end)
  (setq bag (if (eq bag t) '(?\  ?\n ?\t ?\v ?\r ?\f) bag)
    start (or start 0)
    end (or end (length string)))
  (while (and (> end 0)
          (member (aref string (1- end)) bag))
    (decf end))
  (substring string start end))

将函数放在 ~/.emacs.el 中,评估它们(或重新启动 emacs)。然后,您可以使用 M-x set-buffer-file-writable 使当前缓冲区中的文件可写。


当我尝试使用此代码编译我的 .emacs 时,我收到消息“警告:保存偏移被 set-buffer 击败”。
@Alan,编辑将 set-buffer 放在 save-excursion 之外。
感谢您的更改,消除了警告。但是,还有另一个:“警告:运行时调用 cl 包中的函数 `subseq'。”即使我将 (eval-when-compile (require 'cl)) 添加到我的 .emacs 中,我也无法摆脱它。
请参阅this thread。我已经编辑了代码并将 subseq 替换为 substring。那应该避免警告。
现在我已经转到 Emacs 24,我收到消息“警告:函数 `decf' 未知被定义。”
m
manas

如果您正在查看文件目录 (dired),则可以在文件名上使用 Shift + M 并输入 modespec,这与 chmod 命令中使用的属性相同。
M modespec <RET>

请参阅 http://www.gnu.org/s/libtool/manual/emacs/Operating-on-Files.html 中目录列表中文件的其他有用命令


佚名

我尝试了 Vebjorn Ljosa 的解决方案,结果发现至少在我的 Emacs (22.3.1) 中没有“trim-right”这样的功能,用于在 chmod 输出末尾删除无用的换行符。

删除对 'trim-right' 的调用有所帮助,但由于额外的换行符而使状态行“反弹”。


忘了包括它......现在添加。
L
Li Tianyi

C-x C-q 没用。因为您还需要保存文件的权限。

我使用 Spacemacs。它给了我一个方便的功能来解决这个问题。代码如下。

(defun spacemacs/sudo-edit (&optional arg)
  (interactive "p")
  (if (or arg (not buffer-file-name))
      (find-file (concat "/sudo:root@localhost:" (ido-read-file-name "File: ")))
    (find-alternate-file (concat "/sudo:root@localhost:" buffer-file-name))))

我调用 spacemacs/sudo-edit 在 emacs 中打开一个文件并输入我的密码,我可以在没有只读模式的情况下更改文件。

您可以编写一个新函数,例如 spacemacs/sudo-edit