Right now, I have the following in my init
(defun my-edit-file-as-root () "Find file as root" (interactive) (find-alternate-file (concat "/su:root@localhost:" buffer-file-name))) (defun my-edit-file-as-root-maybe () "Find file as root if necessary." (unless (and buffer-file-name (file-writable-p buffer-file-name)) (when (y-or-n-p "File is not writable. Open with root? ") (my-edit-file-as-root)))) (add-hook 'find-file-hook #'my-edit-file-as-root-maybe)
(a distant child of this code)
Unfortunately, this obviously doesn't work if I'm already using TRAMP (because it blindly concatenates the
/su... at the beginning).
I don't know TRAMP internals well enough to determine how to flexibly parse TRAMP filenames and determine what to add to get root access.
How can I modify this to work when already using TRAMP (possibly with multi-hops already in use)?