Selected Article

Emacs Server on Linux and Windows

Posted on: 2025-06-06, Updated on: 2025-06-06 22:35:29 | Read time: 3.7 minutes

Topic(s): emacs & unix/linux

GNU Emacs provides a form of background server process, dubbed “daemon” on UNIX, which can persist session information and accept connections from client instances of Emacs. The Emacs server daemon can be especially useful if you have Emacs open 24/7. When starting Emacs with emacsclient, the configuration has already been loaded and new frames (Emacs's term for GUI windows) will open instantaneously. This article presents one method to automatically start the Emacs daemon on both Linux and Windows user logins.

Linux

On Linux, getting the Emacs daemon to start for your user account on login can be done easily using XDG .desktop files and XDG autostart entries in the ~/.config/autostart/ directory, which starts Emacs with the --daemon flag.

~/.config/autostart/emacs-daemon.desktop:

[Desktop Entry]
Comment[en_US]=
Comment=
Exec=emacs --daemon
GenericName[en_US]=
GenericName=
Icon=
MimeType=
Name[en_US]=emacs-daemon
Name=emacs-daemon
Path=
StartupNotify=true
Terminal=false
TerminalOptions=
Type=Application
X-KDE-SubstituteUID=false
X-KDE-Username=

If you are using KDE Plasma 5, you should be able to see the newly created autostart entry in System Settings -> Startup and Shutdown -> Autostart. After the next desktop login, you should be able to use the Emacs (Client) entry in your desktop environment's application menu to connect to the running Emacs server.

If the application shortcut mentioned above isn't available, you can use the command emacsclient -n -c -a "" to connect to the running server instance.

Windows

I have found that the easiest way to start the Emacs daemon on Windows is to use two shortcuts: one for starting the daemon and another for starting the client. I will present a script for setting these up shortly, but the process involved is as follows:

  1. Create one shortcut which starts Emacs with the --daemon command line argument. The working directory should be set to the path of the Emacs executable. This shortcut should be placed in %APPDATA%\Microsoft\Windows\Start Menu\Programs\Startup\Emacs, which will cause the Emacs daemon starting automatically at user login. A window with console output for the Emacs daemon will open which you can use to debug any initialization errors.
  2. Create a shortcut for the Emacs client with command line arguments: “-nc”. Note that on Windows, the executable for the Emacs client is called emacsclientw.exe with an added “w” character. This shortcut can be placed in %APPDATA%\Microsoft\Windows\Start Menu\Emacs alongside the other Emacs start menu shortcuts.
  3. The Emacs icons can be found in root of the Emacs install under share\icons\ and can be assigned to both shortcuts.

I made the following PowerShell script to setup the shortcuts automatically. To use the script, open PowerShell, and change to the directory where you downloaded the script, for example: cd .\Downloads.

Create-Shortcuts.ps1:

#
# Create-Shortcuts.ps1 - Use to create Emacs server and client shortcut for Windows.
#
Param(
    [Parameter(Mandatory)]
    # Set version using the trailing numbers of the path C:\Program Files\Emacs\emacs-29.3_2
    # In the above path use 29.3_2 as the argument.
    [String]$EmacsVersion,
    [String]$EmacsPath = "C:\Program Files\Emacs"
)
# Root location of Emacs location. From $EmacsPath should be something like \emacs-<VERSION>.
New-Variable -Option ReadOnly -Name EmacsRootPath -Value "$EmacsPath\emacs-$EmacsVersion"
If (-Not (Test-Path -Path $EmacsRootPath)) {
    Write-Error "The given Emacs root path does not exist: $EmacsRootPath"
    Exit # Exit if path doesn't exist
}

Write-Host "Root path: $EmacsRootPath"
# Specify the path to the icon file and the index of the icon within the file
$IconPath = "$EmacsRootPath\share\icons\hicolor\scalable\apps\emacs.ico"
$IconIndex = 0 # Index of the icon within the icon file (0 for the first icon)
Function CreateShortcut($TargetPath, $Arguments, $ShortcutPath) {
    $Shell = New-Object -ComObject WScript.Shell # Create WScript.Shell COM object

    $Shortcut = $Shell.CreateShortcut("$ShortcutPath.lnk") # Create the shortcut
    $Shortcut.TargetPath = $TargetPath
    $Shortcut.Arguments = $Arguments
    $Shortcut.WorkingDirectory = "$EmacsRootPath\bin" # Set start in path to "C:\Program Files\Emacs\emacs-29.3_2\bin"
    $Shortcut.IconLocation = "$IconPath,$IconIndex" # Set icon path and index
    $Shortcut.Save()

    [System.Runtime.InteropServices.Marshal]::ReleaseComObject($Shell) | Out-Null # Release the COM object
}

# Create startup daemon shortcut
CreateShortcut "$EmacsRootPath\bin\emacs.exe" "--daemon" "$env:APPDATA\Microsoft\Windows\Start Menu\Programs\Startup\Emacs"
# Create client start menu shortcut
CreateShortcut "$EmacsRootPath\bin\emacsclientw.exe" "-nc" "$env:APPDATA\Microsoft\Windows\Start Menu\Emacs"

To use the Create-Shortcuts.ps1 script, use the command ./Create-Shortcuts.ps1 -EmacsVersion <VERSION>, replacing <VERSION> with the trailing number of the Emacs installation path. If the Emacs installation path is named C:\Program Files\Emacs\emacs-29.3_2, the value for EmacsVersion should be supplied as -EmacsVersion "29.3_2". You can supply the argument -EmacsPath <PATH> if your installation location differs from the default, replacing <PATH> with the installation location. The default value is set to "C:\Program Files\Emacs".

Thanks for reading!

Resources:


Post a Comment

Click to generate a new captcha.

0 Comments