Next: Recursive Edit Prev: Editing Binary Files Up: Top
Using Emacs as a Server
Various programs such as `mail' can invoke your choice of editor to
edit a particular piece of text, such as a message that you are
sending. By convention, these programs use the environment variable
`EDITOR' to specify which editor to run. If you set `EDITOR' to
`emacs', they invoke Emacs--but in an inconvenient fashion, by starting
a new, separate Emacs process. This is inconvenient because it takes
time and because the new Emacs process doesn't share the buffers in the
existing Emacs process.
You can arrange to use your existing Emacs process as the editor for
programs like `mail' by using the Emacs client and Emacs server
programs. Here is how.
First, the preparation. Within Emacs, call the function
`server-start'. (Your `.emacs' file can do this automatically if you
add the expression `(server-start)' to it.) Then, outside Emacs, set
the `EDITOR' environment variable to `emacsclient'.
Then, whenever any program invokes your specified `EDITOR' program,
the effect is to send a message to your principal Emacs telling it to
visit a file. (That's what the program `emacsclient' does.) Emacs
obeys silently; it does not immediately switch to the new file's
buffer. When you want to do that, type `C-x #' (`server-edit').
When you've finished editing that buffer, type `C-x #' again. This
saves the file and sends a message back to the `emacsclient' program
telling it to exit. The programs that use `EDITOR' wait for the
"editor" (actually, `emacsclient') to exit. `C-x #' also checks to see
if any other files are pending for you to edit, and selects the next
You can switch to a server buffer manually if you wish; you don't
have to arrive at it with `C-x #'. But `C-x #' is the only way to say
that you are "finished" with one.
If you set the variable `server-window' to a window or a frame, `C-x
#' displays the server buffer in that window or in that frame.
While `mail' or another application is waiting for `emacsclient' to
finish, `emacsclient' does not read terminal input. So the terminal
that `mail' was using is effectively blocked for the duration. In
order to edit with your principal Emacs, you need to be able to use it
without using that terminal. There are two ways to do this:
* Using a window system, run `mail' and the principal Emacs in two
separate windows. While `mail' is waiting for `emacsclient', the
window where it was running is blocked, but you can use Emacs by
* Use Shell mode in Emacs to run the other program such as `mail';
then, `emacsclient' blocks only the subshell under Emacs; you can
still use Emacs to edit the file.
Some programs write temporary files for you to edit. After you edit
the temporary file, the program reads it back and deletes it. If the
Emacs server is later asked to edit the same file name, it should assume
this has nothing to do with the previous occasion for that file name.
The server accomplishes this by killing the temporary file's buffer when
you finish with the file. Use the variable `server-temp-file-regexp'
to specify which files are temporary in this sense; its value should be
a regular expression that matches file names that are temporary.
automatically generated by info2www