branch: master
commit ff3b696b77cec4bc57470ecce8eef6d345b59251
Author: fabacino <[email protected]>
Commit: Oleh Krehel <[email protected]>
counsel.el (counsel-find-file): Add new action counsel-find-file-as-root
Fixes #948
---
counsel.el | 25 ++++++++++++++++++++++++-
1 file changed, 24 insertions(+), 1 deletion(-)
diff --git a/counsel.el b/counsel.el
index 9db9049..4084926 100644
--- a/counsel.el
+++ b/counsel.el
@@ -1333,11 +1333,34 @@ done") "\n" t)))
:action (lambda (x)
(let ((default-directory (file-name-directory x)))
(counsel-find-file)))))
+
+(defcustom counsel-root-command "sudo"
+ "Command to gain root privileges."
+ :type 'string
+ :group 'ivy)
+
+(defun counsel-find-file-as-root (x)
+ "Find file with root privileges."
+ (let* ((host (file-remote-p x 'host))
+ (file-name (format "/%s:%s:%s"
+ counsel-root-command
+ (or host "")
+ (expand-file-name
+ (if host
+ (file-remote-p x 'localname)
+ x)))))
+ ;; If the current buffer visits the same file we are about to open,
+ ;; replace the current buffer with the new one.
+ (if (eq (current-buffer) (get-file-buffer x))
+ (find-alternate-file file-name)
+ (find-file file-name))))
+
(ivy-set-actions
'counsel-find-file
'(("j" find-file-other-window "other window")
("b" counsel-find-file-cd-bookmark-action "cd bookmark")
- ("x" counsel-find-file-extern "open externally")))
+ ("x" counsel-find-file-extern "open externally")
+ ("r" counsel-find-file-as-root "open as root")))
(defcustom counsel-find-file-at-point nil
"When non-nil, add file-at-point to the list of candidates."