views:

3428

answers:

5

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 sudo emacs. One way to do this is

(require 'tramp)
C-c C-f /sudo::/path/to/file

but this requires an expensive round-trip through SSH. Is there a more direct way?

[EDIT] @JBB is right. I want to be able to invoke su/sudo to save as well as open. It would be OK (but not ideal) to re-authorize when saving. What I'm looking for is variations of find-file and save-buffer that can be "piped" through su/sudo.

+1  A: 

Ugh. Perhaps you could open a shell in Emacs and exec sudo emacs.

The problem is that you presumably don't just want to open the file. You want to be able to save it later. Thus you need your root privs to persist, not just exist for opening the file.

Sounds like you want Emacs to become your window manager. It's bloated enough without that. :)

JBB
Ha ha you said bloat.Emacs used to seem huge. Now, in comparison to run-time footprints of Java, Ruby, and probably a pile of other stuff, it looks quite lean.Regardless, I think Chris' question gets at a perfectly legitimate use of Emacs.
jfm3
You beat me to it. I use this model consistently. At login I start one Emacs session for general stuff, one for SU access (as root) and one or more for software development (generally per-project but not always). Been doing it for years. Just works.
pajato0
+6  A: 

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 subsequence 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.

Denis Bueno
Wait a second... How often does the cache expire?
Chris Conway
Tramp does **not** round-trip via SSH, it uses a *subshell*.
Teddy
+3  A: 

Your example doesn't start ssh at all, at least not with my version of TRAMP ("2.1.13-pre"). Both find-file and save-buffer work great.

jfm3
You may have your credentials cached. When TRAMP first starts up, it goes through 10-15 seconds of SSH stuff. (I've got 2.1.13-pre too.)
Chris Conway
Are you sure? I mean, it should be starting a subshell, but not a SSH session to localhost. Takes about 5 seconds to run all the TRAMP auto-sniffage the first time.
jfm3
Well, no, I'm not sure. I should say there's 10-15 seconds of TRAMP (maybe SSH) stuff. I'm not concerned about SSH per se, but about the lag in starting up. How long does this startup stuff persist?
Chris Conway
It should only take more than a second the first time you do it. That is, even if you save /sudo::file, delete the buffer, and open /sudo::file2, file2 should open quickly. and It keeps the shell open, and caches the results of all its sniffing.
jfm3
But are the credentials cached: (1) for that Emacs process or (2) for all Emacs processes? And do they expire: (a) when Emacs closes, (b) after some fixed time period (hours? days? weeks?), or (c) after a system reboot?
Chris Conway
You should only ever need one Emacs process. That's the point of using TRAMP in the first place. Authentication credentials are "cached" in a manner identical to how they are when you run `sudo` or `ssh` (that is to say, not at all, or in an `ssh-agent`).
jfm3
+6  A: 

Tramp does not round-trip sudo via SSH, it uses a subshell. See the manual: http://www.gnu.org/software/tramp/#index-method-su-30

Therefore, I recommend that you stick with TRAMP.

Teddy
+1  A: 

At least for saving, a sudo-save package was written exactly for that kind of problem.

huitseeker