Suppose I want to open a file in an existing Emacs session using su
or sudo
, without dropping down to a shell and doing sudoedit
or
I recommend you to use advising commands. Put this function in your ~/.emacs
(defadvice ido-find-file (after find-file-sudo activate)
"Find file as root if necessary."
(unless (and buffer-file-name
(file-writable-p buffer-file-name))
(find-alternate-file (concat "/sudo:root@localhost:" buffer-file-name))))
The nice thing about Tramp is that you only pay for that round-trip to SSH when you open the first file. Sudo then caches your credentials, and Emacs saves a handle, so that subsequent sudo-opened files take much less time.
I haven't found the extra time it takes to save burdening, either. It's fast enough, IMO.
Not really an answer to the original question, but here's a helper function to make doing the tramp/sudo route a bit easier:
(defun sudo-find-file (file-name) "Like find file, but opens the file as root." (interactive "FSudo Find File: ") (let ((tramp-file-name (concat "/sudo::" (expand-file-name file-name)))) (find-file tramp-file-name)))