Difference between revisions of "Emacs server"

From WikEmacs
Jump to navigation Jump to search
(Discuss starting on Windows)
Line 30: Line 30:
 
To entirely close a server started with <tt>--daemon</tt>, run {{Command|save-buffers-kill-emacs}} in a frame.
 
To entirely close a server started with <tt>--daemon</tt>, run {{Command|save-buffers-kill-emacs}} in a frame.
 
= Adding To OS Startup =
 
= Adding To OS Startup =
== Using Systemd ==
+
== Linux ==
  
This is the preferred method of starting Emacs at launch on most major distributions, including Debian, Ubuntu, Fedora, OpenSUSE, Arch, and derivatives.
+
Configuration through the init daemon <code>systemd</code> is the preferred method of starting Emacs at launch on most major distributions, including Debian, Ubuntu, Fedora, OpenSUSE, Arch, and derivatives.
  
 
To tell systemd to start Emacs on init, place the following script <code>emacsd.service</code> in your systemd user configuration folder, replacing the location of the emacs executable if necessary.  On Debian-based distributions, this file should be named <code>/etc/systemd/user/emacsd.service</code>.   
 
To tell systemd to start Emacs on init, place the following script <code>emacsd.service</code> in your systemd user configuration folder, replacing the location of the emacs executable if necessary.  On Debian-based distributions, this file should be named <code>/etc/systemd/user/emacsd.service</code>.   
Line 53: Line 53:
 
== In OS X ==
 
== In OS X ==
 
<code>launchd</code> is the OSX analog of <code>systemd</code> and can be configured just as easily. If someone is familiar with launchd configuration please fill this in.  
 
<code>launchd</code> is the OSX analog of <code>systemd</code> and can be configured just as easily. If someone is familiar with launchd configuration please fill this in.  
 +
 +
== MS Windows ==
 +
Open the startup folder by running the command <code>shell:startup</code> in a run dialog or by typing it in the Windows Explorer path bar. Place a shortcut to Emacs in this folder to run Emacs at boot. Change the target to <code>emacs --daemon</code> to run start it in daemon mode. The best option for more advanced Emacs daemon-mode configuration on Windows is still not settled, there is some discussion on the Emacs Wiki which could potentially be sifted through.
  
 
[[Category: Server]]
 
[[Category: Server]]

Revision as of 21:25, 22 June 2015

Emacs includes an optional client-server architecture.

Starting the Server

With server-start

To start a server in an existing Emacs session, run M-x server-start. A server started in this manner will close when the last visible Emacs frame closes. If you want Emacs to automatically run a server on startup, add the following to your configuration file.

(server-start)

With --daemon

Emacs 23 and higher provide the --daemon command-line argument, which will cause Emacs to immediately start a server and fork into the background. A server started in this manner is fully daemonized and will remain running even with no visible Emacs frames and after its parent terminal has closed (for example, after an SSH session has disconnected).

Note that, if using --daemon, your configuration file will be run while Emacs is still in a terminal. This may affect your theming if you usually use graphical Emacs.

Note Note: This option is not supported on Windows.

Using Clients

Once the server is started you may use the emacsclient command outside of Emacs to send a file to Emacs for editing, e.g. emacsclient ~/.emacs.d/init.el to modify your init file. On a GNU/Linux system it might be a good idea to set your EDITOR environment variable to emacsclient or emacsclient -t. Using emacsclient without any arguments will cause the calling process to simply pause and wait for the Emacs server to report that it's done editing. Using emacsclient -t will cause the calling process to put up a text-mode frame in the same terminal.

If the server was started using --daemon, you should run emacsclient -t or emacsclient -nc to create a visible frame to interact with.

Finishing Up

Once you have finished with a buffer that was opened via emacsclient you should use the [C-x #] (or M-x server-edit) to properly close the loop. Killing the buffer normally will not send the signal to emacsclient that Emacs has finished with the file.

If you're in an Emacs session that was started using --daemon, [C-x C-c] will only close the frame that it was invoked in. It will also signal to the client that owns the frame that its editing job is done, so emacsclient -c or emacsclient -t as EDITOR will behave much like a full emacs or emacs -nw.

To entirely close a server started with --daemon, run M-x save-buffers-kill-emacs in a frame.

Adding To OS Startup

Linux

Configuration through the init daemon systemd is the preferred method of starting Emacs at launch on most major distributions, including Debian, Ubuntu, Fedora, OpenSUSE, Arch, and derivatives.

To tell systemd to start Emacs on init, place the following script emacsd.service in your systemd user configuration folder, replacing the location of the emacs executable if necessary. On Debian-based distributions, this file should be named /etc/systemd/user/emacsd.service.

[Unit]
Description=Emacs daemon

[Service]
Type=forking
WorkingDirectory=%h
ExecStart=/usr/bin/emacs --daemon
ExecStop=/usr/bin/emacsclient --e "(kill-emacs)"
Restart=always

[Install]
WantedBy=console.target

If systemd is not available on your distribution, you can simply add /usr/bin/emacs --daemon to your crontab. However using systemd is preferable because the process will begin up at boot, i.e. without depending on the cron process to start first.

In OS X

launchd is the OSX analog of systemd and can be configured just as easily. If someone is familiar with launchd configuration please fill this in.

MS Windows

Open the startup folder by running the command shell:startup in a run dialog or by typing it in the Windows Explorer path bar. Place a shortcut to Emacs in this folder to run Emacs at boot. Change the target to emacs --daemon to run start it in daemon mode. The best option for more advanced Emacs daemon-mode configuration on Windows is still not settled, there is some discussion on the Emacs Wiki which could potentially be sifted through.