Originate edit of remote file using emacs tramp from ssh session
Asked Answered
S

5

13

This is probably a somewhat out-of-wack question. I use tramp to edit remote files, but I also open several terminals ssh-ing to that remote machine as well for other works (I had problems running ssh shell inside emacs).

Often times during the terminal work I would like to edit some file, and my current procedure is to copy the file name, and then use emacs tramp to open that file (after messing all around with getting the file path in the tramp format). This is way too much work for a quick edit and quite error prone in the path handling part.

The question is: Can I execute some command in the remote ssh session that takes the filename, transform that to tramp format (that's the easy part), and run a local command (like emacsclient blahblahblah) so that I can edit the remote file using tramp in my local emacs?

I'm not sure if I'm clear enough. I don't want to run emacs on the remote machine (either on the terminal or through an x session), but I do want to send file to my local emacs from a remote prompt, like this:

user@remote-machien ~/ $ run_local_emacs somefile
# then the file "/ssh:user@remote-machine/:/home/user/somefile" shows up 
# in my local emacs
Sightseeing answered 9/2, 2010 at 19:32 Comment(0)
D
13

You can set up your emacs-server to use a tcp connection (not just a local socket), and then on the remote side, tell emacsclient to connect to that tcp connection:

In your .emacs

(setq server-use-tcp t)
(setq server-host "name_of_local_machine")
(server-start)

And then on the remote side:

emacsclient -f ~/.emacs.d/server/server /`hostname`:/path/to/local/file

The above call to emacsclient brings up a file local to the "remote" machine in your Emacs running in the "local" machine. Obviously you can wrap the call to emacsclient in whatever kind of script you want to make it easier.

If your home directory is not visible on the remote machine, you will need to customize the server-auth-dir variable like so:

(setq server-auth-dir "/some/path/visible/on/both/machines")

For more documentation, see Emacsclient options.

Dominicdominica answered 9/2, 2010 at 19:58 Comment(6)
So I should start a server on the remote machine as well? I got error when running emacsclient on the remote machine (on ssh): "emacsclient: error accessing server file "~/.emacs.d/server/server"Sightseeing
@Sightseeing No, you don't start a server on the remote machine (that's what you're trying to avoid). The path to the server file must be visible to both machines for this to work. If that's not possible... you might be able to get away with copying the server file over - that's not quite as transparent.Dominicdominica
Ah it works now. I did copy the server file over but there was some permission error; now it works!Sightseeing
i've posted some emacs lisp that scp's the server file over and sets up other parts of this automatically: snarfed.org/emacsclient_in_tramp_remote_shellsMichelson
If the machine you're editing from isn't remotely accessible (my laptop isn't, for instance) then setting up an ssh tunnel can help: If you're ssh-ing to the remote machine, try adding -R 50000:127.0.0.1:50000 to your ssh command... where '50000' is replaced by the port from ~/.emacs.d/server/server (the part after '127.0.0.1:'. You can have emacs always use the same port; see here - though it requires hacking your emacs install (more than just .emacs).Lumberjack
I also adapted .emacs code from here to copy the .emacs.d/server/server file from my laptop to the remote filesystem after calling (server-start)Lumberjack
W
1

Theres also http://www.emacswiki.org/emacs/AnsiTermHints#toc4

Incorporates remote directory tracking which lets tramp open remote files as if it was local

Wonderwork answered 16/3, 2011 at 16:10 Comment(0)
R
0

You can use urxvt (an excellent terminal emulator) and write a perl extension to do this even if emacsclient isn't installed on the remote machine.

Runty answered 10/2, 2010 at 9:52 Comment(0)
C
0

Maybe you already know about this and it doesn't work well for you, but when I've needed to do that sort of thing the filename completion in TRAMP has been helpful enough that I've never thought of looking for alternatives.

C-xC-f/ssh:remotehost:/TabTab

This works best when you've got SSH keys or similar passwordless access set up, but it sounds like you have that already.

Colleencollege answered 28/9, 2015 at 18:21 Comment(0)
J
0

What I am doing here is a reverse ssh connection from remote to local and running emacsclient locally:

me@remote-machine$ ssh -f me@local-machine emacsclient /ssh:remote-machine/$(realpath my-file-name)
Jumble answered 7/10, 2015 at 9:1 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.