Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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.

share|improve this question

migrated from serverfault.com May 6 '13 at 12:49

This question came from our site for system and network administrators.

up vote 1 down vote accepted

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'
share|improve this answer
This is true stuff, but I don't think it accomplishes quite what this guy is looking for. How would this get instantiated from the remote shell? – Caleb May 6 '13 at 13:53
@Caleb well, ah, point, not sure how to configure this for use with \e psql. See updated answer. – terdon May 6 '13 at 14:01

I'm not familiarized with emacs or psql. But...

Looks like you can use sshfs to mount your remote server and execute everything locally:

sshfs user@host mount/directory/
cd mount/directory && emacs

If you need run commands in the remote machine, you can mount with sshfs, then access with ssh then use ssh escape characters to suspend the ssh session and run emacs in your machine (editing the remote files mounted with sshfs).

Another solution would be install emacs in the remote machine and copy your configuration (I chose this solution aggregated in a git repo for my vim stuff)

share|improve this answer

This can't be done as you have described it. You could use SFTP (possibly through FUSE) to edit files on the remote system. It might be possible to do something really creative with scripts and piping, but it's not built-in functionality.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.