Skip to content

Latest commit

 

History

History
369 lines (267 loc) · 16.6 KB

File metadata and controls

369 lines (267 loc) · 16.6 KB

Using the Emacs stand-alone processes, daemon and clients

Home URL:https://github.com/pierre-rouleau/pel
Project:PEL -- Pragmatic Emacs Leverage
Ref:PEL How-to Tips
Created:Thursday, May 23 2024.
Author: Pierre Rouleau <prouleau001@gmail.com>
Modified:2025-08-30 15:43:20 EDT, updated by Pierre Rouleau.
Copyright: © 2024, 2025, 2026, Pierre Rouleau

Short Commands to start Emacs from the terminal shell

Instead of using the long explicit commands listed in the sections below, you can use the following short commands. These support both macOS and Linux.

. Shell Command Description
. e[OPTIONS..] [FILE..]

Start Emacs in text mode.

  • A stand-alone process.
  • A command that you execute from a shell.
  • Supports emacs options and file arguments. See emacs man page.
  • Emacs has access to the shell environment (PATH, LIBPATH, MANPATH, etc...) which can be useful in many situations. You can even launch different Emacs processes from different shells, each ones having different specialized environments.
.

ge :

  • ge [-h | --help]
  • ge [OPTIONS..] [FILE..]

Start Emacs in graphics mode.

  • A stand-alone process.
  • A command that you execute from a shell.
  • Supports emacs options and file arguments. See emacs man page.
  • Emacs has access to the shell environment (PATH, LIBPATH, MANPATH, etc...) which can be useful in many situations. You can even launch different Emacs processes from different shells, each ones having different specialized environments.
  • With PEL, it can use a different customization file than the text mode Emacs.
.

ec :

  • ec [-h|--help]
  • ec [-g|gui] [--name=NAME] [FILE...]
  • ec --start-daemon [--name=NAME]
  • ec --check-daemon [--name=NAME]

Start an Emacs client for an Emacs daemon.

  • Use ec to start an Emacs client to the default Emacs daemon, starting the daemon if it is not already running. The client is a terminal based client by default.
  • To start a graphical (GUI) Emacs client instead, use ec -g or ec --gui. This also starts the Emacs daemon if it is not already running.
  • By default the ec command launches and connects to the default socket name. Use the --name option to specify another name (for both the daemon and its clients). For example, use ec --name=PEL to start and connect to a Emacs daemon that uses the socket named PEL.
    • With this option it is possible to create several independent groups of Emacs daemon/clients.
  • Use ec --start-daemon (optionally specifying the daemon name with --name) to start the Emacs daemon without starting the client. If the Emacs daemon is already running the command prints a message and exits with the exit code 1.
  • To explicitly check if the Emacs daemon for the default socket is running use the ec --check-daemon command. To check for the Emacs daemon that uses a named socket use the same command and specify the --name=NAME option.

The PEL_GE_DIR environment variable for macOS and locally built Emacs

Note that on macOS, if you use a locally built Emacs executable file that has not been activated as the system Emacs, and you want to use that Emacs executable in graphics mode with the ge and ec commands, you will also need to set the ``PEL_GE_DIR``environment variable to the name of the directory holing that Emacs executable file.

For Closing and Terminating Emacs

For instructions on how to close/terminate the Emacs daemon and its clients, see the Closing Suspending PEL PDF

PEL also provides a Emacs launcher that installs the above scripts in your environment. See the quick installation script page for more information.

Test environments:

The commands were tested in the following environments:

  • macOS Sonoma 14.5, Apple Silicon
  • macOS Mojave 10.14.6, i386 architecture
  • Linux Ubuntu 16.04, i386 architecture
  • Linux Ubuntu 22.04, arm64 architecture

Using Emacs daemon on macOS

Goal: Provide ability to launch a graphical (GUI) or text-based (TTY) Emacs client from a macOS terminal.

Challenge: Several people, including myself, had problems launching the graphical based Emacs client from the terminal, regardless of the emacsclient command line used. All instances of Emacs client programs ended up being terminal based only.

Solution: The key is to use an Emacs binary that supports Emacs in graphics mode to launch the Emacs daemon. Once this done, it becomes possible to launch graphical Emacs clients and text-based Emacs clients:

  • To launch a graphical based Emacs client use a graphics-capable emacsclient binary.
  • To launch a text based Emacs client, you can use two methods:
    • Use a graphics-capable Emacs and issue the command emacsclient -nw
    • Use a text-based Emacs and issue one of the following 2 commands:
      • emacsclient -c or
      • emacsclient -nw

On macOS, the graphics-capable Emacs is one of the Emacs binary files inside the /Applications/Emacs.app universal DMG directory tree from Emacs for Mac OS X installed manually or from the Homebrew brew install --cask emacs command.

Other Considerations

Under macOS (as of macOS Sonoma in 2024), if you want to launch an independent terminal-based Emacs process from the terminal shell, it is best to use an Emacs binary that was built without graphical capabilities because it starts faster than an Emacs that was built with graphical capabilities.

Because of that you may want to install two instances of Emacs on macOS:

  • one with graphical capabilities and
  • one that does not have them built-in.

Fortunately it's possible to do both without having to build Emacs locally: Homebrew supports installing both as long as you follow some steps to trick it.

Installing Emacs on macOS

First install the graphics capable version of macOS. This can be done in two different ways:

Both install the same macOS universal DMG from Emacs for Mac OS X.

At this point, Homebrew won't let you install another version of emacs. You need to trick it.

  • First copy the Emacs DMG ( /Applications/Emacs.app ) somewhere else.
  • Next, uninstall Homebrew's installed (cask) Emacs with: brew uninstall --cask emacs.
  • Then install the terminal-only version of Emacs with Homebrew by issuing the following command: brew install emacs
  • Finally, put your Emacs.app copy back into /Applications so you will now also have /Applications/Emacs.app

At this point, if you type which emacs you should see that it is installed inside a Homebrew controlled directory. For macOS running on Apple Silicon processors, that's /opt/homebrew/bin/emacs.

The graphical version of Emacs is located inside the following directory:

>0@14:53:40 L3 roup@macStudio1:/Applications/Emacs.app/Contents/MacOS
zsh% ls -lFGO
total 156416
-rwxr-xr-x@  1 roup  admin  -  4085936 24 Mar 10:18 Emacs*
-rwxr-xr-x@  1 roup  admin  -  8236224 24 Mar 10:18 Emacs-arm64-11*
-rw-r--r--@  1 roup  admin  - 16995328 24 Mar 10:18 Emacs-arm64-11.pdmp
-rwxr-xr-x@  1 roup  admin  -  8469888 24 Mar 10:18 Emacs-x86_64-10_11*
-rw-r--r--@  1 roup  admin  - 16922656 24 Mar 10:18 Emacs-x86_64-10_11.pdmp
-rwxr-xr-x@  1 roup  admin  -  8349024 24 Mar 10:18 Emacs-x86_64-10_14*
-rw-r--r--@  1 roup  admin  - 16995160 24 Mar 10:18 Emacs-x86_64-10_14.pdmp
lrwxr-xr-x   1 roup  admin  -       16 22 May 08:31 bin@ -> bin-x86_64-10_11
drwxr-xr-x@  6 roup  admin  -      192 24 Mar 10:18 bin-arm64-11/
drwxr-xr-x@  6 roup  admin  -      192 24 Mar 10:18 bin-x86_64-10_11/
drwxr-xr-x@  6 roup  admin  -      192 24 Mar 10:18 bin-x86_64-10_14/
-rwxr-xr-x@  1 roup  admin  -      281 24 Mar 10:18 emacs-nw*
-rwxr-xr-x@  1 roup  admin  -    11272 24 Mar 10:18 launch.rs*
drwxr-xr-x@ 50 roup  admin  -     1600 24 Mar 10:18 lib-arm64-11/
drwxr-xr-x@  8 roup  admin  -      256 24 Mar 10:18 lib-x86_64-10_11/
drwxr-xr-x@ 51 roup  admin  -     1632 24 Mar 10:18 lib-x86_64-10_14/
lrwxr-xr-x   1 roup  admin  -       20 22 May 08:31 libexec@ -> libexec-x86_64-10_11
drwxr-xr-x@  5 roup  admin  -      160 24 Mar 10:18 libexec-arm64-11/
drwxr-xr-x@  5 roup  admin  -      160 24 Mar 10:18 libexec-x86_64-10_11/
drwxr-xr-x@  5 roup  admin  -      160 24 Mar 10:18 libexec-x86_64-10_14/
>0@14:53:51 L3 roup@macStudio1:/Applications/Emacs.app/Contents/MacOS
zsh%

And the Apple Silicon ARM64 binaries are see here:

>0@14:55:57 L3 roup@macStudio1:/Applications/Emacs.app/Contents/MacOS/bin-arm64-11
zsh% ls -lFGO
total 1168
-rwxr-xr-x@ 1 roup  admin  - 204752 24 Mar 10:18 ctags*
-rwxr-xr-x@ 1 roup  admin  -  91744 24 Mar 10:18 ebrowse*
-rwxr-xr-x@ 1 roup  admin  -  92912 24 Mar 10:18 emacsclient*
-rwxr-xr-x@ 1 roup  admin  - 204480 24 Mar 10:18 etags*
>0@14:56:03 L3 roup@macStudio1:/Applications/Emacs.app/Contents/MacOS/bin-arm64-11
zsh%

Launching Independent Emacs Processes

The next sections list the explicit commands to launch Emacs in a specific way. These assume Emacs was installed in macOS as described in the previous section.

Launching an independent terminal Emacs process from a terminal shell

Explicit command line:

emacs

or:

emacs -nw

or use the e command described above.

Launching an independent graphical Emacs process from a terminal shell

Explicit command line:

open -n -a /Applications/Emacs.app/Contents/MacOS/Emacs

This uses the macOS open command with the following command line options:

  • -n: open a new instance on each invocation
  • -a application: identify the path of the application

You can also use the ge command described above.

Launching an independent graphical Emacs process from macOS Finder

Assuming no Emacs daemon is running, you can use Spotlight to open Emacs from the keyboard:

  • Type ⌘-SPACE then Emacs and RET

Note that when you use this, Emacs runs under the default OS environment. In some case that may mean that you wont have access to programs you may want to use because they are not present in any directory listed in the systems' PATH.

There are several solutions to this problem.

  1. Use PEL's ability to configure the environment variables from customization: customize PEL's pel-gui-process-environment user-option.
    • Type C-h o pel-gui-process-environment RET from Emacs and access the customization buffer. Then insert new environment variables with their value or identify strings to append or prepend to existing environment variables (like PATH). You can create or update any number of environment variables. The docstring of this customizable user-option provides more information.
  2. Use the exec-path-from-shell package. It launches a shell to extract the environment from it when Emacs starts.

The first method, provided by PEL, is very flexible but it requires setting it through the powerful Emacs customization system. It also has very negligible impact on Emacs startup time. The second method requires installation of another package and assumes that you only have one shell configured the way you want.

Launching a graphics-capable Emacs Daemon

Explicit command line for an Emacs daemon with the default socket name:

/Applications/Emacs.app/Contents/MacOS/Emacs --daemon

If you want to create multiple Emacs daemon, each one must identify a socket by name. The command is:

/Applications/Emacs.app/Contents/MacOS/Emacs --daemon --socket-name=FILENAME

where, according the emacsclient man page:

Use socket named FILENAME for communication. Relative filenames are relative to "$XDG_RUNTIME_DIR/emacs/" or "$TMPDIR/".

See also the EMACS_SOCKET_NAME environment variable, over which this option takes precedence.

Opening Emacs Clients to the running Emacs Daemon

Opening a graphics-based Emacs client

Explicit command for macOS running on Apple Silicon:

/Applications/Emacs.app/Contents/MacOS/bin-arm64-11/emacsclient -c -n

Opening a text-based Emacs client

Explicit command using the mac OS X Emacs, for macOS on Apple Silicon:

/Applications/Emacs.app/Contents/MacOS/bin-arm64-11/emacsclient   -nw

Explicit command using the text-based Emacs installed with Homebrew brew install emacs command:

emacsclient -nw