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

问题描述 投票:116回答:9

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

emacs file file-permissions
9个回答
170
投票

M-x切换只读

或者在更新版本的Emacs中

M-x只读模式

在我的Windows框中,相当于Alt-x以显示元提示并键入“toggle-read-only”以调用正确的elisp函数。

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

C-x C-q

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

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

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


14
投票

确保你没有将'file'与'buffer'混淆。您可以使用C-x C-qtoggle-read-only)将缓冲区设置为只读和返回。如果您有权读取但不能写入文件,则访问该文件时获得的缓冲区(C-x C-ffind-file)将自动置于只读模式。如果要更改文件系统中文件的权限,可以从包含该文件的目录中的dired开始。 dired的文档可以在info中找到; C-h i (emacs)dired RET


12
投票

我找到的是M-x set-file-modes filename mode

它在我的Windows Vista框中运行。例如:M-x set-file-modes <RET> ReadOnlyFile.txt <RET> 0666


6
投票

如果只有缓冲区(而不是文件)是只读的,则可以使用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使当前缓冲区中的文件可写。


6
投票

正如其他人所提到的那样:M-x toggle-read-only可行。

但是,现在不推荐使用它,并且M-x只读模式是当前的方法,它被设置为C-x C-q键绑定。


6
投票

CTRL + X + CTRL + Q.


2
投票

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

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


0
投票

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

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


0
投票

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这样的新函数。

© www.soinside.com 2019 - 2024. All rights reserved.