-
-
Save haxney/797576 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(defun find-file-as-root () | |
"Find a file as root." | |
(interactive) | |
(let* ((parsed (when (tramp-tramp-file-p default-directory) | |
(coerce (tramp-dissect-file-name default-directory) | |
'list))) | |
(default-directory | |
(if parsed | |
(apply 'tramp-make-tramp-file-name | |
(append '("sudo" "root") (cddr parsed))) | |
(tramp-make-tramp-file-name "sudo" "root" "localhost" | |
default-directory)))) | |
(call-interactively 'find-file))) | |
(defun toggle-alternate-file-as-root (&optional filename) | |
"Toggle between the current file as the default user and as root." | |
(interactive) | |
(let* ((filename (or filename (buffer-file-name))) | |
(parsed (when (tramp-tramp-file-p filename) | |
(coerce (tramp-dissect-file-name filename) | |
'list)))) | |
(unless filename | |
(error "No file in this buffer.")) | |
(find-alternate-file | |
(if (equal '("sudo" "root") (butlast parsed 2)) | |
;; As non-root | |
(if (or | |
(string= "localhost" (nth 2 parsed)) | |
(string= (system-name) (nth 2 parsed))) | |
(car (last 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" nil nil filename)))))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment