From ccdc042a891b57d7370213ec8ed97a7ba640899d Mon Sep 17 00:00:00 2001 From: Gergely Polonkai Date: Thu, 10 Nov 2016 11:31:50 +0100 Subject: [PATCH] Create function open-this-file-as-other-user It opens the current file as the specified user or root. --- init.el | 1 + lisp/file-manip.el | 13 +++++++++++++ 2 files changed, 14 insertions(+) create mode 100644 lisp/file-manip.el diff --git a/init.el b/init.el index bbbde37..5adc269 100644 --- a/init.el +++ b/init.el @@ -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) diff --git a/lisp/file-manip.el b/lisp/file-manip.el new file mode 100644 index 0000000..c50fc7a --- /dev/null +++ b/lisp/file-manip.el @@ -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))))