Plug Emacs Basics

Presenter Michael Olson
Date 2006-09-15
XEmacs vs. Emacs
Basic keystrokes
Sample configuration file
How to do basic things
Find a word in many files
Find your place in a file
Perform search and replace
Change to a directory and open files
Debug a C/C++ program


Place in memory where an opened file is stored. These don't necessarily have to have a file associated with them, however. In that case, a buffer would act like a temporary file.
To make things easier for new Emacs users, we will oversimplify and use the word "file" throughout the presentation.
Emacs operates in several "modes", the most prevalent of which is Text mode. Having different modes allows Emacs to color files and change keystrokes depending on what kind of file you are editing (examples: text, source code, outline, configuration).
The sequence of keys that you have to press in order to perform an action. Emacs can have prefix keys, which means you hit the keys, release them, and then hit something else to complete an action.

XEmacs vs. Emacs

Note: Some of what I say in this section may be subject to bias, since I help to develop Emacs (to a small extent) and programs for Emacs (to a much larger extent). I'm also a GNU fan.

XEmacs is similar to Emacs, but not quite the same. It is a fork of Emacs version 19 that started around 1991 and acquired the name "XEmacs" in 1994. At that time, it was difficult for outside contributors to get their work integrated into Emacs, so they formed their own version, which allowed more people to join. When it was released, it was arguably superior to Emacs.

Important: At the time, it was called "XEmacs" because it had support for the X Window System, and Emacs 19 did not, to the best of my knowledge. This is no longer the case — Emacs 21 works both in X and in a terminal window (you have to pass the "-nw" option to make it stay in the terminal).

Opinion: Now, a better moniker for "XEmacs" is "Brand X Emacs", to indicate that XEmacs is more "generic" and does not represent the GNU "brand" as well as Emacs.

The release of Emacs 21 in 2001 incorporated most of the interface changes that XEmacs made, with a few exceptions. If we judge by the current stable releases of Emacs (version 21.4) and XEmacs (also version 21.4), it could be argued that XEmacs is slightly superior in terms of built-in features, but Emacs has slightly superior documentation.

Since 2001, there has been a great increase in the pace of development on Emacs. More people have access to the Emacs code base than ever before, and many exciting features have been added to it. For example, when Emacs 22 comes out, it will have GTK 2 support instead of the GTK 1 support that XEmacs has. Pretesting for Emacs 22 is tentatively scheduled for the end of September 2006 (the month of this presentation), and a release is expected by the end of December 2006.

We're done with the controversial part, so on to the informative part :^) .

Basic keystrokes

The way we refer to key sequences in Emacs is probably a little different than what you're used to. Here's what they look like in the Emacs world.

Familiar notation Emacs notation
Alt+x global-font-lock-mode, ENTER M-x global-font-lock-mode RET
Control+h, T C-h T
Control+h, f, what-page, ENTER C-h f what-page RET
Control+h, i C-h i

As you can see, the Emacs notation is more concise than what you might be used to.

Now we will cover some of the basic keystrokes that are needed in order to use Emacs.

Keystroke Purpose
C-x C-c Exit Emacs
C-x C-f Find and open a file
C-x C-w Save the contents to a different file ("Save As")
C-x C-s Save the current file
C-h or F1 Trigger the interactive help feature
C-h k Find out what the next keypress does
<arrow keys> Move around in the file
Page Up / Page Down Move up and down by a pageful
Home / End Move to the beginning and end of a line
C-a / C-e The same as the above
C-< / C-> Move to the beginning and end of a file
C-_ or C-/ Undo the most recent change or edit
C-x C-b Switch from one open file to another
C-x o Switch to the other file that is on the screen

Copying and pasting uses a slightly different method than what you're used to. To select a region of text, move to the beginning of the area you want to select and hit C-SPC (Control + the Space key). Release the keys. Then move to the end of the region that you would like to select. Hit M-w (Alt + w) to copy the region to the Emacs clipboard. Using C-w instead would remove ("wipe") the region and put it in the Emacs clipboard. Then move to where you want to insert the text and hit C-y to insert ("yank") the text into place.

Delete and backspace should do what you expect, as long as you use the "normal-erase-is-backspace-mode" stanza from my sample configuration file, mentioned below.

Sample configuration file

The configuration file for Emacs is usually located in .emacs. See the Plug Emacs Conf Presentation page for a sample .emacs file that has sensible (in my opinion) defaults. Pay attention to the parts of it that mention the Tab key.

How to do basic things

Find a word in many files

This technique allows you to search for a phrase in files that are in the same directory as the current file.

Type M-x grep-find RET phrase RET (where RET is the Return key) and you will see a new buffer that has clickable links. Hit RET on one of them, and you will be taken to that match.

Find your place in a file

Hit C-s and start typing. Emacs will move to the first instance of that phrase in the current file. Hitting C-s again will find the next instance of the phrase. You can hit Delete and Backspace to modify the phrase while searching.

Perform search and replace

Type M-% (Alt + Shift + "%"). Enter your search string. Hit Return. Then enter the replacement string. Hit Return.

Change to a directory and open files

Type C-x C-f to open a file as normal, but enter the path to a directory and hit Return. You will see a listing of the files in the directory. Now you can hit Return on a file to edit it.

Debug a C/C++ program

This assumes that you are familiar with GDB (the GNU Debugger). If not, this probably won't be useful to you.

Say you're working on a program, and you have one of its files open. Type M-x gdb and hit Return. Hit Return again. You will now see an open GDB session that you can type command into. You can set a breakpoint by going to that place in the file and hitting C-x SPC, where SPC is the Space key.


I will answer any questions or demo things at this time.