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 this small Elips function can be useful. It detects if there’s a file that is associated with the current buffer, if so open this file as root permissions:

(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.")))

Above function is only effective for local file.

My site is free of ads and trackers. Was this post helpful to you? Why not BuyMeACoffee