Created
November 10, 2016 10:05
-
-
Save gergelypolonkai/192c83aa0556d5cdaf4018f57b75a84b to your computer and use it in GitHub Desktop.
Emacs: Open the current file as another user
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 open-this-file-as-other-user (user) | |
"Edit current file as USER, using `tramp' and `sudo'. If the current | |
buffer is not visiting a file, prompt for a file name." | |
(interactive "sEdit as user (default: root): ") | |
(when (string= "" user) | |
(setq user "root")) | |
(let* ((filename (or buffer-file-name | |
(read-file-name (format "Find file (as %s): " | |
user)))) | |
(tramp-path (concat (format "/sudo:%s@localhost:" user) filename))) | |
(if buffer-file-name | |
(find-alternate-file tramp-path) | |
(find-file tramp-path)))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment