Why I use Emacs
Introduction
These days at the lab, I have been having friendly arguments with a
colleague about the merits of Emacs vs Vim.
A long time ago (2001-2006) I was a power Vim user, I could move to
any place in a file without thinking about it, I left ":wq" in all the
files that I edited with other editors and my .vimrc was easily the
most complex configuration file I had on my system. I won't get into
the details of why I switched (long story short: the languages I was
interested in back then, Common Lisp and OCaml, had way better Emacs
modes than Vim did), instead I'll explain some of the reasons why I
have been using Emacs for the past 8 years and why I see myself
sticking with it for many years to come.
Emacs: The Editor
There is a joke that goes "Emacs is a great operating system: it just
lacks a good text editor." It's usually uttered by Vim users who have
the only editor that I consider is superior at manipulating text than
Emacs, and by people using other editors (e.g. Atom, Sublime Text) who
parrot the Vim users.
The truth is that Emacs is a great text editor if you learn to use it
properly. Beyond the basic movement commands of moving one
character/word/sentence/paragraph at a time, Emacs has many commands
that make manipulating text great:
- sexp manipulation: Emacs is written in Lisp, and it has commands
that work at the s-expression level. Great for selecting the
current form you are currently within, moving to the enclosing one,
to the left/right siblings, etc.
- advanced search and replace functionality: like all editors, Emacs
can search for strings, and it can also search for regular
expressions. When you do a search-replace command, the power of
regular expressions is often very helpful to perform complex
manipulations, and if that's not enough, you can even inject Lisp
code in your replacement!
- Macros: if search and replace is not powerful enough, or requires
too much hackery to work properly, one possible avenue is to use
macros. Macros allow you to record your keystrokes and replay them
again later. If you were careful during the initial recording to
use general commands, it's possible to apply a macro to an entire
file to successfully modify it. I use macros practically every day
when I use Emacs.
- hippie-expand: very often, you will want to write a word that
already exists somewhere: either in the current buffer, in another
buffer, perhaps it's a filename, etc. hippie-expand is a command
that will search many sources of inputs and try to complete the
partial word that you have written. I use it so much that when I am
in another program, I feel paralyzed.
M-x align
: often when you write or program, you have multiple consecutive
lines that have the same structures, but differ in some of the
content. For example, a tabular section in LaTeX. M-x align
and
M-x align-regex
are two commands that can help you get the
different parts of your text properly aligned. Nice text is always
better to look at.- Rectangular editing: though definitely lacking compared to Vim's
visual block mode, rectangular editing is a feature that is too
often lacking in editors. How cool is it to add, delete or modify
text on multiple lines interactively?
M-x ibuffer
: ibuffer allows you to see which files are currently
opened (more generally, which buffers exist in Emacs) and manipulate
them. One manipulation you can do is mark some of them and give an
edition command to perform in all of them, usually a search and
replace. This is great when you want to modify multiple files.- evil-mode: if at the end of the day, you still find Vim to be a
superior program for manipulating text, you can install evil-mode, a
Vim emulation layer that will give you the vast majority of Vim's
commands inside Emacs. Many editors offer a Vim compatibility mode,
but it's always very superficial, and beyond insert and normal mode,
there's not much to see. Evil-mode on the other hand supports more
Vim commands than I know! You can use text objects, visual block
editing, complex ex commands (e.g. :norm works), macros, Vim's undo
commands, etc. If your favorite command is not yet there, it is
highly possible that it is being worked on right now. Several Vim
plugins are being ported to evil-mode too (e.g. nerd commenter,
matchit). If you are a Vim user, I encourage you to give it a try,
it's not a toy, it's very serious, very polished project.
Emacs: The Operating System
Going back to the joke "Emacs is a great operating system: it just
lacks a good editor", it turns out that the part about Emacs being a
great operating system is also true!
- dired-mode: dired-mode allows you to manipulate directories and
files on your file system from within Emacs. You can create
directories, copy/move/delete files, you can rename files, etc.
When I am not manipulating files with my shell, I am using
dired-mode.
- Embedded shells: Emacs has many ways to give you access to a shell:
M-x shell
for a basic shell, M-x eshell
for a shell that
integrates nicely with Emacs and Lisp, M-x ansi-term
for an actual
terminal emulator. If you have keyboard shortcuts for those
commands (I use C-x m
for M-x shell
and C-x M
for M-x
ansi-term
), you can very quickly switch between your editing task
and your shell. - Package manager: Emacs has modes, Lisp programs that give it new
capabilities. It used to be that you had to find, download and compile these
modes yourself. With the newly-added package-manager,
M-x
list-packages
, and repositories such as MELPA, installing new
capabilities for Emacs is as easier than using apt-get. - System utilities: Emacs has many utilities to interact with a Unix
system. Some that I use often are
M-x man
to view man pages, M-x
ispell
to spell-check a document and M-x grep
to perform a grep
on the file system from within Emacs and visit the results (modes
also exist for users of ack and ag). You can find many more
such utilities in Emacs's package manager. - ido-mode: ido is an Emacs mode to make switching buffers easier.
Whereas most modern editors use tabs to represent every opened
files, in Emacs you can do without such a waste of screen estate.
Instead, ido allows you to quickly switch from one buffer to another
by typing just a part of the buffer's name you want to switch to.
It supports flex-matching, and also integrates with opening and
writing files. Even with hundreds of buffer, ido makes switching
between them easy and efficient.
- Tramp: tramp allows an Emacs user to open files over network (ftp,
ssh, and many others) or to open files using sudo. Very useful if
you want to open a file on a remote machine without having to launch
a terminal and connect with ssh.
- Info: Emacs has an entire documentation built-in, info, and using
C-h i
you can find information on a great number of subjects: the
Emacs manual, the Emacs Lisp reference, documentation on the many
modes installed on your system, etc. - Themes: Emacs has a large collection of color themes to match your
mood. Some of my personal recommendations: if you want something
light, Leuven is nice; I am also a fan of phoenix-dark-pink if I
want something dark and with low contrast.
- IRC: Some people aren't really into using a tool for 17 different
tasks and prefer to use one tool per task. I mostly do too, however
IRC is different; I use it to discuss with other members of my lab,
and if the conversation was in another program, I might miss it
because I am focused on my programming task. I use Circe in Emacs,
and when someone talks on a channel, a small notification appears in
the mode-bar, and the shortcut
C-c C-SPC
allows me to quickly
switch to the channel, read what's going on, and switch back to my work.
Emacs: The Development Environment
I am a programmer, and Emacs features for development are what really
drew me in. Emacs has support for practically all known programming
languages, and very often its support is better than anything else you
can find. One great advantage of this wide support is that you can
use Emacs for all your programming needs, whatever the language may
be, and benefit from the intimate knowledge you have of your editor.
To me, it seems like a much better deal than knowing in less details
one editor per language. It's also less annoying (and less memory
hungry) if you happen to work in many different languages at once
(e.g. Python, Django templates, HTML, CSS, JavaScript).
- Modes for languages: all programming modes usually feature syntax
highlighting and auto indenting, something pretty much standard in
all programmers' editors, but it's worth mentioning that you'll find
such support for more languages in Emacs than in any other editor.
The programming modes can also offer extra functionality specific to
the language, such as switching between .ml and .mli for OCaml or
shifting a block left/right for Python.
- Compiler integration: using the
M-x compile
command allows you to
interact with a build system such as make. Emacs is able to parse
the output of make and allows you to move back-and-forth between
the different errors and warnings. Support for integrating with
ant or maven is available in the package manager. - REPL integration: many languages have a REPL, a read-eval-print loop
that allows the programmer to use the language interactively. Emacs
has great support for REPLs, allowing you to run them inside Emacs
and to send commands from your source file buffers to the REPL
process.
- Debugger support: although I'm not a big fan of the debugger support
in Emacs,
gud
is a mode that allows Emacs to become a bug-finding
station. - Tags: tags are a mechanism to index the different identifiers in a
program; Emacs uses this information to allow the programmer to find
the declaration site of a function for example. If ctags is too
basic for you, Emacs also has support for more advanced tools, such
as cscope and GNU global.
- Magit: Git took the programming world by storm a few years ago, and
today it is the de facto standard VCS tool used by millions of
programmers. Magit is Emacs's user-friendly interface to Git; not
all commands are supported, however the basic flow of git
(committing, pushing, pulling, viewing logs, branching, tagging,
etc.) are all supported. Magit is the reason I switch from
Mercurial to Git.
- fly-make: fly-make allows Emacs to run a compiler/interpreter in the
background to detect syntax and semantic errors in your program and
report them immediatly with squiggly lines. I'm not a big fan, but
if that's the kind of thing you are into, go for it!
Emacs: The Lisp Machine
Emacs is really a Lisp machine: all modes are written in Lisp and you
can configure the editor using Lisp commands. Being a Lisp Machine is
also what allows all the cool modes mentioned above: when you need
something, you can always use Emacs Lisp to code it. Although Emacs
Lisp is not the greatest language in the world, it is still a powerful
and sophisticated language that can be used to transform a problem you
have into a solution that an entire community can use.
This unbounded extensibility is the reason why GNU Emacs is still used
today, more than 30 years after its initial release. If a piece of
software cannot keep up with the times, it will eventually be replaced
by something better. By giving the users of Emacs the power to fit it
to do anything they please, the authors of Emacs have ensured that the
people most interested in keeping it up to date can do so.
Conclusion
Emacs may not be for everyone, I accept that, but I strongly suggest
that you give it a try. Not so that you can adopt it, but so that
when you go back to your preferred editor, you can bring something new
and fresh to their development.
I'll probably still be using Emacs in 2024, and if I start having
Emacs pinkie syndrome, I can always just enable evil-mode :)