GNU Emacs 101
(With an admitted bias for Windows 10 users)
EVERY PC POWER USER should have a full-featured text editor in their toolbox. There are many advanced editors to pick from, but none of them are as flexible and customizable as GNU Emacs. Some of the new programming tools from JetBrains are amazing for specific programming languages, and I’m growing to like VS Code for certain tasks, but I always fall back to GNU Emacs as my general purpose editing tool. It is old software, but still worth learning in 2017. GNU Emacs 25.3 was recently released, so if you are new to Emacs and want to give the latest version a try let me explain what you need to know and walk you through the basics.
Emacs is old-school software, but in the right hands it can be a fire-breathing beast with ninja-like powers. Watching an Emacs master edit text is like watching an experienced sushi chef create razor thin slices in a blur of high-speed motion. Like the sushi knife, Emacs is a professional-level tool and it rewards those who learn to use it properly. Learning to use Emacs takes time and practice, but the results are worth it. Emacs is old. It was in wide use before the mouse was common, it is older than current keyboard shortcuts such as
ctrl-v, and it even predates the standardized keyboard layout we use today. There is a good chance that Emacs is older than you are. Since most of the software you use every day sticks to modern conventions, using software like Emacs that does things its own way can be bewildering at first, even infuriating. But reprogramming your brain and muscle memory to avoid the mouse and learn a few dozen powerful new and unfamiliar keystrokes to rapidly navigate and modify your text is a key part of what makes editing in Emacs so blazing fast. Follow this step-by-step guide to install Emacs and begin the process of unleashing its hidden powers.
INSTALLING GNU EMACS
Start by visiting the official home of GNU Emacs. Pre-complied binaries for Windows are available here. Download the file
emacs-XX.X-x86_64.zip (where XX.X is the latest official release version number, currently 25.3). Linux users can install Emacs using their package manager, and Mac OS X users should check out Emacs For Mac OS X.
The Windows version of Emacs does not have an installation program. Instead, you install the software by unpacking a compressed file into a folder of your choice and running the software directly from that folder. I use 7-Zip to decompress the downloaded file into
C:\emacs. I recommend that you choose a folder location like
C:\emacs that does not contain spaces in the directory path. Once you have downloaded and unpacked Emacs, you can run the editor by double-clicking on
runemacs.exe in the
bin folder. Also in the bin folder is a program called
addpm.exe which will create a shortcut for Emacs in the Windows start menu.
The single most important thing you can do to get off to a good start with Emacs is to go through the built-in tutorial. Here are three ways to start the tutorial: click the words Emacs Tutorial on the default screen that comes up when you start Emacs (under the cursor in the image above); use the menu to select
Help → Emacs Tutorial; or use the keyboard to type
ctrl-h, then release
ctrl-h and type the character
t. The tutorial takes most people about an hour-and-a-half to complete. Yes, it is boring, and yes, you may want to roll your eyes when subjects like “scrolling” are introduced. But trust me here and just do it. Finish the entire tutorial and try to internalize what it is showing you. In about the same time it takes to watch the movie Ex Machina you can lay the foundation for mastering the world’s most powerful text editor.
Emacs is insanely configurable, which is one of its great strengths. It makes sense to complete the tutorial and learn the basics before you start customizing Emacs. Once you have finished the tutorial and spent some time practicing basic navigation, working with buffers, files and editing, you can start to change the way Emacs looks and behaves (check out the tricked-out Emacs image at the top of this article!).
When Emacs starts it looks for an initialization file called
.emacs (pronounced “dot Emacs”) which contains code for any customizations that you want loaded. This code is written in the Emacs Lisp programming language, and is used throughout Emacs to define both major functionality and minor configurations. So far you have not needed a
.emacs file since you have used the default settings.
Let’s set up our
.emacs file now. On Windows the best practice is to create a “home” folder for our
.emacs file and then set up a Windows environment variable called
HOME that will tell Emacs how to locate the folder. I keep my Emacs configuration in a folder called
POSIX_Home in my documents folder. Once you create the folder that will hold your configuration file, you can type
env into the Windows start menu search box. As you type you should see a shortcut appear that says “Edit the environment variables for your account”. Clicking this shortcut will open the Environment Variables dialog from the Control panel. In the User variables section click New and type
HOME into the Variable name box. Enter the full path of your folder into the Variable value box. On my system this is
C:\Users\Michael\Documents\POSIX_Home (see image below). Click OK and then OK again to save your changes.
Emacs now knows where to look for the
.emacs initialization file, but you have not yet created the file. Most experienced Emacs users create their initialization files by hand, carefully adding to them over time as they use Emacs in different ways. The built-in Emacs documentation contains tons of info about customizing Emacs in this way. You can also find many blogs and websites that provide sample
.emacs files that you can use as models when you are ready to hand code your own
.emacs file. Another option is an Emacs feature called Customize that will create the
.emacs file for you based on selections you make from a ridiculously clunky user interface built-into Emacs. Many people start using Customize for simple tweaks and then manually add their own code to the
.emacs file as they gain experience and want more control over their configuration.
CREATING .EMACS WITH CUSTOMIZE
Let’s use Customize to set up a dark color theme. You will use the mouse and menus for this, but it is possible to do everything I describe without touching the mouse or menus. Using the mouse in Emacs, select
Options → Customize Emacs → Custom Themes. Emacs shows you a list of installed themes. Use the mouse to check the box next to
misterioso and then click the gray button that says “Save Theme Settings”. Emacs will create a
.emacs file in your home folder and write code into the file to automatically start the
misterioso color theme when Emacs starts.
Now let’s use the search feature in Customize to turn off the Emacs toolbar and hide those ugly and redundant buttons. This time select
Options → Customize Emacs → All Settings Matching… and type
tool bar and Enter when prompted in the minibuffer. Out of a huge number of settings, Customize will show you only the handful that match the words “tool bar”. Use the mouse to click the arrow to the left of “Tool Bar Mode” exposing the buttons. Click the “Toggle” button and you will see the word “on” to the right of the button switch to “off”. Next, click the gray “Apply and Save” button which will write this change to your
.emacs file. After confirming
Yes to the request to save all settings, close Emacs. Restart Emacs to test your changes, noticing the color theme and the refreshing lack of ugly tool bar buttons, then take a look at the
.emacs file so you can see the code that Customize generated for you. (Customize generated the
.emacs file in the folder you created earlier before you set up the environment variable — mine was called
POSIX_Home which I put in my documents folder.)
EDITING .EMACS BY HAND
Now let’s manually enter some simple Emacs Lisp code that will accomplish the same two changes without using the Customize system. Open the
.emacs file in Emacs if it is not already open. Delete all the code that Customize wrote for you so that the
.emacs file is empty. Add these two lines to your
For variety we have chosen the light-blue theme instead of
misterioso, but you could also write
(load-theme ‘misterioso) if you like that theme better. Pay attention to the parentheses and important single apostrophe before the theme name, and the minus one (not the letter L) after
tool-bar-mode. Save this file with
ctrl-x ctrl-s and then restart Emacs to see your changes. You can see that manually customizing Emacs does not have to be hard once you know what options are available and what code is needed in the
.emacs file to bend these features to your will. You can learn a lot from example
.emacs files you find online and the detailed descriptions in the Emacs manual.
If you want to go back to the default Emacs configuration you can delete the
.emacs file or better, delete the code you entered and keep the
.emacs file handy for any future changes. Emacs will give you an error message if your
.emacs file is totally empty, so if you do delete your code you can put a comment, starting with a semicolon, into the file, such as
;My Emacs Configuration
to keep it from being empty.
GETTING MORE POWERFUL
So other than having an ancient user interface and non-standard keyboard shortcuts, why is Emacs so powerful? The short answer is that Emacs provides a well-documented platform, a programming language, and a set of features that allow developers to build specialized text editing “modes”. These modes turn Emacs into a thousand specialized editing tools, all optimized for the language, text format or unique characteristics of the files you want to edit. Emacs comes with many modes built-in, but there are a lot more you can download and install. To harness the full power of Emacs you need to install modes that will be useful to you, and make sure your
.emacs file loads them with a configuration you like. Emacs contains its own package manager to make this process easier. This said, as with everything in Emacs there are multiple ways to do it. In this case there are several package repositories to pick from and configuring the system is a multi-step process. You might want to start here or search for “gnu emacs package manager” to find an up-to-date tutorial and examples of setting up the package manager and installing a few packages.
Good luck with Emacs, and let me know if you get hooked.
MAKING EMACS WORK LIKE WINDOWS
If teaching your fingers new keyboard shortcuts for cut, copy, paste and undo is out of the question, then you should know about CUA mode. CUA stands for Common User Access and it makes Emacs behave more like Windows in a few important ways. Turning on CUA mode from the
Emacs Optionsmenu or by adding
(cua-mode t)to your
.emacsfile will give you Windows-style editing keyboard shortcuts. You can learn more about CUA mode within Emacs by typing
ctrl-h f cua-modeand then Enter.