my-emacs-d/lisp/file-manip.el
Gergely Polonkai ccdc042a89 Create function open-this-file-as-other-user
It opens the current file as the specified user or root.
2016-11-10 11:31:50 +01:00

14 lines
611 B
EmacsLisp

(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))))