Previous: Emacs Server, Up: Emacs Server


42.1 Invoking emacsclient

To run the emacsclient program, specify file names as arguments, and optionally line numbers as well. Do it like this:

     emacsclient {[+line[column]] filename}...

This tells Emacs to visit each of the specified files; if you specify a line number for a certain file, Emacs moves to that line in the file. If you specify a column number as well, Emacs puts point on that column in the line.

Ordinarily, emacsclient does not return until you use the C-x # command on each of these buffers. When that happens, Emacs sends a message to the emacsclient program telling it to return.

But if you use the option ‘-n’ or ‘--no-wait’ when running emacsclient, then it returns immediately. (You can take as long as you like to edit the files in Emacs.)

The option ‘--alternate-editor=command’ is useful when running emacsclient in a script. It specifies a command to run if emacsclient fails to contact Emacs. For example, the following setting for the EDITOR environment variable will always give you an editor, even if no Emacs server is running:

     EDITOR="emacsclient --alternate-editor emacs +%d %s"

The environment variable ALTERNATE_EDITOR has the same effect, but the value of the ‘--alternate-editor’ takes precedence.

Alternatively, the file etc/emacs.bash defines a bash function which will communicate with a running Emacs server, or start one if none exists.

If you use several displays, you can tell Emacs on which display to open the given files with the option ‘--display=DISPLAY’. This can be used typically when connecting from home to an Emacs server running on your machine at your workplace.

If there is more than one Emacs server running, you can specify a server name with the option ‘-s name’.

You can also use emacsclient to execute any piece of Emacs Lisp code, using the option ‘--eval’. When this option is given, the rest of the arguments is not taken as a list of files to visit but as a list of expressions to evaluate.