GNU Emacs

From Gentoo Wiki
Jump to:navigation Jump to:search

GNU Emacs is a powerful, extensible, self-documenting text editor. It is released by the Free Software Foundation and has been under development since 1976.

In Gentoo, GNU Emacs is maintained by the team of the same name, which can be reached through Detailed developer information can be found on the project page.


USE flags

Some packages have the USE=emacs, which usually adds emacs related functionality.

USE flags for app-editors/emacs The extensible, customizable, self-documenting real-time display editor

X Add support for X11
Xaw3d Add support for the 3d athena widget set
acl Add support for Access Control Lists
alsa Add support for media-libs/alsa-lib (Advanced Linux Sound Architecture)
aqua Include support for the Mac OS X Aqua (Carbon/Cocoa) GUI
athena Enable the MIT Athena widget set (x11-libs/libXaw)
cairo Enable support for the cairo graphics library
dbus Enable dbus support for anything that needs it (gpsd, gnomemeeting, etc)
dynamic-loading Enable loading of dynamic libraries (modules) at runtime
games Support shared score files for games
gfile Use gfile (dev-libs/glib) for file notification
gif Add GIF image support
gmp Use the GNU multiple precision arithmetic library (dev-libs/gmp) instead of the bundled mini-gmp subset
gpm Add support for sys-libs/gpm (Console-based mouse driver)
gsettings Use gsettings (dev-libs/glib) to read the system font name
gtk Add support for x11-libs/gtk+ (The GIMP Toolkit)
gui Enable support for a graphical user interface
gzip-el Compress bundled Emacs Lisp source
harfbuzz Use media-libs/harfbuzz as text shaping engine
imagemagick Use media-gfx/imagemagick for image processing
inotify Enable inotify filesystem monitoring support
jit Compile with Emacs Lisp native compiler support via libgccjit
jpeg Add JPEG image support
json Compile with native JSON support using dev-libs/jansson
kerberos Add kerberos support
lcms Add lcms support (color management engine)
libxml2 Use dev-libs/libxml2 to parse XML instead of the internal Lisp implementations
livecd !!internal use only!! DO NOT SET THIS FLAG YOURSELF!, used during livecd building
m17n-lib Enable m17n-lib support
mailutils Retrieve e-mail using net-mail/mailutils instead of the internal movemail substitute
motif Add support for the Motif toolkit
png Add support for libpng (PNG images)
selinux !!internal use only!! Security Enhanced Linux support, this must be set by the selinux profile or breakage will occur
small-ja-dic Generate a smaller-size Japanese dictionary (saves about 5 MiB in installed image)
sound Enable sound support
source Install C source files and make them available for find-function
sqlite Add support for sqlite - embedded sql database
ssl Add support for SSL/TLS connections (Secure Socket Layer / Transport Layer Security)
svg Add support for SVG (Scalable Vector Graphics)
systemd Enable use of systemd-specific libraries and features like socket activation or session tracking
threads Add elisp threading support
tiff Add support for the TIFF image format
toolkit-scroll-bars Use the selected toolkit's scrollbars in preference to Emacs' own scrollbars
tree-sitter Support the dev-libs/tree-sitter parsing library
valgrind Enable annotations for accuracy. May slow down runtime slightly. Safe to use even if not currently using dev-util/valgrind
webp Add support for the WebP image format
wide-int Prefer wide Emacs integers (typically 62-bit). This option has an effect only on architectures where "long" and "long long" types have different size.
xattr Add support for extended attributes (filesystem-stored metadata)
xft Build with support for XFT font renderer (x11-libs/libXft)
xpm Add support for XPM graphics format
xwidgets Enable use of GTK widgets in Emacs buffers (requires GTK3)
zlib Add support for zlib (de)compression

For Xorg or Wayland, at least USE="gui gtk" is required. For Wayland, beginning with emacs 29, it is recommended USE="-X gtk": it will enable "pure GTK mode" bug #831154.

There are other options for GUI toolkits:

  • USE flags for toolkits USE=gtk, USE=motif, USE=athena, and USE=aqua are mutually exclusive.
  • Generally, USE=gtk is a good choice for systems with one display.
  • However, if intending to use Emacs as a daemon only, USE="motif -athena -gtk -Xaw3d" or USE="athena Xaw3d -gtk -motif" is recommended instead because of bug #292471.
  • USE="athena Xaw3d" resembles USE=gtk very well.
  • USE=aqua is a special case applying only to macOS.


Emacs can use gcc to compile Emacs Lisp code to native binaries (they have .eln suffix) which gives a nice performance boost.

To use it, activate USE=jit for app-editors/emacs and sys-devel/gcc:

FILE /etc/portage/package.use
app-editors/emacs jit
sys-devel/gcc jit

Then complete a world upgrade for these changes to take effect:

root #emerge --ask --update --changed-use @world
The Gentoo ebuild compiles the whole emacs distribution Lisp code:
Emacs packages do not yet handle JIT by themselves: bug #735148.


Emacs can be built to the use "pgtk" (Pure GTK) frontend: Emacs will no longer use X11 APIs directly, instead it only uses GTK (and Cairo), hence (in theory) any platform supported by GTK, such as Wayland (without the need for XWayland).

To activate pgtk, disable USE=X and enable USE=gui and USE=gtk for app-editors/emacs:

FILE /etc/portage/package.use
app-editors/emacs gui gtk -X

Then run:

root #emerge --ask --update --changed-use @world

This will also get rid of the annoying warning of:

Warning: due to a long standing Gtk+ bug
Emacs might crash when run in daemon mode and the X11 connection is unexpectedly lost.
Using an Emacs configured with --with-x-toolkit=lucid does not have this problem.


To use Emacs for ERC or when desiring to download packages from ELPA/MELPA via Emacs, it is wise to compile with the ssl USE flag. Emacs will use the program gnutls-cli (provided by net-libs/gnutls), but only when it is also compiled with the tools USE flag. The tools USE flag is not enabled by default.


To install Emacs, run:

root #emerge --ask app-editors/emacs

Emacs supports various GUI toolkits to present its output.

Several versions side-by-side

In Gentoo, several Emacs versions can be installed on a system simultaneously. The upstream version already installs elisp and data files into versioned subdirectories. To avoid file collisions between slots, in Gentoo binaries and man pages are suffixed with their corresponding version number, too.

The eselect module from app-eselect/eselect-emacs can be used to link /usr/bin/emacs and its auxiliary programs to the ones belonging to the desired Emacs version. Consult the eselect user guide for details on eselect.


Emacs can be customized by clicking through the GUI (use M-x customize-group RET).

Emacs can also be configured by using the ~/.emacs configuration file, which is written in Emacs Lisp, Emacs' own Lisp dialect. The ~/.emacs file is not automatically created on installation or on first invocation, though the directory ~/.emacs.d can be.



Customization is available via /etc/conf.d/emacs and /etc/conf.d/emacs.<user>

Gentoo provides OpenRC init scripts for Emacs in the package app-emacs/emacs-daemon:

root #emerge --ask app-emacs/emacs-daemon

First, create a symlink for at least one user in /etc/init.d:

root #ln -s /etc/init.d/emacs /etc/init.d/emacs.myuser

Then add it to the appropriate runlevel:

root #rc-update add emacs.myuser default

Finally, start up the new Emacs service:

root #/etc/init.d/emacs.myuser start

Usually, an Emacs server socket will be created for emacsclient to connect to. If the XDG_RUNTIME_DIR variable is set at the time the Emacs server is started, the socket will be /run/user/<user_id>/emacs/server. If that variable is not set at the time the Emacs server is started, the socket will be created at /tmp/emacs<user_id>/server, e.g. /tmp/emacs1000/server. To use emacsclient with this socket, use the -s / --socket-name option, e.g.:

user $emacsclient --socket-name='/tmp/emacs1000/server'
Starting an Emacs server during boot, and thus in the absence of a D-Bus session bus, will mean the DBUS_SESSION_BUS_ADDRESS environment variable is not set appropriately within the server's environment. This might cause various issues, e.g. Emacs unable to opening a new tab in an existing Firefox instance. To fix this, add the following shell command to the startup file of your window manager or desktop environment (using the --socket-name option if necessary):
emacsclient --eval "(progn (setenv \"DBUS_SESSION_BUS_ADDRESS\" \"${DBUS_SESSION_BUS_ADDRESS}\")  (dbus-init-bus))"
Note that Emacs 28.2 and earlier have an issue where this use of setenv would not work; this has been fixed in commit 1383efe01d of 2023-06-12. The fix has been applied to app-editors/emacs-{28.2-r9,28.3_rc1-r4} and later.
Wayland users will also need to manually set the WAYLAND_DISPLAY variable. To do so, add the following shell command to the startup file of your window manager or desktop environment (using the --socket-name option if necessary):
emacsclient --eval "(setenv \"WAYLAND_DISPLAY\" \"${WAYLAND_DISPLAY}\")"


With systemd, simply enable the upstream user unit:

user $systemctl --user enable --now emacs-28

Builtin method

The user can also start the daemon inside an already running instance of emacs. For that, type M-x server-start RET (that is, Meta, or Alt, followed by x server-start, and finally Enter).

Connect to emacs daemon

user $emacsclient --socket-name=/tmp/emacs$(id -u)/server file
If user starts the daemon inside emacs, everything works out-of-the-box. Otherwise, in order to save typing while connecting to the emacs daemon, export EMACS_SOCKET_NAME in a user's profile file, as in the example above. See bug #794649. This method also guarantees that exporting EDITOR=emacsclient will work as expected.


Exiting Emacs

For beginners who don't know the key combinations, it may be difficult to exit Emacs. To close Emacs, type C-x C-c (Ctrl+x followed by Ctrl+c).


For a quick-start documentation, type in Emacs: C-h t (Ctrl+h followed by t). For further help on how to use Emacs, start emacs and type C-h r (Ctrl+h followed by r).

Starter Kits

Since Emacs supports so many features and additional packages, configuration and customization can become cumbersome, an Emacs distribution may help starting out. They aim to provide a "batteries included" approach and plug together many features and packages automatically:

Vim controls

Contrary to popular belief, many Emacs users prefer to get the best out of "both worlds". To do so, Vim and Emacs features can be combined (and used alongside), for example, with Evil.

Spacemacs and Doom support this out of the box.

Additional elisp packages

Emacs has lots of additional packages written in elisp.

Users have two choices:

  1. If installing packages per-user, package.el is recommended. Other methods exist too, like straight.el.
  2. If installing packages through the system's package manager is preferred, then only emerge is needed. gs-elpa can be used to create ebuilds if one does not already exist for a needed package.

gs-elpa is a tool to generate ebuilds for elisp packages where no ebuild already exists.

To install elisp packages system-wide under Portage's control, use gs-elpa. Read eselect-repository documentation before using it, as gs-elpa represents ELPA repositories as g-sorcery overlays.

It currently supports 4 repositories:

  1. gnu-elpa
  2. marmalade
  3. melpa
  4. melpa-stable

Once eselect repository is setup, install gs-elpa:

root #emerge --ask app-portage/gs-elpa

Start adding elisp repos and emerging packages, e.g.:

root #eselect repository list
root #eselect repository enable gnu-elpa
root #eselect repository enable melpa-stable
root #emerge --ask app-emacs/starter-kit app-emacs/starter-kit-bindings app-emacs/starter-kit-eshell app-emacs/starter-kit-lisp
Always add the gnu-elpa repository first, as other repos depend on it.

Bugs related to gs-elpa should be reported on its issue tracker.

If bugs like this are encountered, packages can be excluded from dependencies adding their name to the "external" object in the "common-config" section of /etc/g-sorcery/gs-elpa.json configuration file. For the example issue, it would be (together with already added packages):

FILE /etc/g-sorcery/gs-elpa.json
"external": {"emacs": "virtual/emacs", "cl-lib": "virtual/emacs", "eieio": "virtual/emacs"}

See also