Create function open-this-file-as-other-user

It opens the current file as the specified user or root.
This commit is contained in:
Gergely Polonkai 2016-11-10 11:31:50 +01:00
parent 50d518a906
commit ccdc042a89
2 changed files with 14 additions and 0 deletions

View File

@ -827,6 +827,7 @@
(load "buf-manipulation.el")
(load "text-manip")
(load "frame-manip")
(load "file-manip")
;; Define aliases
(defalias 'yes-or-no-p 'y-or-n-p)

13
lisp/file-manip.el Normal file
View File

@ -0,0 +1,13 @@
(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))))