GNU Emacs 101 for Beginners

2692

No matter how slick Linux on the desktop gets, there will probably always be a gulf between new users and veterans for whom the Linux environment has become second nature. Root accounts, the filesystem hierarchy, the dizzying array of distributions — they seem strange at first, but eventually new users learn that are nothing to be feared. So too with the “programmer’s editors” Emacs and Vi/Vim: the first encounter can be intimidating because of the unfamiliarity of either editor. As an Emacs user, I’m interested in providing some advocacy, so if you’ve been wondering just what the point of an editor like Emacs is, or if you’re not sure how to get started, read on for a gentle introduction.

Why Emacs?

Naturally, you can choose any of several dozen applications to edit your files with, from lightweight utilities like Nano to full GUI systems with plug-ins. So why do people like Emacs? Two reasons.

First, at the core of Emacs’ appeal is the fact that it is easily customized. Technically speaking, Emacs itself is an interpreter for the Lisp programming language, and it simply starts up running a text-editing environment. But you can change everything about it, adding and removing functions, modifying features, and tying in to other programs. Although most people do not study Lisp, it is very easy to learn, and there are plenty of simple modifications you can make to bend Emacs to your will in mere minutes. That’s not true of most editors.

That said, you don’t have to change anything about it in order to get started. Which leads to reason number two: the developer community around Emacs is huge, and as a result there is a vast ecosystem of tweaks and extensions that you can find and install without any effort. Sure, you could write your own re-programmable editor from scratch, but building up a vibrant community of users and contributors would take forever.

In short, you might think of Emacs as the Firefox of editors. There are other options, and lots of them are good. Some might even offer more speed or a smaller memory footprint. But the advantage Emacs offers is its customizability; with add-ons you can tailor the program to fit exactly how you want to work.

The Basics

A good chunk of Emacs’ intimidation factor stems from the conventional wisdom that the interface is totally unlike other Linux applications, so you have to learn a lot just to get started. It is true that Emacs has been around longer than all GUI desktop environments, and as a result has a legacy keybindings (and names for things) which differ from the standards we’ve picked up from the Windows and Mac worlds.

But you don’t need to care about them, because on today’s desktop systems, you can run Emacs as a full-blown GTK+ GUI editor, complete with toolbar, menu bars, scroll-wheel support, and all of the other conveniences. Thus, my recommendation on all the Emacs cheat-sheets and references is simple: don’t bother. If you just want to get started, fire up Emacs and use the menus — you can look into keybindings later.

EmacsIf that worries you … okay, just memorize this one command, and you’ll have no trouble: Ctrl-]. That’s holding down the Control key and hitting the right-bracket key. What this command does is stop whatever command sequence or function you’re currently in, and return you to plain-ole editing. No matter what trouble you think you’ve gotten into (such as a mistyped keystroke that starts something unfamiliar), Ctrl-] will end it and set you back down on your feet, pronto.

Emacs is a standard application these days, so you can install it from your distribution’s package manager. You may even find several versions available, because people like to fork it and maintain specialized builds. If in doubt, choose the vanilla Emacs package. The most recent release is 23.3, from May 2011.

When you fire up Emacs for the first time, you will see some startup messages flash across the new window, but in a moment it will settle down and be ready to use. Across the top are the menus and toolbar, down the side is the scrollbar, and at the bottom are two special lines akin to a “status area.” The next-to-bottom line is mostly dashes, and is there to separate the editing portion of the screen from the line below. But this line will also tell you the line number the cursor is on, the name of the currently-open file, and highlight a pair of asterisks if the file has been modified. The very bottom line is called the “minibuffer” in Emacs-speak. It shows messages and alerts, and whenever you type a control key-sequence, the keys you press appear here so you can see that you typed correctly.

By default, whenever you launch Emacs it opens a temporary file called “scratch” in the main portion of the window. Emacs refers to the files it has open as “buffers” (which reflects the fact that they don’t get written to file on disk until you say so). The scratch buffer has a short paragraph at the top explaining that nothing you type in scratch gets saved. That paragraph starts with semicolons, because semicolons are the comment-delimiter in Lisp. See, you can actually type Lisp code into scratch and have it executed. But you don’t have to.

At this point, you can open a file from the file menu, edit it, cut and paste, and save it all from the menus. As you do so, you’ll pick up on some Emacs-isms. For example, Emacs rarely if ever uses pop-up dialogs. If it needs to ask you something (such as what word you want to search for in a text search), it does so in the minibuffer — or, worst-case-scenario, it will expand the minibuffer up a few lines, for instance to let you choose a file from a directory listing. This is because Emacs predates modern Unix windowing systems, and to this day its authors make sure it still functions correctly in a windowless environment.

You can also see Emacs’ keybindings next to their respective commands in the menus. This is a good way to get used to the keybindings. Another Emacs-ism is the fact that way more commands have keybindings, because Emacs lets you bind two-letter commands. For example, Ctrl-X Ctrl-F opens a new file. That seems unusual at first, until you notice that all of the file-management keybindings reside in the same “block”: Ctrl-X Ctrl-S saves a file. Besides , the other important key is Meta. It’s an anachronistic name — on almost all keyboards you use Alt for this key, but there are still some exceptions, so the name sticks. Ctrl and Meta are displayed in the minibuffer and menus as C- and M- respectively.

Modes

Now let’s talk about that (Lisp Interaction) label at the bottom of the screen in the scratch buffer. It tells you that you are working in Lisp mode. If you open or create a plain text file, the mode will change to (Text).

Emacs modes are a big part of why programmers like the editor. There are modes for every conceivable programming language, and they assist you with writing code in several ways. They can highlight syntax, auto-indent, complete commands, match parentheses, and allow you to jump through large portions of your code at once (such as jumping directly to a function definition, if you have run across a function call and need to refresh your memory).

In addition to programming languages, there are modes to simplify work with particular file formats (such as TeX) and utilities (such nroff), and “minor modes” that change smaller aspects of their parent mode. An example of this would be Text-Based Table mode, which lets you navigate around a WYSIWYG-style table in plain text, far faster than manually lining up your columns and rows.

There are also Emacs editing modes that have nothing to do with programming, such as Picture mode for “ASCII art” and outline mode, which lets you collapse and expand nested outlines. In both of these cases, the special mode simply changes the way the cursor moves around the document, rather than changing the contents of the file in unusual ways. But these add-ons are not just cosmetic; you can also do serious work with them, such as encrypt and decrypt files, or compile and build code.

Even further out into left field are pure applications that are written as Emacs modes. There are modes for reading email, checking social networks, browsing the web, and playing games. In reality, these are Lisp programs, but by tying in to the Emacs framework, you can switch over to one of them and use less resources than you would firing up a separate program.

Your first power tricks, your first customizations

Modes are well and good, but you didn’t come here to edit source code. To whet your appetite, let’s look at a few of Emacs’s interesting tricks.

Emacs has several powerful search-and-replace options, including full support for regular expressions. But perhaps the most intriguing to new users in incremental search. This search command lets you start typing your search term, and jumps immediately to the next match with every additional character you type. If you make it your default search method, you type fewer characters over all.

The recursive edit command, which you trigger with Ctrl-r lets you pause whatever action you are currently doing and resume editing (or reading) a file, or even executing another command. You can resume the suspended action with Ctrl-Meta-c. This can be especially helpful when you are in the middle of a long process, such as spell-checking a document.

When it comes to customizing the editor, a good place to start is with a ColorTheme. I use a dark GTK+ theme on my desktop, and don’t care for the high-contrast black-on-white text in Emacs’ default look. So I use a custom color scheme instead. The ColorTheme package is an add-on written in Lisp, which you can install through your package manager.

To activate it, you will need to follow the instructions on the ColorTheme project page, which tell you how to save the right commands into your personal customizations file, which is stored at ~/.emacs. This file contains any Lisp commands you want Emacs to execute whenever you start it up, and it can load settings, launch add-ons, or even define new functions that you write yourself.

Once you get comfortable editing .emacs, the odds are you will periodically find a new command you want to save for everyday use. For example, I have (fset 'yes-or-no-p 'y-or-n-p) in my .emacs file, a setting that turns off Emacs’ demand that you type out the full words “yes” or “no” when it asks you a yes/no question. I don’t save a lot of time by typing “y” and “n” instead, but i save a lot of aggravation.

More fun is defining new functions altogether. Here a good place to start is the Emacs Wiki, with a useful feature like word count. You will get a feel for Lisp programming, and add a helpful new feature at the same time.

The Emacs Wiki is a good place to start looking whenever the “I wonder if there’s a way to … ” questions start popping up. It is filled to the brim with tips and code snippets you can use immediately, and links to far more that are maintained elsewhere. Plus, it is actively maintained, so there is no real danger of finding content out-of-date enough to do any harm. Browse around, and you will get an idea of what other people are doing with Emacs, as well as why they like it. You might even come up with some ideas of your own.