(defun find-alternative-file-with-sudo ()
"Open current buffer as root!"
(interactive)
(when buffer-file-name
(find-alternate-file
(concat "/sudo:root@localhost:"
buffer-file-name))))
(global-set-key (kbd "C-x C-r") 'find-alternative-file-with-sudo)
--
My Emacs Files At GitHub
I took this and ran with it.
ReplyDelete(defun toggle-sudo-edit (&optional buf)
"Toggle BUF between editing as root and normal user.
Default BUF is current buffer. The positions of point and mark
are preserved through the toggle."
(interactive)
(let* ((buf (or buf (current-buffer)))
(old-point (with-current-buffer buf (point)))
(old-mark (with-current-buffer buf (mark)))
(filename (buffer-file-name buf))
(root-string "/sudo:root@localhost:")
(currently-root (string-match-p (concat "^" root-string) filename))
(new-filename (if currently-root
(substring filename (length root-string))
(concat root-string filename))))
(find-alternate-file new-filename)
(goto-char old-point)
(set-mark old-mark)))