Often I ssh onto a remote machine from ansi-term and wish to open various files in Emacs. As I see it, there are two options:

  1. emacs -nw filename - hiding the terminal, opening the file in the current buffer and generally creating all sorts of havoc with the key-bindings
  2. emacs filename - opening in new window

Ideally, I'd like to use emacs filename to open the file in a separate buffer in the current window. I'm wondering if such a function exists, or if it might be easily written. Between ansi-term and tramp, the machinery seems to be there.

3 Answers 3


M-x shell and eshell can possibly be made to do this entirely in elisp? They don't seem to do so by default, but it might be worth digging a bit if you're keen.

ansi-term is a bit of a hybrid case, but it seems that some PROMPT_COMMAND trickery can be used with an external command to get a similar result.

To do this for remote shells in general (regardless of Emacs) -- I believe you'll need some external support for invoking emacsclient back on your local machine with the appropriate tramp path.

Here's my old suggestion for doing that, and that then led me to notice a variety of other solutions from other folks. Ryan Barrett lists several along with his own solution in his emacsclient in TRAMP remote shells blog post. Harold Abnabit then elaborates on Ryan's approach in Emacsclient and TRAMP.

(n.b. I have not tested any of these solutions myself)

  • My limited testing suggested that M-x shell didn't perform as desired but the first link worked beautifully, thank you!
    – glinka
    Commented Jul 14, 2015 at 15:15

... open file in a new Emacs buffer, but in the same window. The effect would be the same as using C-x C-f /ssh:host:path-to-file.

Have you tried tramp through the command line? Say you normally invoke a local file as:

emacs /path/to/local/file

then to invoke a remote file, try this:

emacs --eval '(find-file "/ssh:[email protected]:/path/to/remote.file")'

The effect would be the same as using

C-x C-f /ssh:[email protected]:/path/to/remote.file

inside Emacs.

The progression would be: a) ansi-term b) ssh remote_machine c) emacs file

then why not run emacs through the ssh command like so?

ssh remote_machine 'emacs /path/to/file'
  • This is perhaps a little closer, but in the problem at hand I'm already on the host machine in the Emacs terminal. The progression would be: a) ansi-term b) ssh remote_machine c) emacs file.
    – glinka
    Commented Jul 10, 2015 at 22:13
  • @glinka, see the expanded response
    – Emacs User
    Commented Jul 10, 2015 at 22:25
  • Appreciate your input! This is indeed what I currently do, but I don't have superuser privileges on the remote machine and the Emacs version there is broken so it'd be far nicer to use the local Emacs to open the file.
    – glinka
    Commented Jul 10, 2015 at 22:41
  • You don't need superuser privileges to use any of the solutions given by me or the others. I don't know how or why you think so. Maybe you need to rephrase your needs more clearly, give the exact commands you use and behaviors you want/don't want. The only other solution I can think of is to use remote editing via X-Wndows remote display functions. But for that you'll have to be a lot more forthcoming with details about your setup.
    – Emacs User
    Commented Jul 11, 2015 at 6:42
  • Apologies, it was poorly worded. I meant only that I cannot install a clean, updated and working version of Emacs without sudo. I understand that none of your suggestions require elevated privileges. I think @phils may have a solution though, I need to test it out.
    – glinka
    Commented Jul 11, 2015 at 7:03

You can always open a remote file in your local Emacs, it doesn't depend on ansi-term. Just do C-x C-f /ssh:host:path-to-file.

  • Right, I'd essentially like to use this functionality in the context of ansi-term. It seems doable.
    – glinka
    Commented Jul 10, 2015 at 15:54
  • Could you please elaborate what you mean with this? An example, what you mean with "in the context of ansi-term"? Commented Jul 10, 2015 at 18:27
  • I'd like to issue the command emacs file at the ansi-term prompt in Emacs, and have this open file in a new Emacs buffer, but in the same window. The effect would be the same as using C-x C-f /ssh:host:path-to-file. Does that clear it up?
    – glinka
    Commented Jul 10, 2015 at 18:32
  • I understand what you intend to do, but I don't understand why you want to do. Why do you want to start an Emacs process on the other machine? Commented Jul 11, 2015 at 8:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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