55

I'd like to be able to run emacs on one computer:

server $ emacs --daemon

And then connect to it from another:

local $ emacsclient -c server

Is this possible? If so, how?

liszt
  • 725
  • 1
  • 5
  • 10
  • 1
    Just a hint: if you don't need to keep the remote Emacs session running and you just want to edit files on the remote host with Emacs then you might want to try out TRAMP, which lets you edit files over the network transparently. – paprika Sep 27 '14 at 20:04
  • @paprika I think OP wants to use a remote emacs configuration locally. Hence TRAMP might not be suitable. That is the reason I prefixed my answer with the disclaimer. – Vamsi Sep 27 '14 at 20:15
  • 3
    There has been a discussion on StackOverflow on the subject: http://stackoverflow.com/questions/12546722/using-emacs-server-and-emacsclient-on-other-machines-as-other-users – Crash Coredump Sep 29 '14 at 12:25
  • [emacsclient and TRAMP](http://blog.habnab.it/blog/2013/06/25/emacsclient-and-tramp/) [Automatically open remote files in local emacs](http://andy.wordpress.com/2013/01/03/automatic-emacsclient/) I have had success with the first solution. The second seems to be simpler, I'm yet to try it out. (NOTE 2022: broken links) – Crash Coredump Sep 29 '14 at 12:31
  • 1
    Answer looks interesting, but please don't just post links. Include some excerpts and snippets so the answer stands on its own feet. – Malabarba Sep 29 '14 at 12:51

3 Answers3

26

You cannot use emacsclient to connect to an Emacs instance running on a remote computer. This client-server concept is related to (local) processes, not network nodes. However, you can use various network technologies to log in to the remote computer, attach to an emacs server running there and display the emacsclient's screen locally. Depending on the operating system that is used the networking could be done via SSH (terminal/X-Forwarding), VNC, RDP etc.

UPDATE:
As some people pointed out, emacsclient actually does have an option to connect to the server via TCP. However, emacsclient was never meant to be used remotely, the TCP socket option is required for compatibility with non-UNIX systems (i.e. operating systems where UNIX domain sockets are not available, like Windows).

paprika
  • 1,944
  • 19
  • 26
  • 5
    According to this [post](http://stackoverflow.com/a/12549749/2081427) your statement is not exactly right. Could you be more specific?. – DJJ Aug 29 '15 at 19:56
  • 13
    @paprika: No, the authentication mechanism used for the TCP connection was designed specifically for the remote-access case. I pushed for the use of TCP on the Windows side so that the code is also useful on POSIX for remote connections, thus reducing Windows-specific code and adding functionality to the POSIX side. – Stefan Aug 01 '16 at 14:31
  • 1
    @Stefan do you have more details of the emacs remote server usage? – erjoalgo Aug 17 '20 at 16:32
12

Probably not what you asked for but assuming you have ssh setup with X-forwarding, you could start emacsclient on the server and forward it to remote DISPLAY. (Disclaimer: code typed directly into webform)

 local> ssh server -f emacsclient -c --display=$DISPLAY
Vamsi
  • 3,916
  • 22
  • 35
8

This may be not what you want but just in case see if it can help you some way.

I usually work inside a virtual machine bootstrapped with Vagrant, I have my ~/.emacs.d directory synced between my machine (local) and the virtual machine (remote) putting the following in the Vagrantfile file:

config.vm.synced_folder "~/.emacs.d", "/home/vagrant/.emacs.d"

Also, my Emacs config automatically starts a server on startup:

(require 'server)
(setq server-use-tcp t
      server-socket-dir "~/.emacs.d/server")
(unless (server-running-p)
    (server-start))

So when I launch an Emacs server inside the virtual machine, I can connect to it with the following:

ssh -Y -i ~/.vagrant.d/insecure_private_key "<virtual machine hostname>" 'emacsclient -c -f ~/.emacs.d/server/server'

virtual machine hostname is the hostname of the virtual machine I have configured in my ~/.ssh/config:

Host <virtual machine hostname>
  HostName 127.0.0.1
  User vagrant
  Port 2222
  UserKnownHostsFile /dev/null
  StrictHostKeyChecking no
  PasswordAuthentication no
  IdentityFile /home/anler/.vagrant.d/insecure_private_key
  IdentitiesOnly yes
  LogLevel FATAL
  ForwardAgent yes

Note: Before launching the Emacs server inside the virtual machine I check that the ~/.emacs.d/server/server file is not present (if it is I just remove it) because otherwise it won't work.

Anler
  • 239
  • 1
  • 6
  • Just realized you are calling emacsclient in the remote server. In that case, you do not have to pass the server file explicitly as emacsclient will automatically connect to the first server it finds. – Vamsi Sep 27 '14 at 20:25
  • You don't need to specify the private key file in the command line if you have your identity file correctly set in your `.ssh/config` file. You just need to specify the `Host`. In doubt you can use the output of the command: `vagrant ssh-config` to set your `~/.ssh/config` file. – Alexandro de Oliveira Jun 28 '17 at 23:04