SSH – How to Bridge Backwards from an SSH Connection to Local Emacs-Server

editorsemacsssh

I'd very much like to log in to a remote machine with ssh and via the $EDITOR environment variable (or something similar) be able to edit in my local Emacs. In this case, specifically for when I log in and run psql and use the \e psql command to edit queries. Being able to edit them in my local Emacs would be a huge bonus in my work flow.

I have a nagging suspicion that there is no way this can be done, but before giving up I thought I'd throw it out here.

Best Answer

You can actually do this using tramp:

TRAMP stands for `Transparent Remote (file) Access, Multiple Protocol'. This package provides remote file editing, similar to Ange-FTP. The difference is that Ange-FTP uses FTP to transfer files between the local and the remote host, whereas TRAMP uses a combination of rsh and rcp or other work-alike programs, such as ssh/scp.

Add these lines to your ~/.emacs file (source):

(require 'tramp)
(setq tramp-default-method "scp")

You should then be able to open the remote file like this:

emacs /remote.server.com:/remote/path/foo.txt

Specifically for use with the psql command you would need something different though. Don't have a way of testing this now but perhaps setting emacs as an alias to your local emacs might work. Add this line to your remote .bashrc file:

alias emacs='ssh user@local emacs'
Related Question