Chapter Three - Mastering Editors

Download Report

Transcript Chapter Three - Mastering Editors

Chapter Three
The UNIX Editors
Lesson A
The vi Editor
2
Objectives
• Describe an ASCII text file
• Explain why operating system editors use
ASCII files
• Create and edit simple documents using
the vi editor
3
Understanding UNIX Files
• Almost everything you create in UNIX is
stored in a file
• All information stored in files is in the form
of binary digits
– Binary digits are also known as bits
– Bits have two states: 1 (on) and 0 (off)
– 0’s and 1’s as a way to communicate with a
computer is known as machine language
4
Understanding UNIX Files
• Machine language (or bit combinations) is
translated into plain English using ASCII
• ASCII stands for American Standard Code for
Information Interchange
– ASCII uses a byte (a string of 8 bits) to represent
keyboard characters such as letters and numbers
– Text files contain printable, ASCII characters
– Binary files contain nonprintable characters, or
machine language
5
6
Understanding UNIX Files
• Programmers develop source code for their
programs as text files which are compiled before
being executed
• Programmers also create scripts which are files
containing commands. Scripts are not compiled,
rather executed through an interpreter at run
time
• Executable Program Files are compiled and
interpreted files that can be run on the computer
to cause actions to be taken
7
Using Editors
• Editors let you create and edit ASCII files
• UNIX normally includes two editors: vi and
Emacs
• Vi and Emacs are screen editors: they
display the text you are creating (or
editing) one screen at a time
8
Using the vi Editor
• Called vi because it is visual – it immediately
displays on screen the changes that you make
to text
• It is also modal – works in three modes
– Insert mode - lets you enter text
– Command mode - lets you enter commands to
perform editing tasks
– Extended (ex) command set mode - lets you use an
extended set of editing commands
9
Using the vi Editor
To create a new
file in the vi editor,
type vi and the
name of the new
file at the
command prompt
10
Using the vi Editor
• When started, the vi editor is in command
mode
– In order to insert text, you must issue the “i”
command to enter insert mode
– You can repeat the line just entered with the
repeat command (.)
– To edit what you’ve just typed, move the
cursor with the various keyboard cursor
movement keys
11
Using the vi Editor
In insert mode,
every character
you type appears
on the screen.
You are actively
entering text into
the file.
12
Using the vi Editor
To return to
command mode, hit
the Escape key.
Now, hit the . key to
repeat the last line.
The repeat command
repeats the last
insertion, or line.
Now, edit the new line
in order to create the
next item
13
Using the vi Editor
14
Using the vi Editor
• While still in command mode:
– To delete text, move to a character and then
type “x”
– You can undo a command (reverse its effects)
by typing “u”
– To search for a text pattern, type a forward
slash (/), type the pattern, and press Enter
15
Using the vi Editor
16
Using the vi Editor
Use the delete
commands and
the cursor
movement keys
to edit the text
17
Using the vi Editor
18
Using the vi Editor
19
Using the vi Editor
The status line at
the bottom of the
screen displays
information,
including lineoriented
commands and
error messages
20
Using the vi Editor
During text
searches, you can
replace, too. Use
line-oriented
commands in ex
mode to perform
this action
21
Using the vi Editor
• Saving a File and Exiting vi
– You should always save the file before exiting
vi, otherwise changes are lost
– To save a file and continue working on it, hit
Escape, type :w filename (write)
– While in command mode, use the :wq (write
and quit) command to save and exit vi, or the
:zz command to exit after saving
22
Using the vi Editor
• In vi, you can also:
– Add text from another file
– Leave vi temporarily to perform other UNIX
tasks, then return to your file
– Change your display while editing, such as
adding line numbering to help editing
– Copy, cut, and paste text to help editing
– Print text files
– Cancel an editing session
23
Using the vi Editor
24
Using the vi Editor
25
Lesson B
The Emacs Editor
Victoria College doesn’t have access to
an Emacs editor so just read through
the following section. You won’t have
any labs on Emacs
26
Vi Practice
•
In the vi editor, show the command used to do the following:
•
1.change to insert mode
–
•
2. change from insert mode to command mode
–
•
<Escape>/unix <Enter>
Search for “unix” throughout the entire document and replace it with “linux”
–
•
<Escape>O (uppercase O inserts a blank line above, lowercase o inserts a line below the cursor position)
6. search for the word “unix” in the current document
–
•
<Escape>:wq OR
<CTRL>ZZ OR
<Escape>:x
5. add a blank line above the current cursor position
–
•
x (You must be in command mode – if you are in insert mode, an x will be entered into the file.)
4. exit the editor and save changes in one step. Assuming that you have already associated a file
name with the file.
–
–
–
•
Hit the escape key
3. delete a character
–
•
i
<Escape>:1,$s/unix/linux/g
7. exit the editor without saving changes
–
<Escape>:q!
27
Objectives
• Compare and contrast the features of
Emacs and the vi editor
• Become familiar with the most important
Emacs editor commands
• Create and edit simple documents using
Emacs editor
28
Using the Emacs Editor
• More complex than vi, so not as popular, but it is
more consistent and most commands begin with
Alt or Ctrl key combinations
• It is not modal like vi
• Supports a sophisticated macro language and
lets you extend beyond the Emacs program and
read email, for instance
• Emacs is reputed to have more features than
any other UNIX program
29
30
31
Using the Emacs Editor
• Creating a New File in Emacs
– Type the Emacs command with the name of
the new file after it at the command line
– If you are using the GNOME interface, start
Emacs by clicking the Main Menu icon,
clicking Programs, clicking Applications, and
then clicking Emacs
32
Using the Emacs Editor
The Emacs editor
opens in a GUI,
with menus
containing
command choices
33
Using the Emacs Editor
Notice the
extensive
command choices
within the menu
structure
34
Using the Emacs Editor
To save and exit,
perform these
key sequences:
Ctrl+x and Ctrl+s
to save, then
Ctrl+x and Ctrl+c
to exit
35
Using the Emacs Editor
• Editing an Emacs File
– To navigate the cursor location, use the arrow keys,
or Ctrl key combinations
– Text is inserted by typing
– Ctrl+x and then “u” is the undo command
– You can cut, copy and paste text
– There is a text search feature as well
– Emacs lets you format your documents with the word
wrap feature
36
Chapter Summary
• Bytes are computer characters stored using
numeric code, the numeric code is then
formatted to plain English via ASCII, and the
ASCII characters are stored in text files
• The vi editor is a popular choice among UNIX
users to edit text files
• In the vi editor’s insert mode, characters you
type are inserted in the file
• With vi, you initially edit a copy of the file in the
computer’s memory and the existing file itself is
not altered until saved to disk
37
Chapter Summary
• The Emacs editor is popular as an alternative
to the vi editor and is included with UNIX
systems, including Linux
• Emacs has a powerful command syntax, is
extensible, and supports a sophisticated
language of macro commands
• You start Emacs by typing the emacs
command at the command line
• You navigate an Emacs file by using cursor
movement keys or Ctrl key combinations
38
Chapter Summary
• In Emacs, you can undo your editing
changes in sequence, even after you’ve
made many changes
• In Emacs, you insert text simply by typing
it in, plus you have the editing features of
copy, cut and paste, as well as text
searching features
39
40