Name: _____________________ Class: CET 421
SSN/ID:   _____________________ Section & Group: ____________
Emacs


What is Emacs???

Emacs is a text editor — a computer application which allows users to create, modify, and save text files on a computer's file system. Emacs is a self-documenting, customizable, extensible, real-time display editor.

Emacs is classified as a display editor because it displays the contents of a file as its typed rather than requiring a special command to view the contents. It is real-time because the screen updates as each character is typed. Changes can be seen immediately as they are input. We call Emacs self-documenting since its complete documentation is built-in and is available at any time with just a keystroke (C-h). Emacs is customizable and extensible because commands can be re-mapped to different keystrokes, and new commands can be created and added to its basic functionality.

Because the source to GNU Emacs is freely-distributed, its flexibility is further enhanced. Emacs is found on a variety of operating systems and architectures, most notably on UNIX and Windoze machines. The particular variety of Emacs we will be discussing in this document is GNU Emacs (version 19), a widely-installed distribution of Emacs from the Free Software Foundation.

How Emacs Interacts with Files

Emacs, as with editors in general, does not directly access a file's contents. Instead, Emacs copies the contents of the file into a buffer. A buffer is a temporary work area where Emacs allows you to edit a text file. The real file is left untouched as a backup until Emacs is given the explicit command to save the buffer, at which time the changes are saved permanently. Emacs also supports the "version" capability of the operating system on which it resides. For instance, on UNIX, the previous edition of a file exists as the filename appended with a tilde (~).

Before getting started...

The Emacs Display

The Emacs display consists of several parts: the text window, mini-buffer (also called the echo area), mode line, menu bar (X version only) and the frame. The Emacs image on your particular screen might not look exactly like the diagram below because of your particular environment. The text window is the area which displays the current section of the open buffer. The last line of the text window is called the mode line and is displayed in reverse video.

Directly underneath the mode line, on the last line of the entire Emacs display, is the mini-buffer. When the first keystroke of a key sequence that requires a second keystroke has been entered, the cursor moves to the mini-buffer to prompt for the rest of the command. To cancel a command in progress, type C-g.

The entire Emacs display is contained within a frame.

The Emacs Display

Getting Started: Creating and saving a file

  1. Start up Emacs with a new file called test by entering emacs test from the UNIX prompt and pressing the RETURN KEY.
    When you are using Emacs in an X environment, it may be more convienent to put the Emacs session into the background by appending an ampersand (&) when starting Emacs. For example, emacs test & invokes an Emacs frame and allows you to continue entering additional commands at the UNIX prompt, such as g++ source.cpp -o run_me.exe.
  2. An empty Emacs buffer will appear on your screen. Type a sentence into the text buffer and save it with the command C-x C-s.
  3. Enter C-x C-c to exit Emacs.
Emacs: Moving Around

The cursor, sometimes called a point, is the point of insertion and the point of deletion. In other words, when characters are typed they appear at the location of the cursor and when the backspace key is typed, characters will disappear from the location immediately left of the cursor.

To insert and delete characters, it is necessary to move the cursor to the desired location in the text buffer. The following lab utilizes the most commonly used commands for changing the location of the cursor.

Edit the file sample.txt by entering emacs sample.txt & at the UNIX prompt and press the RETURN KEY. Note that this will only work if you have already copied the file sample.txt into your directory, as per above.

Practice moving forward and backward, from one line to the next, and to the beginning and end of the buffer. Use the table of commands below:

         Description              Lisp Function Name

    C-f  next character           forward-char
    C-b  previous character       backward-char
    C-n  next line                next-line
    C-p  previous line            previous-line
    C-e  end of line              end-of-line
    C-a  beginning of line        beginning-of-line
    M->  end of buffer            end-of-buffer
    M-<  beginning of buffer      beginning-of-buffer
    C-v  forward one screen       scroll-up
    M-v  back one screen          scroll-down
Emacs: Killing Text!

This lab shows how to kill (delete) small amounts of text near the cursor as well as larger amounts of text, such as words, lines, and paragraphs, by defining regions. Regions are defined by setting invisible marks in the buffer. All killed text is placed into a buffer called the kill ring. The kill ring is a record of all your previous kills.

Text which has been killed is saved in the kill ring as a block. A block might consist of a single character, or any sized region. Note that subsequent kills with no intervening insert commands will be saved as one block. For instance, multiple C-k (kill-line) commands will save all of the killed text as one single block in the kill ring. The kill ring can be accessed as a method to bring killed text back into your buffer (see "Moving Text & Undoing Changes").

For a summary of kill commands see the Lab Command Summary. Now try the lab, which involves killing text:





Lab Command Summary

This is a summary of commands used in the labs for this handout as well as some other helpful commands which were not demonstrated in the labs.

Working with Files
read a file ......................................... C-x C-f
save a file ......................................... C-x C-s
save all files ........................................ C-x s
insert file into buffer ............................... C-x i

Getting Help
display quick list commands ........................... C-x p
apropos, show commands matching string ................ C-h a
show the function bound to a key ...................... C-h c
describe a function ................................... C-h f
get mode-specific information ......................... C-h m

Incremental Search
search forward .......................................... C-s
search backwards ........................................ C-r
exit search ...................................... return key
abort current search .................................... C-g

Query Replace
interactively replace a string .......................... M-%
Mini-buffer choices are:
replace this occurance, go on to the next one ..... space bar
replace this occurance, donŐt move ........................ ,
skip to the next occurance without replacing ..... delete key
replace all remaining occurances .......................... !
exit query replace ............................... escape key

Killing Text
kill one character backward/forward .......... delete key/C-d
kill one word backward/forward ............. M-delete key/M-d
kill text to end of line/beginning of line ...... M-O C-k/C-k
kill sentence backward/forward ........... C-x delete key/M-k
kill marked region ...................................... C-w
copy region to the kill ring ............................ M-w
yank last thing killed .................................. C-y

Marking Regions
set a mark ...................................... C-space bar
mark entire buffer .................................... C-x h
mark page ........................................... C-x C-p
mark paragraph .......................................... M-h
exchange point and mark ............................. C-x C-x

Moving Around
move one character backward/forward ................. C-b/C-f
move one word backward/forward ...................... M-b/M-f
move one line backward/forward ...................... C-p/C-n
move to beginning/end of line ....................... C-a/C-e
move to beginning/end of sentence ................... M-a/M-e
move to beginning/end of paragraph .................. M-{/M-}
move to beginning/end of buffer ..................... M-</M->
scroll to previous/next screen ...................... M-v/C-v

Buffers
select another buffer ................................. C-x b
list all buffers .................................... C-x C-b
kill a buffer ......................................... C-x k

Working in the Mini-Buffer
complete the command as much as possible ............ tab key
complete and execute the command ................. return key
show possible completions ................................. ?
abort command ........................................... C-g


Ricky J. Sethi <rickys at sethi.org>
Last modified: Mon Mar 21 02:05:16 PST 2005