28

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!

rahmu
  • 19,673
  • 28
  • 87
  • 128

3 Answers3

28

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.

Gilles 'SO- stop being evil'
  • 807,993
  • 194
  • 1,674
  • 2,175
  • While 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. – Paul Rubel Nov 20 '18 at 16:13
12

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.

Jeff Schaller
  • 66,199
  • 35
  • 114
  • 250
choroba
  • 45,735
  • 7
  • 84
  • 110
  • 3
    Apparently, you've missed `dired-jump` (normally bound to `C-x C-j`). It runs `dired` and jumps to the line for the file you're editing. – cjm Sep 10 '12 at 16:33
  • 2
    @cjm: It does not work for me unless I load `dired-x`. – choroba Sep 10 '12 at 16:47
  • Sorry, I'd forgotten that I'd set up an autoload for `dired-jump` about 20 years ago. I recommend it. – cjm Sep 10 '12 at 17:04
  • 1
    Annoyingly `toggle-read-only` has been replaced with `read-only-mode`, although the binding is the same. – Charlie Martin Jul 19 '18 at 21:47
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))
Talespin_Kit
  • 557
  • 1
  • 5
  • 10