Home > Online Help > Unix > Basic Editing with Emacs

Basic Editing with EMACS


This is just a very small part of all key bindings used by GNU Emacs. The set of commands listed below is believed to cover everything needed in common editing.

 In key combinations we use the following notation:

 C- means Ctrl-

 M- means Meta- or Alt- or ComposeChar- (depending on which keyboard/OS you're on)

If your keyboard doesn't have a working Meta, Alt, or ComposeCharacter key, you can use the Escape key. If your keyboard doesn't have an Escape key either (e.g. you are using a DEC keyboard), try the F11 key. When using Escape or F11, type Escape THEN the key. (i.e. don't press both keys at the same time, just type Escape then whatever the letter/number is)

 Thus, for instance, C-x means to press Ctrl and x at the same time.

Leaving Emacs
exit Emacs
          C-x C-c

Files
read a file into Emacs
          C-x C-f

save a file back to disk
          C-x C-s

write buffer to a file
          C-x C-w

Buffers
select another buffer
          C-x b

list all buffers
          C-x C-b

kill a buffer
          C-x k

Marking Regions
set mark here
          C-@ or C-SPC

exchange point and mark
          C-x C-x

 

Killing and Deleting
kill line (to end of)
          C-k

kill region
          C-w

copy region to kill ring
          M-w

yank back last thing killed
          C-y

Incremental Search
search forward
          C-s

search backward
          C-r

Multiple Windows
delete all other windows
          C-x 1

delete this window
          C-x 0

split window in two
          C-x 2

switch to another window
          C-x o

Query Replace
replace a text string
          M-%

Directory Editing
run Dired on a directory
          C-x d

Motion
go to buffer beginning
          M-<

go to buffer end
          M->

Formatting
fill paragraph
          M-q

set fill column
          C-x f

Case Change
uppercase word
          M-u

lowercase word
          M-l

capitalize word
          M-c

Info
get detailed info
          C-h i

Transposing
transpose characters
          C-t

transpose words
          M-t

transpose lines
          C-x C-t

Shells
execute a shell command
          M-!

start a shell in window
          M-x shell

Error Recovery
abort command
          C-g

undo an unwanted change
          C-x u or C-_

redraw garbaged screen
          C-l

Getting Help
describe the current mode
          C-h m

 



[Organization of the Screen] [C and Fortran Formatting] [File Handling] [Using Multiple Buffers] [Multiple Windows]
[The Mark and the Region] [Notes] [More Info]

Basic Philosophy

The Organization of the Screen

On a text-only terminal, the Emacs display occupies the whole screen. On the X Window System, Emacs creates its own X windows to use. We use the term frame to mean an entire text-only screen or an entire X window used by Emacs. Emacs uses both kinds of frames in the same way to display your editing. Emacs normally starts out with just one frame, but under X you can create additional frames if you wish.

 When you start Emacs, the entire frame except for the last line is devoted to the text you are editing. This area is called window. The last line is a special echo area or minibuffer window where prompts appear and where you can enter responses. You can subdivide the large text window horizontally or vertically into multiple text windows, each of which can be used for a different file. Below the word window always refers to the subdivisions of a frame within Emacs.

 The window that the cursor is in is the selected window, in which editing takes place. Most Emacs commands implicitly apply to the text in the selected window. The other windows display text for reference only, unless/until you select them.

 Each window's last line is a mode line which describes what is going on in that window. It is in inverse video if the terminal supports that, and contains text that starts like -----Emacs: SOMETHING. Its purpose is to indicate what buffer is being displayed above it in the window; what major and minor modes are in use; and whether the buffer contains unsaved changes.

C and Fortran Formatting

Emacs will automatically format C and Fortran code for you as you type. To use Emacs' C indentation and formatting mode, the command is "M-x c-mode" and for Fortran "M-x fortran-mode".

File Handling

The basic unit of stored data in Unix is the file. To edit a file, you must tell Emacs to examine the file and prepare a buffer containing a copy of the file's text. This is called visiting the file. Editing commands apply directly to text in the buffer; that is, to the copy inside Emacs. Your changes appear in the file itself only when you save the buffer back into the file.

In addition to visiting and saving files, Emacs can delete, copy, rename, and append to files, keep multiple versions of them, and operate on file directories.

Using Multiple Buffers

The text you are editing in Emacs resides in an object called a buffer. Each time you visit a file, a buffer is created to hold the file's text. Each time you invoke Dired, a buffer is created to hold the directory listing. If you send a message with C-x m, a buffer named *mail* is used to hold the text of the message. When you ask for a command's documentation, that appears in a buffer called *Help*.

At any time, one and only one buffer is selected. It is also called the current buffer. Often we say that a command operates on the buffer as if there were only one; but really this means that the command operates on the selected buffer (most commands do).

When Emacs has multiple windows, each window has a chosen buffer which is displayed there, but at any time only one of the windows is selected and its chosen buffer is the selected buffer. Each window's mode line displays the name of the buffer that the window is displaying.

Each buffer has a name, which can be of any length, and you can select any buffer by giving its name. Most buffers are made by visiting files, and their names are derived from the files' names. A newly started Emacs has a buffer named *scratch* which can be used for evaluating Lisp expressions in Emacs. Each buffer records individually what file it is visiting, whether it is modified, and what major mode and minor modes are in effect in it.

Multiple Windows

Emacs can split a frame into two or many windows. Multiple windows can display parts of different buffers, or different parts of one buffer. Multiple frames always imply multiple windows, because each window belongs to one and only one frame.

The Mark and the Region

There are many Emacs commands which operate on an arbitrary contiguous part of the current buffer. To specify the text for such a command to operate on, you set the mark at one end of it, and move point to the other end. The text between point and the mark is called the region . You can move point or the mark to adjust the boundaries of the region. It doesn't matter which one is set first chronologically, or which one comes earlier in the text.

Once the mark has been set, it remains where you put it until it is set again at another place. The mark remains fixed with respect to the preceding character if text is inserted or deleted in the buffer. Each Emacs buffer has its own mark, so that when you return to a buffer that had been selected previously, it has the same mark it had before.

Notes

Emacs is largely a self-documenting editor. M-x info will often provide the answers you are looking for. Also, M-x apropos will return a list of commands which contain a certain word in them, so for instance "M-x apropos line" would print all of the commands which have "line" in their names, "forward-line" for instance. Finally, the M-x keystroke combination is quite common, and on some DEC keyboards can be sent by typing the "Do" key in the upper right.

 


More Info

For more information, try the GNU Emacs On-line Manual at http://www.cis.ohio-state.edu/htbin/info/info/emacs.

 This page was compiled from GNU Emacs Reference Card and Info files by Ruda Kulhavý, and adapted by David Bushong.