Anda di halaman 1dari 6

Page 1 of 6

EE1030
Introduction to UNIX
Salt Lake Community College
Department of Engineering
(Electrical Engineering) MHM

ASSIGNMENT #4 emacs (a text editor in UNIX)

EDITING A FILE: emacs

emacs is a popular editor that is often found on many UNIX systems (and for those for which it
is not included in the distribution, a version is probably available via download on the Internet).
emacs had its start in the Lisp-based artificial-intelligence community. In 1975, Richard Stallman
and Guy Steele wrote the original version, which has evolved into the version that is now
distributed for free and in source-code form through the Free Software Foundation (FSF).
Stallman formed the FSF because of his objection to copyright and patent laws and his belief that
all software should be free. emacs stands for “editor macros.”

Starting emacs: To start emacs with a blank file, enter the command emacs with no parameters.
Your screen should look like something like the following:

Welcome to GNU Emacs

Get help C-h (Hold down CTRL and press h)


Undo changes C-x u Exit Emacs C-x C-c
Get a tutorial C-h t Use Info to read docs C-h i
Activate menubar F10 or ESC ` or M-`
(`C-' means use the CTRL key. `M-' means use the Meta (or Alt) key.
If you have no Meta key, you may instead type ESC followed by the character.)

If an Emacs session crashed recently, type M-x recover-session RET


to recover the files you were editing.

GNU Emacs 20.7.1 (i386-unknown-freebsdelf4.4)


of Tue Jan 15 2002 on sophist.cs.slcc.edu
Copyright (C) 1999 Free Software Foundation, Inc.

GNU Emacs comes with ABSOLUTELY NO WARRANTY; type C-h C-w for full details.
Emacs is Free Software--Free as in Freedom--so you can redistribute copies
of Emacs and modify it; type C-h C-c to see the conditions.
Type C-h C-d for information on getting the latest version.
Page 2 of 6

If you press enter hey, the next screen will appear as :

This buffer is for notes you don’t want to save, and for Lisp evaluation.
If you want to create a file, visit that file with C-x C-f,
then enter the text in that file’s own buffer.

Notice the status line in the bottom of the screen.

Exit this buffer by pressing ^c ^x .

Try: emacs (Copy and comment, make note of the status bar,exit)

Use clear command to have a better and clean screen.

Try: clear

To create or edit an existing file, specify its name as a command-line parameter.

Try: emacs smartpet.text1 ( Type two or more paragraphs)

To end your text use ^c then ^ x . You should see a statement asking you to save yes ‘y’ or no
‘n’, type y for yes to save your file.

Try: clear

Try: ls ( Copy and comment about the file that you just created, is it there ?)

The line that is second from the bottom is called the mode line, and it contains information in the
following left-to-right order:

-If the first three dashes contain a “**”, it means that the current file has been modified.
-The name that follows “Emacs:” is the name of the current file. If no file is currently loaded, the
name “*scratch*” is used instead.
-The current editing mode line is shown between parentheses. In this case, it’s Fundamental,
which is the standard editing mode.
-The last entry indicates your relative position in the file as a percentage. If the file is very small
and fits completely on the screen, the All is displayed. If you’re at the top or the bottom of a file,
then Top or Bot are displayed, repectively.

emacs Commands: To enter text, simply start typing. The initial emacs welcome banner
automatically disappears when you type the first letter. Long lines are not automatically broken,
so you must press the Enter key when you wish to start a new line. Lines longer than the screen
width are indicated by a “/” character at the end of the screen, and the remainder of the line is
“wrapped” onto the next line:
This is a very long line that illustrates the way that unbroken lines a / re displayed.
This is a much shorter line.
- - - Emacs: *scratch*(Fundamental) --- All ---------
Page 3 of 6

emacs’ s editing features are accessed via either a control sequence or a metasequence. I’ ll
indicate control sequences by preceding the name of the key with the prefix Control-. For
example, the squence “ Control - H t” means “ press and hold the Control key and then
press the “ H” key. (For control sequences, it doesn’ t matter whether you use uppercase or
lowercase, so I suggest that you use lowercase, as it’ s easier). Then release both keys and press
the “ t” key on its own.” Similarly, metasequences use the Esc key. For example, the sequence
“ Esc x” means “ press the Esc key (but don’ t hold it) and then press the “ x” key.” The next few
sections contain many examples of emacs command sequences.

Getting Out of Trouble: Whenever you’ re learning a new editor, it’ s quite easy to get lost and
confused. Here are a couple of useful command sequences to return you to a sane state:
-The command sequence Control-G terminates any emacs command, even if it’ s only partially
entered, and returns emacs to the state at which it’ s waiting for a new command.
-The command sequence Control-X 1 closes all emacs windows except your main file window.
This sequence is useful, as several emacs options create a new window to display information,
and it’ s important to know how to close them once you’ ve read their contents.

Getting Help: There are several ways to obtain help information about emacs. One of the best
ways to get started with emacs is to read the self-describing help tutorial. I suggest that you do so
before anything else. To read the tutorial, use the command sequence Control-H t. The tutorial
will appear and give you directions on how to proceed.
Leaving emacs: To leave emacs and save your file, use the command sequence Control-X
Control-C. If you haven’ t saved your file since it was last modified, you’ ll be asked whether you
want to save it.
emacs Modes: emacs supports several different modes for entering text, including Fundamental,
Lisp Interaction, and C. Each mode supports special features that are customized for the
particular kind of text that you’ re editing. emacs starts in Fundamental mode by default, which is
the mode that you will be using during the description of emacs.

Entering Text:
To enter text, simply start typing. For example, here’ s a short four-line poem:

There is no need for fear in the night,


You know that your Mommy is there,
To watch over her babies and hold them tight,
When you are in her arms you can feel her sigh all night.
- - - Emacs: *scratch* (Fundamental) --- All -------

Try: emacs smartpe.text2 ( Type the above four lines poem, verify the mode line, save and exit,
Comment on what you have seen)

This next section describes the editing features of emacs that allowed me to change this poem to
something a little better.

Common Editing Features: The most common emacs editing features can be grouped into the
following categories:
-cursor movement
-deleting, pasting, and undoing text
Page 4 of 6

-searching text
-search/replacing text
-saving/loading files
-miscellaneous
These categories are described and illustrated in the next set of subsections, using the sample
poem that I entered at the start of this section.
Moving the Cursor: Here’ s a table of the common cursor-movement commands:

Movement Key sequence


Up one line Control-P (previous)

Down one line Control-N (next)

Right one character Control-F (forward, wraps around)

Left one character Control-B (backward, wraps around)

To start of line Control-A (a is first letter)

To end of line Control-E (end)

Back one word Esc b (back)

Forward one word Esc f (forward)

Down one screen Control-V

Up one screen Esc v

Start of file Esc<

End of file Esc>

For example, to insert the words “ worry or” before the word “ fear” on the first line, move the
cursor to the first line of the file by typing Esc< and then moved forward several words by using
the Esc f sequence. Then type in the words, which were automatically inserted at the current
cursor position.
Try: emacs smartpe.text2 ( insert the word worry on before the word fear)

Deleting, Pasting, and Undoing: Here is a table of the common deletion commands:
Item to delete Key sequence
Character before cursor Delete key
Character after cursor Control-D
Word before cursor Esc Delete key
Word after cursor Esc d
To end of current line Control-K
Sentence Esc k
Page 5 of 6

Whenever an item is deleted, emacs remembers it in an individual “ kill buffer.” A list of kill
buffers is maintained so that deleted items may be retrieved long after they have been removed
from the display. To retrieve the last killed item, press Control-Y. After you have pressed
Control-Y, you may press Esc y to replace the retrieved item with the previously deleted item.
Every time you press Esc y, the retrieved item moves one step back through the kill-buffer list.
You may append the next deleted item onto the end of the last kill buffer rather than create a new
one by pressing Esc Control-W immediately prior to executing the delete command. This feature
is useful if you wish to cut different bits and pieces out of a file and then paste them all together
back into one place. You may undo editing actions one at a time by typing Control-X u for each
action that you wish to undo.
Here is a summary of the kill-buffer and undo commands:
Action Key sequence
Insert last kill buffer. Control-Y

Retrieve previous kill. Esc y

Append next kill. Esc Control-W

Undo. Control-X u

Try: emacs smartpe.text2 ( move the curser to the end of the word Mommy, delete it and Undo
it, copy and comment)

Searching: emacs allows you to perform something called an incremental search. To search
forward from your current cursor position for a particular sequence of letters, type Control-S.
The prompt “ I-search:” is displayed on the bottom line of the screen, indicating that emacs wants
you to enter the string that you wish to search for. As you enter the character sequence, emacs
searches to find the first string from your initial cursor position that matches what you’ ve entered
so far; in other words, partial substrings are found as you enter the full string. To terminate the
search and leave your cursor at its current position, press Esc. If you delete characters in the full
string before pressing the Esc key, emacs moves back to the first match of the remaining
substring. To repeat a search, don’ t press Esc, but instead press Control-S to search forward or
Control-R to search backward. Here is a summary of the searching commands:
Action Key sequence
Search forward for str. Control-S str

Search backward for str. Control-R str

Repeat last search forward. Control-S

Repeat last search backward. Control-R

Leave search mode. Esc

Try: while you are in the emacs file , search for some words in smartpe.text2, Comment.

Search/Replacing: To perform a global search/replace procedure, press Esc x, followed by the


string “ repls” followed by the Enter key. emacs then prompts you for the string to be replaced.
Enter the string and press Enter. emacs then prompts you for the replacement string. Enter the
Page 6 of 6

string and press Enter. emacs then performs the global text substitution.
Saving/Loading Files: To save your current work to a file, press Control-X Control-S. If your
work hasn’ t been associated with a filename yet, you will be prompted for a filename. Your work
is then saved into its associated file.

To edit another file, press Control-X Control-F. You will be prompted for the new filename. If
the file already exists, its contents are loaded into emacs; otherwise, the file is created.
To save your file and then quit out of emacs, press Control-X Control-C.
Here’ s a summary of the saving and loading commands:
Action Key sequence
Save current work. Control-X Control-S
Edit another file. Control-X Control-F
Save work and then quit. Control-X Control-C

Miscellaneous: To redraw the screen, press Control-L. To place emacs into auto-wrap mode,
which automatically inserts line breaks when words flow past the end of a line, type “ Esc x auto-
fill-mode” and press Enter. To leave this mode, repeat the command again.

Try: Make your own choice of working with the above table, Comment.