Emacs: edit current file as root

Emacs: edit current file as root

Ever so often I open a file that I don’t I have permission to edit (so
automatically Emacs will go into read-only-mode). It’s only then I find that I
need to edit it, so I will have to type in the following command to open this
file, again, as root:

C-x C-f /sudo:root@localhost:/path-to-the-file/

To reduce the number of keystrokes, I have written the following small Elisp
function, it detects if there is a file that is associated with the current
buffer; if so, open the file as root:

(defun wenshan-edit-current-file-as-root ()
  "Edit the file that is associated with the current buffer as root"
  (interactive)
  (if (buffer-file-name)
      (progn
        (setq file (concat "/sudo:root@localhost:" (buffer-file-name)))
        (find-file file))
    (message "Current buffer does not have an associated file.")))

Note : This function is only effective for local files.

eval it, then you could call it by pressing:

M-x wenshan-edit-current-file-as-root

Happy Hacking!


Date: 2013-05-09

Author: 任文山 (Ren Wenshan)

Org version 7.9.3d with Emacs version 24

Validate XHTML 1.0

3 thoughts on “Emacs: edit current file as root