EmacsClient allows one to open a file for editing in an already running Emacs. Because it doesn’t start a new Emacs instance at each invocation, you can setup EmacsClient as the default editor, e.g. in the environment variable EDITOR or VISUAL. EmacsClient is part of and works only in conjunction with GNU Emacs. The program GnuClient provides the corresponding functionality for XEmacs, but also works with GnuEmacs.

Start the Emacs Server

Starting Interactively

To enable the Emacs server, add the command


to your init.el. This is a requirement for using the Emacs client! When the server is running, closing the last Emacs frame will leave the server running. To shut Emacs down completely, call the command `(kill-emacs)’.

Emacs Daemon

The command

 emacs --daemon

will launch an emacs in daemon mode, starting the server, running the init files and then detaching into the background. This is handy for automatically running at launch. This can save you a few seconds since Emacs will already be running by the time you open your first file! This was not supported on Windows before version 25.

EmacsAsDaemon details various ways to run this command in different distributions, init scripts vs systemd and so on.

Using EmacsClient

Terminal environment

Simply replace every place you’d run emacs with this command instead.

emacsclient -create-frame --alternate-editor=""

The emacsclient program will connect to a running instance of Emacs if it exists. -c tells Emacs to open the file in a new frame, which is optional. Specifying -a=”” tells emacsclient to start an an instance of emacs if it cannot find one already running.

To use emacsclient as your editor, add the following to your .bashrc:

export EDITOR="emacsclient -t"                  # $EDITOR opens in terminal
export VISUAL="emacsclient -c -a emacs"         # $VISUAL opens in GUI mode

If you prefer to launch GUI Emacs from your terminal when editing files, replace the -t in the second line with -c

Desktop Environment

You can install a desktop entry for Emacsclient. This will make it available in context menus in Gnome, KDE, XFCE, and other Freedesktop environments. Create a file named emacsclient.desktop in ~/.local/share/applications containing the following:

[Desktop Entry]
Name=Emacs (Client)
GenericName=Text Editor
Comment=Edit text
Exec=emacsclient -a "emacs" %F


See EmacsMsWindowsIntegration for a fairly complete and workable solution for Emacs 23/24, using stock Emacs builds. EmacsMsWindowsIntegration contains info on how to set up taskbar/Start-menu shortcuts and make Emacs open particular file types using emacsclient.

See MsWindowsGlobalContextMenu to add Emacs client to the right click context menu in Explorer.

WThirtyTwoFileAssociations to see how to associate file extensions to open with Emacs client on Windows.

ExternalDotExe shows a way to edit text fields in many applications with Emacs client.

Custom commands and configurations

Using emacsclient to make a new frame of a remote Emacs 22 on a local display:

    ssh -Y remote_host -f emacsclient --eval '"(make-frame-on-display \"$DISPLAY\")"'

Edit in a new frame or in the current frame

Open a new frame from you shell by adding to your .bashrc or .zshrc

   new-frame() {
      emacsclient -e "(new-frame)"

Tiling Window Managers

See TilingWindowManagers for info about using Emacsclient with tiling WMs.

Making C-x k end an emacsclient session

If your fingers are wired to using C-x k to kill off buffers (and you dont like having to type C-x #) then try this :

  (add-hook 'server-switch-hook
            (lambda ()
              (when (current-local-map)
                (use-local-map (copy-keymap (current-local-map))))
	      (when server-buffer-clients
		(local-set-key (kbd "C-x k") 'server-edit))))

Functionality similar to kill-emacs-query-functions for clients

‘kill-emacs-query-functions’ allows one to call functions that query the user about killing emacs, with the potential for aborting the quit. i have a simple check in my .emacs that works fine for a standalone emacs process but doesn’t get called when exiting an emacs client.

 (setq kill-emacs-query-functions
      (cons (lambda () (yes-or-no-p "Really kill Emacs? "))

Using Emacsclient with Gnu Screen

See here: GnuScreen

Opening Server Files Always in a New Frame

To open the files designated by emacsclient in their own frame, you can use the following hook as a Server Switch Hook which can be setup in customize-group <RET> Server <RET>, or in your InitFile, as such:

    (add-hook 'server-switch-hook
              (lambda nil
                (let ((server-buf (current-buffer)))
                  (switch-to-buffer-other-frame server-buf))))

If you’re setting this up via M-x customize, you’ll of course want to drop the (add-hook) call.

You might also want to close the frame when you’re done with it. To do this, setup a Server Done Hook in the same section to call delete-frame.

    (add-hook 'server-done-hook 'delete-frame)

I also like emacs to cleanup stuff when I finish the emacsclient, so I add another hook to kill the buffer when finished. This also takes care of weird buffer switching behaviors when closing client buffers.

    (add-hook 'server-done-hook (lambda nil (kill-buffer nil)))

The whole thing is actually in my .emacs-custom file:

   '(server-done-hook (quote ((lambda nil (kill-buffer nil)) delete-frame)))
   '(server-switch-hook (quote ((lambda nil (let (server-buf) (setq server-buf (current-buffer)) (bury-buffer)      (switch-to-buffer-other-frame server-buf))))))


You can also use ‘server-kill-new-buffers’ instead of calling ‘kill-buffer’ in your ‘server-done-hook’, e.g.

    (custom-set-variables '(server-kill-new-buffers t))
    (add-hook 'server-done-hook (lambda () (delete-frame)))


Setting Up emacsclient to Run Emacs Only on X11

I’ve found it very useful to set my $EDITOR to emacsclient, and let it judge wether or not to start emacs, based on the $DISPLAY variable, which is often an indication of running on a faster machine for me. So I’ve wrote the following script, and installed it as /usr/bin/emacsclient.vim.

#! /bin/sh


if [ -z "$DISPLAY" ]; then
  exec emacsclient.emacs21 -a vim "$files"
  exec emacsclient.emacs21 -a emacs "$files"

Note that I avoided calling directly emacsclient, which might be an alternative that points to emacsclient.vim, which would lead to an horrible recursion.

To complete the setup on Debian, I’ve installed this script as an alternative to the editor command:

update-alternatives --install /usr/bin/editor editor /usr/bin/emacsclient.vim 50

Now I can choose emacsclient.vim as my editor using:

update-alternatives --config editor


Piping stdout to an Emacs buffer using emacsclient

See EmacsPipe.

Setting frame colors differently for graphical clients and console clients

See: SettingFrameColorsForEmacsClient


Color coding (multiple) emacs server instances

You can start different emacs server instances w/ different color schemes (so you instantly know which one your using).

This works to set the background color:

emacs --daemon=todo --background-color='#93DB80'
emacs --daemon=edit --background-color=yellow

But if you try setting the cursor color – it fails miserably.

Another, more flexible option:

starting emacs server (daemon)

Example scripts for starting different instances of emacs server:


export ESRVNAME=todo
emacs --daemon=$ESRVNAME



export ESRVNAME=edit
emacs --daemon=$ESRVNAME
ps -ef | grep -i 'emacs --daemon'

Also see http://www.gnu.org/s/libtool/manual/emacs/Emacs-Server.html#Emacs-Server

set color choices in .emacs

Set your color choices in your .emacs file:

;; set colors for my emacs server instances
(defvar esrvname (getenv "ESRVNAME")) ;; is there a var that provides the server-name?
(ignore-errors ;; this prevents error when environment variable is not set
  (when (string-match "todo" esrvname)
    (setq default-frame-alist
          (append default-frame-alist
                  '((background-color . "#93DB80")
                    (cursor-color . "yellow")
    ;; or set a color-theme, e.g., (color-theme-classic)
  (when (string-match "edit" esrvname)
    (setq default-frame-alist
          (append default-frame-alist
                  '((background-color . "yellow")
                    ;; (foreground-color . "black")
                    (cursor-color . "blue")

starting emacsclient

An example script for starting emacsclient (for different instances of emacs server):


emacsclient -server-file edit -c "$@"

This allows you to pass any parameters to emacsclient, e.g., filenames, or even the ‘-t’ (or ‘-nw’) option (it will override the ‘-c’)

Another example:


umask 0007 # you can set a different umask than your default for emcas session
cd ~/docs/todo/
emacsclient -server-file todo -c ~/docs/todo/work.org &

Also see: http://www.gnu.org/s/libtool/manual/emacs/emacsclient-Options.html#emacsclient-Options

Web Browser Plugins

The Emacs server can be used to edit text fields in web browsers using plugins.

Firefox Add-on: It's All Text

To execute emacsclient From Firefox to call Windows Native Emacs to edit web pages, see Mozilla page and comments about add-on It's all text.

There is a tool in [NxhtmlMode[nXhtml]] that allows you to do this. You can find it in the nXhtml menus under “nXhtml / Tools / External Editor Setup”.

Chrome extension: Edit with Emacs

Edit with Emacs - a Google Chrome browser extension: Source: http://github.com/stsquad/emacs_chrome or https://chrome.google.com/extensions/detail/ljobjlafonikaiipfkggjbhkghgicgoh to install in Chrome. This extension by Alex Bennee allows users to edit web-page TEXTAREAs with Emacs. It’s nice to use Emacs when typing in posts or editing Wiki pages. It uses its own edit-server.el instead of gnuclient or emacsclient currently, and doesn’t let you edit page source yet. (This is not strictly about emacsclient, but seems related.)

MozEx is a Firefox extension which allows the user to use external programs for editing TEXTAREAs, viewing page source, handling mailto, news, telnet and FTP links. http://mozex.mozdev.org/


“No connection could be made because the target machine actively refused it”

The #1 problem people face is a stale or outdated server file. Just rm ~/.emacs.d/server/* and your problem is usually solved.

workaround for emacsclient "can't find socket" error on cocoa/ns build (OS X) via ssh

I find that after starting emacs server (eg, /Applications/Emacs.app/Contents/MacOS/Emacs --daemon) I can start a client session locally, but emacsclient can’t find the socket if I try to create a terminal-only client via ssh (OS X 10.6.2, GNU Emacs compiled with ./configure --with-ns). I’ve seen this reported as a bug (bug#3992: 23.1.50) on bug-gnu-emacs-gnu. Here’s a workaround.

First, you need to make sure that you’re using the right emacsclient binary. Apple distributes GNU Emacs 22 (installed in /usr/bin), but its emacsclient binary is not compatible with GNU Emacs 23.

alias emacsclient="/Applications/Emacs.app/Contents/MacOS/bin/emacsclient"

Then, you need to set the server-socket-dir variable in your .emacs so that the standard path assumed by emacsclient is used instead of the path that Emacs 23 defaults to on MacOS X.

(setq server-socket-dir (format "/tmp/emacs%d" (user-uid)))

Obviously, this needs to be executed before (server-start) is called.

Now you can attach to the server process using the above alias in terminal mode via an ssh session:

 emacsclient -nw

And it also works locally to launch the cocoa GUI:

 emacsclient -c &

I expect that this will fail if you are running multiple servers.


In KDE, you can set File Associations to open in emacsclient instead of KWrite.