ARCHIVED: In Emacs, how do I use an already running session from another window?

This content has been archived, and is no longer maintained by Indiana University. Information here may no longer be accurate, and links may no longer be available or reliable.

The Emacsclient program can let you edit a file using an already running Emacs session rather than starting up a new one. It does this by sending a request to the already running Emacs, which must be expecting the request.

Setup

In order for Emacsclient to work, you must first execute the Server-start function. You can do this by entering the following command line option:

  emacs -f server-start

Another way to invoke Server-start is from the .emacs file:

  (if (some conditions are met) (server-start))

When you do this, Emacs starts a subprocess running a program called Server. Server creates a Unix domain socket in the user's home directory named .emacs_server.

To invoke Emacsclient from your newsreader, mail reader, etc., try setting the environment variable EDITOR (or sometimes VISUAL) to the value emacsclient. You may have to specify the full pathname of the Emacsclient program, for example:

  # csh commands:
  setenv EDITOR emacsclient
  setenv EDITOR /usr/local/emacs/etc/emacsclient  # using full pathname

  # sh command:
  EDITOR=emacsclient export EDITOR

Normal use

When Emacsclient is run, it connects to the .emacs_server socket and passes its command line options to Server. When Server receives these requests, it sends this information on the Emacs process, which at the next opportunity will visit the files specified. (You can specify line numbers just like with Emacs.) You will have to switch to the Emacs window by hand. After editing a file, you can type C-x # to indicate this. This will switch to another buffer created at the request of Emacsclient if there are any. When you have invoked C-x # on all of the files that Emacsclient requested to be edited, Emacs will send notification of this to Server which will pass this on to Emacsclient, which will then exit.

Note: Emacsclient and Server must be running on computers which share the same file system for this to work. The pathnames that Emacsclient specifies should be correct for the file system that the Emacs process sees. The Emacs process should not be suspended at the time you invoke Emacsclient. You should invoke Emacsclient either from another window or from a shell window inside Emacs itself.

There is an enhanced version of Emacsclient/Server called Gnuserv which is available in the Emacs Lisp Archive. Gnuserv uses Internet domain sockets, so it can work across most network connections. It also supports the execution of arbitrary Emacs Lisp forms and also does not require the client program to wait for completion. It is available via anonymous FTP from the Emacs Lisp Archive, in the directory packages/gnuserv.shar.

This information comes from the Emacs FAQ.

At Indiana University, for personal or departmental Linux or Unix systems support, see Get help for Linux or Unix at IU.

This is document abun in the Knowledge Base.
Last modified on 2018-01-18 08:44:28.