Emacs Tutorial

In some of your early labs, you may have been introduced to the emacs editor.  In Linux/Sun labs, you probably even had the graphical version, which allows you to point and click, highlight, and use menus.  However, if you log in through a terminal (such as PuTTY), you don't have access to these features--you can't use the mouse at all!  If you want to learn how to use emacs through a terminal--or just want to learn to edit without switching back and forth between the mouse and keys, then this is a great place to start!  The later half of this document serves as a quick reference for common commands, so bookmark or print this so you can keep it handy!

Emacs jargon

The first key to understanding emacs is learning the notation.  Commands are usually listed in cryptic emacs slang--but it really isn't that hard to figure out:

C-x means to hold control, and hit x, for any character x (examples: C-x, C-u, C-a). 

M-x means to hold the "meta" button before pressing x.  This is often a point of confusion for newcomers to emacs.  Most computers do not have a "meta" key labeled as such.  On most standard desktop computers, this means the "alt" key.  In some labs, you may find a meta key, labeled with a small black diamond.

C-M-x means to hold control AND meta, and then hit the character.

C-x y means to hold control and press x.  Then, release control and press y.  Similarly for M-x or C-M-x: if there is a space between the command letters, you should release any of the special keys.

Finally, some commands do not have shortcuts, and are called by name.  For these, you type M-x followed by the command name, then hit return.  Example: M-x goto-line [line number] will jump to a specific line in the file.

Now that you know the syntax, you can get started with emacs!

Navigation

Navigating through an emacs file (called a "buffer") is usually easy to do.  Most terminals allow you to use the arrow keys to maneuver through the lines.  Page up and page down will typically work too, although the home and end keys work less frequently.  For more detailed information on navigating within emacs, try reading the emacs tutorial--just type C-h t (recall, that means control-h, release control, and hit t).  This will open the emacs tutorial on top of whatever files you had open, and it can walk you through some simple commands.

Some of the more useful commands are listed here:

C-l     (that's a lower-case L, not a number 1) This command centers the screen on the current line, so you can get exactly the text you want to see.
M-<     Jump to the beginning of the entire file
M->     Jump to the end of the entire file
M-x goto-line [number]  Jump to any line number in the file
C-M-f   When the cursor is on an opening brace or parenthesis, use this to find the matching closing brace/paren
C-M-b   When the cursor is on a closing brace or parenthesis, use this to find the matching opening brace/paren.
        Mnemonic: f for forward, b for backward.

Undo

Before you go much further, it's important to learn how to undo what you've done!  If you accidentally hold down control instead of shift, you could start typing some disastrous commands with no idea of what you've done.

C-g     Abort current command.  Use this if your cursor is trapped in the "mini-buffer" - the single line at the bottom of the screen where extended commands appear.  Also use it to cancel any C-x or M-x commands, or to cancel a search that is taking too long, etc.
C-_     Undo the last command.  You can type this repeatedly to continue undoing your last actions.

Files and buffers

It's always important to learn how to open or save new files!  Here are some commands to help you with that.

C-x C-c         Close emacs.  You will be asked if you want to save each modified buffer (hit y/n, or C-g to cancel the quit)
C-x C-s         Save the active buffer
C-x C-w         Save as...
C-x C-f         Open (or create) a file.  You will be prompted for the file and directory (you can use tab completion here).  If the file does not exist, it will be created.  Although this new file will occupy the screen, your other files are still open.
C-x b            Switch buffers to another file you have opened already.  You will be prompted for the name of the file (you can use tab completion) or you can scroll through available buffers with the arrow keys.

Files and buffers (advanced)

You can also split the emacs window into several frames.  These frames can view different locations in a single file, or separate files.  For example, you can browse a C header file to see function prototypes, and edit the source file at the same time.

C-x 0   Close the active frame.  (That's a zero, not an O)
C-x 1   Close all frames except the active one. (Mnemonic: show 1 frame)
C-x 2   Split the current frame horizontally into two frames.  Both will display the same buffer by default.
C-x 3   Split the current frame vertically into two frames.  Bothwill display the same buffer by default.
C-x o   Switch to another frame. (Mnemonic: o for "other frame").  You can switch to the frame and then use C-x b or C-x C-f to change what buffer is displayed in the frame.
C-x C-b Splits the current frame and displays a list of open buffers in the inactive frame.  You can switch to this frame and then use the arrow keys to select a buffer.  Hit enter, and that buffer will be loaded into the frame.
C-x d   Prompts you for a directory (default: current directory) and when you hit enter, displays all files in that directory.  You can browse these files with the arrow keys, and hit enter to select a file to be opened.

Cut, paste, and copy

In emacs, cutting is referred to as "killing" text, and pasting is called "yanking".  The following commands will help you to kill and yank text in your documents:

C-k     Kill a line of text (except the newline).  If it is a blank line, the newline is removed.  If you hit C-k repeatedly to remove several lines, they will be grouped together and will "yank" as a single large piece of text.
C-y     Yank the most recently killed text
C-space Set the "mark" to the cursor's current location (see below commands)
C-w     Kill all text between the cursor's current location and the last mark that was set
M-w     Copy all text between the cursor's current location and the last mark that was set.  This allows you to "yank" that text without deleting it from its original location.
C-x C-x Swap the cursor's current location and the mark.  This is very useful for returning to previous edit locations.
M-y     Use this immediately after yanking text to yank a different group of text.  For example, if you kill paragraph 1, then paragraph 2, then paragraph 3, C-y will yank paragraph 3 (the most recent kill).  If you then type M-y, it will replace the yanked paragraph 3 with paragraph 2.  M-y again will replace this with paragraph 1.  Further M-y commands will cycle through paragraphs 3, 2, and 1 (and any other kills you have made).


Search and replace

It's always useful to search through your documents for certain phrases.  Here's how to do it in emacs:

C-s     Search forward.
C-r     Search backwards
        For either of these, you will first need to type the pattern you are searching for.  After you have entered the pattern, further C-s or C-r commands will find the next or the previous instance of this pattern, respectively.  If it reaches the end (or the beginning, for a reverse search) of the document and cannot find another instance of the pattern, it will notify you that the search failed.  You can then hit C-s (or C-r, for reverse searches) again to start the search again from the top (bottom) of the document.  At any point in the search, you can hit C-g to cancel and return to the point where you started the search.  If you find the spot you were looking for, you can hit enter to end the search.  When you end the search with enter, the mark is saved from the point where you began the search, so you can return there with C-x C-x.

M-%     Search and replace.  You will be prompted for a search pattern, then a replace pattern.  Emacs will search through the document, prompting you at every match.  For each match, type y to confirm a replacement, n to skip a replacement but continue searching, q or enter to quit, or ! to replace all with no further prompting.