Hacker News new | ask | show | jobs
by cycomanic 875 days ago
One problem that I looked briefly into, but have not found a solution is that often I'm browsing the file system (in a terminal) on the remote box and want to open up a file in a certain location (often just for a quick edit). The process of typing pwd, and copying the location into emacs (similar applies to Neovim) already is too much friction, that it's easier to just open the barebones Vim on the remote.

Anyone aware of a solution of how to start the local editor from the remote to open the remote file?

5 comments

Best solution to that is to use Eshell. Run `M-x eshell` to open the shell inside Emacs (on your local machine), then type `cd /ssh:orod-na-thon:path/to/files`. This will transparently ssh to the server and change to the path to your files. The `ls` command will now show the files that exist on the remote server rather than the local computer. You might now expect that there would be an eshell command that you could type to open a remote file in your local Emacs, but there isn’t.

No, in Emacs you always open a file by typing `C-x C-f`. (Unless you rebound it to some other key, in which case make the obvious substitution.)

Any time you hit `C-x C-f` to open a file it defaults to opening files from the working directory of the current buffer. The working directory of the eshell buffer is on the remote server, so the default list of files that you see are all the ones you were already looking at with `ls`. You can start typing a filename and autocomplete will do the rest.

This is an even deeper and more convenient composition of the shell and the editor than having special commands for tasks like opening a remote file in the local editor.

> You might now expect that there would be an eshell command that you could type to open a remote file in your local Emacs, but there isn’t. No, in Emacs you always open a file by typing `C-x C-f`

`C-x C-f` calls the `find-file` function which can be called directly from eshell

> You might now expect that there would be an eshell command that you could type to open a remote file in your local Emacs, but there isn’t.

All Emacs Lisp functions are Eshell commands. Say 'find-file <filename>' at Eshell and you're in.

Technically correct, but it is way easier to hit `C-x C-f` just like you would when you are opening a file at any other time. No need for special cases, you just open the file.
`vterm` beats the crap out of `eshell` as a functional terminal, unless you've put a lot of effort into customizing it.

https://www.masteringemacs.org/article/running-shells-in-ema...

That’s because eshell is not a terminal. It’s a shell. You don’t need a terminal to explore the files on the other server.
Probably not the answer You are looking for...

Try some variant of this in your remote .bash_profile or what-not:

  if [ "$PS1" ]; then
      export PS1='\h:\w\$ '
      if [[ "x${TERM}" = "xeterm" || "x${TERM}" = "xeterm-color" ]]; then
       function set-eterm-dir {
         echo -e "\033AnSiTu" $(whoami)
         echo -e "\033AnSiTc" $(pwd)
         echo -e "\033AnSiTh" $(hostname -f)
       }
      PROMPT_COMMAND=set-eterm-dir
    fi
  fi
Now, locally, invoke M-x ansi-term, then within the ansi-term ssh to the remote machine. Change dirs, hit C-x C-f to open a file in the current (remote) directory.

See also the ansi-term hints on emacswiki

https://www.emacswiki.org/emacs/AnsiTermHints#h5o-5

or the comments in /usr/local/share/emacs/*/lisp/term.el

Edit: formatting

Apologies, I meant to add more saying I could not get a remote machine to invoke the local emacs reliably. I got close with hacking up emacsclient to ssh back, but was eventually stymied by new network policies at $JOB and eventually settled for the above.

db48x has a great answer below.

In the specific case of Emacs, assuming you're connected via ssh, sshd is running on port 22 of both hosts, an Emacs server is running locally, and emacsclient is in the default local path, a bash function or script along the lines of

  for i in "$@"; do
    ssh "${SSH_CONNECTION%% *}" emacsclient --no-wait "/ssh:$(hostname):$(realpath "$i")"
  done
should work.

For best results, use ssh-agent + agent forwarding to avoid password prompts.

I don't have a specific answer for you, but what I normally do is open

   /remote:/the/path/<tab>
and autocomplete the file name.

Additionally, you can just do dired:

   /remote:/the/path  <return>
and scroll to the remote file I want to edit and press enter
> The process of typing pwd, and copying the location into emacs (similar applies to Neovim) already is too much friction, that it's easier to just open the barebones Vim on the remote.

Vim has had the ability to open files remotely over ssh, sftp, etc. for a long time using the built-in plugin netwr [1].

Neovim takes it to another level [2]:

    Nvim's RPC functionality allows clients to programmatically control Nvim.
    Nvim itself takes command-line arguments that cause it to become a client to
    another Nvim running as a server. These arguments match those provided by
    Vim's clientserver option.
[1]: https://vonheikemen.github.io/devlog/tools/using-netrw-vim-b...

[2]: https://neovim.io/doc/user/remote.html