Skip to content

Instantly share code, notes, and snippets.

@gergelypolonkai
Created November 10, 2016 10:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gergelypolonkai/192c83aa0556d5cdaf4018f57b75a84b to your computer and use it in GitHub Desktop.
Save gergelypolonkai/192c83aa0556d5cdaf4018f57b75a84b to your computer and use it in GitHub Desktop.
Emacs: Open the current file as another user
(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