Right. There's also the sophomorically named "TRAMP" for connecting from a locally hosted emacs session to files across a wide range of protocols including ssh.
TRAMP also exhibits a wide range of performance problems. Getting it not to hang the display of the buffer list or lock up Emacs completely for minutes at a time requires a deep dive in to a decade of Stack Overflow breadcrumbs. I've done this, and I'm still not completely satisfied with the performance. I do however love the concept.
No, it requires only one thing: turn on persistent SSH connections. This means that new SSH connections to the same server do not actually require opening a new TCP connection, do not require negotiating new session keys, and do not require additional authentication. To turn this on, add these settings to your ~/.ssh/config file:
ControlMaster auto
ControlPersist yes
ControlPath ~/.ssh/control/%C
You should also use key–based authentication so that TRAMP never has to ask for a password, but this is less important once you are using persistent connections. Add something like this to your ssh config:
Host orod-na-thon
Hostname 192.168.2.133
User db48x
IdentityFile ~/.ssh/id_ed25519_your_private_key
Now go to Emacs and open up /ssh:orod-na-thon:~/.ssh/authorized_keys. This is the TRAMP path for your authorized_keys file on the machine named orod-na-thon. It will ask you for your password. Paste in your public key (which you can get by opening up the .pub file that goes alongside the private key in ~/ssh), and save it. Now future connections to orod-na-thon will be able to authenticate automatically using this key pair, and there will be no password prompt when you open a file there.
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?
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
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.
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.
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.
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.
> 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.
Yes, that's great, and it makes things extremely snappy while the socket connection remains open. Sleep your computer overnight, experience some network stormy weather, leave Emacs buffers open for weeks (as one does) and eventually one of those ControlMaster sockets are going to become wedged, and when that happens you're going to go through some things.
If that happens and you don’t want to suffer through the default 30–second (or whatever it is) timeout, then shorten it. Add `ConnectTimeout 5` to your config.
Good idea fine tuning the timeouts. I took your suggestion and dropped the SSH ConnectTimeout to 5, I was still using the default. I also peeked at the Tramp timeouts, and I realized that I had the variable as tramp-connect-timeout and not tramp-connection-timeout. Thanks!
I had this trouble when I upgraded emacs. I'm now stuck on 27.2 because later versions broke tramp in some way, and I haven't taken the time to debug it.