LinuxCommandLibrary

emacsclient

Connect to and reuse an existing Emacs instance

TLDR

Open a file in an existing Emacs server (using GUI if available)

$ emacsclient [path/to/file]
copy

Open a file in console mode (without an X window)
$ emacsclient [[-nw|--no-window-system]] [path/to/file]
copy

Open a file in a new Emacs window
$ emacsclient [[-c|--create-frame]] [path/to/file]
copy

Evaluate a command, printing the output to stdout, and then quit
$ emacsclient [[-e|--eval]] '([command])'
copy

Specify an alternative editor in case no Emacs server is running
$ emacsclient [[-a|--alternate-editor]] [editor] [path/to/file]
copy

Stop a running Emacs server and all its instances, asking for confirmation on unsaved files
$ emacsclient [[-e|--eval]] '(save-buffers-kill-emacs)'
copy

SYNOPSIS

emacsclient [options] [file...]

PARAMETERS

-a editor
    Use editor if there is no Emacs server running.

-c
    Create a new frame instead of using the existing one.

-t
    Open the file in the current terminal.

-n
    Do not wait for the server to finish.

-s server-file
    Specify the Emacs server file.

-e command
    Evaluate a Lisp command instead of opening a file.

-f frame
    Target command at specific existing frame

DESCRIPTION

The emacsclient command provides a way to quickly edit files using an existing Emacs instance. Instead of launching a new Emacs process each time you want to edit a file, emacsclient connects to a running Emacs server (started with `emacs --daemon` or `emacs --bg-daemon`) and instructs it to open the specified file in a new or existing buffer. This significantly reduces startup time, especially for large or customized Emacs configurations.

When invoked, emacsclient sends a command to the Emacs server, which then handles the file opening. The client then waits (by default) until the Emacs server has finished editing the file, making it suitable for use within scripts and other automated workflows. The behavior concerning waiting can be altered through flags to allow emacsclient to exit sooner.

CAVEATS

The Emacs server must be running for emacsclient to function correctly. The user's EMACS environment variable, when set, overrides the default editor used when no server is active, which would otherwise default to the command `emacs`. emacsclient relies on a properly configured Emacs environment.

USAGE EXAMPLES

Editing a File: `emacsclient myfile.txt`
Editing a File in a new Frame: `emacsclient -c myfile.txt`
Evaluating a Lisp Expression: `emacsclient -e '(message "Hello, world!")'`
Using alternative editor: `emacsclient -a vim myfile.txt` (if emacs server isn't running it will open myfile.txt in vim)

HISTORY

The emacsclient command was developed as part of the Emacs suite to improve editing speed and efficiency. It allows users to leverage a single, persistent Emacs instance for multiple editing sessions, avoiding the overhead of repeated Emacs startup. It became a standard tool for integrating Emacs into workflows where fast file editing is crucial.

SEE ALSO

emacs(1)

Copied to clipboard