background image

1 - 5 

Fundamentals UNIX  2.0 - Lab 9.2.6 

Copyright 

 2002, Cisco Systems, Inc. 

Fundamentals of UNIX 

Lab 9.2.6 - Using Emacs 

(Estimated time: 30 min.) 

 
Objectives: 
Use Emacs to: 

•Access the Emacs TUTORIAL 

•Find Emacs help 

•Visit and edit files 

•Save files and edit 

 

Background: 
The instructions in this lab are written with GNU Emacs in mind, which is installed on most Linux desktop 
systems by default. XEmacs is also available in some distributions of Linux. If the student happens to 
have XEmacs on the student’s computer system, feel free to use it instead of GNU Emacs. The student 
may want  to use both and compare them. All the procedures in the lab should work identically in both 
versions of Emacs. 
 
For the purposes of this lab, the term Emacs means both versions of the editing system, unless 
specifically cited as GNU Emacs or XEmacs. 
 
Tools / Preparation: 
To perform this lab the student will need: 

•  A UNIX computer with Emacs installed. 

•  A login ID and password. 

 

 
Notes: 
 

 
 
 
 
 

 
 

background image

2 - 5 

Fundamentals UNIX  2.0 - Lab 9.2.6 

Copyright 

 2002, Cisco Systems, Inc. 

Step 1. Start Emacs. 
 
The executable command that starts GNU Emacs is called 

emacs

. The command to start XEmacs is 

xemacs

. In the exercises that follow, wherever it says 

emacs, 

shown in lower case, 

xemacs

 will also 

work, if the student has it on the system. 
 
There are several ways to start Emacs. 
 

•  Login and open a terminal window. 

•  To start Emacs, run this command in a terminal window:  emacs -q & 

 
The 

-q

 option causes Emacs to start without startup customizations. The student should do it this way for 

the lab to be sure to get the default startup. The ampersand (&) causes the command to run in 
background. 
 
Another way to start Emacs: 
 

emacs & 

 
The normal way to start Emacs: 
Emacs loads customizations unless the user tells it not to. The potential for customization in Emacs is 
vast. Virtually no two users modify it the same way. 
 

emacs -nw 

 
The 

-nw

 means no windows, this means to start Emacs running in a terminal window rather than with its 

graphical interface. The student may wish to try starting that way and compare the operation. A few users 
prefer the character interface and use it exclusively. 
 
If the student is running GNOME, Emacs is available as a menu item. Press the GNOME foot, then 
Programs, Applications, and Emacs. 
 
Read the splash screen. The student will see this if the student has no initialization file or if the student 
started with the 

-q 

option. Emacs assumes the user may be new and in need of help. 

 

a.  What information does the splash screen show?  

 

b.  If the student also tried starting with 

-nw

, was the splash screen the same?  

 

 
 

NOTE: If the student waits long enough, the splash screen disappears, but the buffer named 
*scratch*, including the asterisks, remains. 

 
 
Step 2. Bring up the Emacs TUTORIAL. 
 
Graphical Emacs may be run using the menus and mouse in much the same way as any other GUI 
editor. The purpose of this lab is to familiarize the student with the Emacs way of doing things, which, 
once learned, is much faster. We suggest the student save exploration of the menus for another time. 
 
To start the TUTORIAL, execute the key combination Control+h t. 

background image

3 - 5 

Fundamentals UNIX  2.0 - Lab 9.2.6 

Copyright 

 2002, Cisco Systems, Inc. 

 

a.  What is the first matter the TUTORIAL explains following the copyright? 

 

 

TIP: Pressing the Alt key on most standard PC keyboards provides the META key function.  

 
From this point on this lab will use standard Emacs command notation when telling the student what to 
type next. To restart the TUTORIAL at any time, type 

C-h t

 
NOTE: Two details the student should know before proceeding: 
 

•  In Emacs any key combination that runs a single Emacs command is called a key, always singular, 

even though multiple keystrokes are involved. 

•  Often, in referring to interactive Emacs commands by name, ones that can be executed from a 

keyboard, the name is shown with the 

M-x

 prefix for clarity. This happens because 

M-x command-

name

 is how any interactive command may be executed when not using its key binding. 

 
Step 3. Work through the first part of the TUTORIAL. 
 
There is no better way to learn about Emacs than to start with the Emacs TUTORIAL, which has been 
standardized for many years. It is designed for the user to read and do exactly as it says as the user 
progresses, using the TUTORIAL text itself as a scratch file to play with. By the time the student gets to 
the end the student will know all the essentials of Emacs, enough to be comfortable using it to accomplish 
real work. 
 
For now, go through the first four sections of the TUTORIAL. Begin with the section at the beginning on 
notation, followed by the sections titled: 
 

•  SUMMARY 

•  BASIC CURSOR CONTROL 

•  WHEN EMACS IS HUNG 

 

NOTE: The XEmacs TUTORIAL inserts a section “Cursor Control with an X Terminal” before “When 
XEmacs is hung”. . 

 
This TUTORIAL should take the student about 15 minutes. 
 

a.  What key allows the student to back out of a command when Emacs seems to be tangled up?  

 

 
Step 4. Learn to get help. 
 
Help is available at every keystroke in Emacs. The amount of information available is vast, because 
Emacs was designed to be self-documenting. The student should never need to buy a book learn about. 
 

•  To see all general categories of help available, type 

C-h ?

. If the student is using XEmacs, type 

C-h 

? ?

 

a.  How would the student produce a list of all key bindings? 

____________ 

b.  What key is bound to the command 

M-x isearch-forward

? ____________ 

c.  What key is bound to 

M-x kill-line

____________ 

background image

4 - 5 

Fundamentals UNIX  2.0 - Lab 9.2.6 

Copyright 

 2002, Cisco Systems, Inc. 

d.  How would the student find out what command the key 

C-x C-f

 runs? What command is it?  

 

e.  How would the student find information about commands whose names include the string ’buffer‘? 

Are there any such commands?  

 

 

f.  How would the student find documentation about the command 

M-x find-file

? Hint: 

commands and functions are synonymous in Emacs. What does it do?  

 

 
Step 4. Edit files. 
 

•  Type 

C-x C-f

 and respond 

junkfile

 to the prompt. Note: Always press Enter following prompts. 

Look at the modeline, this is the black band in reverse colors one line above the bottom. 

 

a.  What happened?  

 

b.  Did the file 

junkfile

 have to exist before the student visited it? ____________ 

c.  Watch the modeline and type one letter. What happens in the modeline? 

 

 
Type in a few words or characters and move the cursor around using Emacs commands learned in the 
TUTORIAL. Note: Do not use the mouse or menus. 
 

d.  What is the primary difference between vi and Emacs in going from typing text to moving the 

cursor or executing commands? 

 

 

e. Type 

C-x C-f

 and respond /etc/passwd to the prompt. Did Emacs warn the student that the 

junkfile

 has not been saved junkfile before going to another file? 

 

f.  How is the modeline different from what the student saw before? 

 

 

g.  Try to type some characters. Could this be done? 

 

 

h.  What does the key C-x k do? Try using it. 

 

 

•  Type 

C-h k C-x k

 to see complete documentation on 

M-x kill-buffer. 

•  Type 

C-x 1

 to return to a single window. 

 
Step 5. Learn to kill file buffers without saving, and to save files. 
 

•  First type 

C-x C-f /etc/passwd

 to visit the password file again. 

background image

5 - 5 

Fundamentals UNIX  2.0 - Lab 9.2.6 

Copyright 

 2002, Cisco Systems, Inc. 

•  Open a second new file for edit. Type 

C-x C-f ~/xyz

 The tilde (~) character is a shortcut for the 

student’s home directory. Type a few letters in the new file's buffer. 

•  Type 

C-x C-b

 (

M-x list-buffers

) to see a list of open buffers in another window. 

 

a.  Are there the same number of buffers as open files? 

 

b.  Notice the column MR. What does this column indicate about the state of the file buffers for 

password, xyz, and 

junkfile

? Type 

C-x 1

 to return to a single window. 

 
 

c. Type 

C-x k

 (M 

-x kill-buffer

) to attempt to delete or kill the current file buffer. Enter to 

accept the default. What happens? 

 
 

d.  Type y and press Enter. Did Emacs let the student kill it? 

 

 

e.  Type y again, and this time type yes. Did it work?  

 

f.  The student should now be back to the password file. Kill the buffer to return to 

junkfile

. Watch 

the modeline and type 

C-x C-s

 (

M-x save-buffer

). How does the appearance of the 

modeline change? 

 
 

g.  What message appears in the status line or the bottom line? 

 
 

 
Step 6. Exit Emacs. 
 

a.  First, type a few more characters into 

junkfile

 to return its status to modified. Type 

C-h k C-x 

C-c

 to learn about  

M-x save-buffers-kill-ema.  

How does this function allow quitting 

Emacs easily?  

 
 

 

b. Type 

C-x C-c to begin exiting from Emacs. Did this prompt the student regarding any unsaved 

buffers? 

 
 

 

c.  Restart Emacs with the command:  emacs -q junkfile  Notice that Emacs may be started 

with one or more file names as arguments. 

 

d. Type 

C-x C-c. 

Did Emacs prompt for verification?