Last active

Toggle between root and non-root users

  • Download Gist
Emacs Lisp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
(defun toggle-alternate-file-as-root (&optional filename)
"Toggle between the current file as the default user and as root."
(let* ((filename (or filename (buffer-file-name)))
(parsed (when (tramp-tramp-file-p filename)
(coerce (tramp-dissect-file-name filename)
(unless filename
(error "No file in this buffer."))
(if (equal '("sudo" "root") (butlast parsed 2))
;; As non-root
(if (or
(string= "localhost" (nth 2 parsed))
(string= (system-name) (nth 2 parsed)))
(nth -1 parsed)
(apply 'tramp-make-tramp-file-name
(append (list tramp-default-method nil) (cddr parsed))))
;; As root
(if parsed
(apply 'tramp-make-tramp-file-name
(append '("sudo" "root") (cddr parsed)))
(tramp-make-tramp-file-name "sudo" "root" "localhost" filename))))))

I created a fork which makes a round-trip to and from root on "/etc/passwd" work, at leas for me on Emacs on Ubuntu 10.10.

Excellent, thank you.

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.