A Brief Emacs Reference

Introduction

Emacs is a powerful text editor, if not a pretty one. It is commonly used when working at a terminal, although its functionality makes it attractive (especially for programmers) in any environment.

This is just a brief introduction to some of the commands of Emacs. It is meant to give you enough to use Emacs for common tasks without scaring you away. If you are interested in Emacs, I encourage you to take the time to learn more of its commands and functions.

Terminology

Emacs uses buffers to hold text which is to be edited. To edit a file, you would load the contents of a file into a buffer, edit the buffer, and then save the buffer back to disk. (Emacs remembers which file the buffer came from and saves it as the same file.) Note that a buffer doesn't have to be a file; it can be other text, such as the output of a program, or information from Emacs.

An Emacs window displays a buffer, and is identified by the status bar at the bottom which tells you information about the buffer, such as the name of the buffer (which is just the file name if the buffer came from a file), what mode Emacs is in (Emacs has different modes for editing different kinds of text, such as HTML, C++, etc.), and what line the cursor is on. When you first start Emacs, there will be only one window; however, you can create multiple windows, and some Emacs commands will create a window automatically to display results. See the Windows section below for more information.

The blank line at the bottom is called the minibuffer, and is used for entering commands and for brief status messages.

Overview

C-key Press key while holding the Ctrl key  
S-key Press key while holding the Shift key  
M-key Press key while holding the Alt (Meta) key
or press (and release) the Esc key, then press key
 
key1,key2 Press key1, then press key2  
   

Basics

C-x,C-c Exit Emacs  
M-x Enter a command by name (in Emacs, every command has a name--even the commands that also have keystrokes)  
C-g Quit current command; if you started a command you don't want to finish, or if Emacs is doing something you don't want it to  
M-` Access menu bar in terminal mode  
Tab Complete as much as possible (when entering command, file name, etc.)  
   

Files

C-x,C-f Load file into buffer  
C-x,C-v Replace buffer with new file (e.g. if you opened the wrong one)  
C-x,C-s Save buffer back to file  
C-x,C-w Write file to different location (Save As...)  
C-x,s Save all buffers associated with files  
   

Editing

C-space Begin highlighting (move cursor to select region)  
C-w Cut  
M-w Copy  
C-y Paste (yank) last thing cut or copied  
M-y Cycle back through previous things cut or copied
Only works immediately after a paste (yank)
 
C-k Kill (cut) from cursor to end of line  
C-x,h Hilite entire buffer  
   

Buffers

C-x,k Kill (close) current buffer  
C-x,b Switch to different buffer  
C-x,C-b See list of all buffers (you can pick one from the list)  
   

Windows

C-x,2 Split window (horizontally) into 2 windows  
C-x,o Move cursor to next window  
C-x,1 Join all windows back into 1  
   

Search & Replace

C-s Incremental search (starts at cursor, goes forward)
Case insensitive if you use all lowercase
Once you have started a search, pressing C-s again finds the next instance of the string you are searching for
 
C-r Incremental search (starts at cursor, goes backward)
Case insensitive if you use all lowercase
Once you have started a search, pressing C-s again finds the next instance of the string you are searching for
 
M-% Search and replace (Starts at cursor, goes forward
Case insensitive if you use all lowercase
Will ask for confirmation before each replace; press y/n
 
   

Macros

A keyboard macro is simply a sequence of keystrokes which Emacs remembers and can perform repeatedly. This is very useful in performing repetitive tasks.

C-x,( Begin recording keyboard macro  
C-x,) End recording keyboard macro  
C-x,e Execute keyboard macro  
C-u,num,
C-x,e
Execute keyboard macro num times.
(Note that the C-u,num prefix can be used to execute any command multiple times.)
 
   

Other

M-x,shell Run shell (command prompt) in an Emacs buffer. This is useful, not only because you don't have to leave Emacs to use a shell, but also because the you can perform Emacs commands on the buffer, such as searching for text, saving it to a file, etc.  
M-x,doctor In case you need a little psychotherapy after using Emacs...  
   

Help

C-h Help; in particular...  
C-h,i Info--Emacs documentation  
C-h,k Describe key--press a key (or key combination) to see documentation on the command it performs  
C-h,f Describe function--enter a function name to see documentation