How to modify write permission on current buffer in emacs?
Solution 1
After changing the file mode, and before doing any edit, run M-x revert-buffer
to reload the file. If the file is now writable, the buffer will no longer be read-only.
Alternatively, type C-x C-q
(read-only-mode
). This makes the buffer no longer read-only. You can edit and even save, but you'll get a confirmation prompt asking whether you want to overwrite the read-only file.
Solution 2
To change the read-only status of a buffer, use C-xC-q (toggle read-only-mode
). To change file permissions, you can run dired
on the file's directory (C-xd), search for the file by C-s and use M to change its mode.
Solution 3
If the workflow requires to change the file permission of the buffer repeatedly, then having a custom function would help like the following.
This works only on unix machines(executes system command "chmod"
(defun chmod-plus-w ()
(interactive)
(shell-command-to-string (concat "chmod +w " (buffer-file-name (current-buffer))))
(revert-buffer))
Related videos on Youtube
rahmu
Updated on September 18, 2022Comments
-
rahmu over 1 year
Is it possible to change the write permissions on a file from inside emacs, without killing/re-opening the buffer?
Sometimes I forget to modify the permissions on a file before opening it. I can modify the permissions from inside emacs (
M-! chmod u+w filename
) but this doesn't update the buffer which remains write protected and refuses to modify the file.Is there a way to update permissions inside the buffer? Bonus point if I can assign this to a shortcut!
-
cjm over 11 yearsApparently, you've missed
dired-jump
(normally bound toC-x C-j
). It runsdired
and jumps to the line for the file you're editing. -
choroba over 11 years@cjm: It does not work for me unless I load
dired-x
. -
cjm over 11 yearsSorry, I'd forgotten that I'd set up an autoload for
dired-jump
about 20 years ago. I recommend it. -
Charlie Martin almost 6 yearsAnnoyingly
toggle-read-only
has been replaced withread-only-mode
, although the binding is the same. -
Hodaya Shalom over 5 yearsWhile C-x C-q still works, as of emacs 24.3 it now calls read-only-mode rather than toggle-read-only and toggle-read-only has been disabled.