26
Introduction to Unix (CA263) File Editing By Tariq Ibn Aziz

Introduction to Unix (CA263) File Editing By Tariq Ibn Aziz

Embed Size (px)

Citation preview

Page 1: Introduction to Unix (CA263) File Editing By Tariq Ibn Aziz

Introduction to Unix (CA263)

File Editing

By

Tariq Ibn Aziz

Page 2: Introduction to Unix (CA263) File Editing By Tariq Ibn Aziz

Objectives

• After reading this chapter, you should be able to:– Explain the basics of UNIX/Linux files,

including ASCII, binary, and executable files– Understand the types of editors– Create and edit files using the vi editor

Page 3: Introduction to Unix (CA263) File Editing By Tariq Ibn Aziz

The Unix Language

• ASCII, American Standard Code for Information Interchange

• Originally, ASCII used only 7 bits (0 and 1) providing 128 characters

• Extended ASCII uses 8 bits to store 256 characters. For example English letter “H” has a decimal code 72. In term of bits, H is 01001000

Page 4: Introduction to Unix (CA263) File Editing By Tariq Ibn Aziz

ASCII Text Files

• Computer files containing nothing but printable characters are called text file.

• Files containing nonprintable characters, such as machine instructions, are called binary files.

• Unicode offers up to 65,536 characters. • Unicode was developed because 256 character

in ASCII are not enough to support some languages, such as Chinese, that need more than 256 characters.

Page 5: Introduction to Unix (CA263) File Editing By Tariq Ibn Aziz

ASCII Text File

Page 6: Introduction to Unix (CA263) File Editing By Tariq Ibn Aziz

USING THE VI EDITOR

• The vi editor is a modal editor.• It works in three mode

– Insert mode• lets you enter text, is accessed by typing the letter “i”

– Command mode• Which is started by pressing Esc

– Ex mode• Employs an extended set of commands that were initially

used in an early UNIX editor called ex.• Press Esc and type (:) to enter extended command

Page 7: Introduction to Unix (CA263) File Editing By Tariq Ibn Aziz

USING THE VI EDITOR

• Start the vi editor with the –e option (vi –e filename), which places vi exclusively in ex mode.

• To use the vi editor, it is important to master the following tasks:

■ Creating a file■ Inserting, editing, and deleting text■ Searching and replacing text■ Adding text from other files■ Copying, cutting, and pasting text■ Printing a file■ Saving a file■ Exiting a file

Page 8: Introduction to Unix (CA263) File Editing By Tariq Ibn Aziz

Creating a New File in the vi Editor

1. Enter vi plus the name of the file you want to create, such as vi data.

2. Press Esc, type :w

Page 9: Introduction to Unix (CA263) File Editing By Tariq Ibn Aziz

Creating a New File in the vi Editor

• Another Way to create a file1. open the vi editor without specifying the file

name with the vi command

2. You can save the file and specify a file name at any time by pressing Esc, typing :w filename

Page 10: Introduction to Unix (CA263) File Editing By Tariq Ibn Aziz

Using the vi Editor (continued)

To create a new file in the vi editor, type vi and the name of the new file at the command prompt

Page 11: Introduction to Unix (CA263) File Editing By Tariq Ibn Aziz

Inserting Text

• Before you can insert text in your new file, you must use the i (insert) command.

• In insert mode, every character you type appears on the screen.

• You can return to command mode at any time by pressing the Esc key.

Page 12: Introduction to Unix (CA263) File Editing By Tariq Ibn Aziz

Repeating a Change

• When started, the vi editor is in command mode– To insert text into a file, you must switch to

insert mode – You can repeat the line just entered with the

repeat command (.)– Note: You must press Esc to use (.) to repeat

the command

Page 13: Introduction to Unix (CA263) File Editing By Tariq Ibn Aziz

Moving the Cursor

• Designers of vi chose the letter keys because of their relative position on the keyboard.

Page 14: Introduction to Unix (CA263) File Editing By Tariq Ibn Aziz

Deleting Text

• 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

Page 15: Introduction to Unix (CA263) File Editing By Tariq Ibn Aziz

Searching and Replacing Text

1. Searching and replacing is a line-oriented command that executes independently of the cursor position

2. To find more instances, type n while you are in command mode

Page 16: Introduction to Unix (CA263) File Editing By Tariq Ibn Aziz

Save File and Exit

• 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, type the :w

(write) command– While in command mode, use the :wq (write and quit)

command to save and exit iv, or the :zz command to exit after saving

– You can also use :x to save and exit

Page 17: Introduction to Unix (CA263) File Editing By Tariq Ibn Aziz

Save File and Without Exiting

Page 18: Introduction to Unix (CA263) File Editing By Tariq Ibn Aziz

Information about file status

• If you are in an editing session and want to review information about the file status, press Ctrl+g or Ctrl+G

• The status line at the bottom of the screen displays information, including line-oriented commands and error messages.

Page 19: Introduction to Unix (CA263) File Editing By Tariq Ibn Aziz

Adding Text from Another File

• To copy the entire contents of one file into another file: – (1) use the vi editor to edit the file you want to

copy into; and – (2) use the command :r filename, where

filename is the name of the file that contains the information you want to copy

Page 20: Introduction to Unix (CA263) File Editing By Tariq Ibn Aziz

Leaving vi Temporarily

• If you want to execute other UNIX/Linux commands while you work with vi, you can launch a shell or execute other commands from within vi.– Go to command mode– Type :!cal (a colon, an exclamation point, and the

command) – press Enter.

Page 21: Introduction to Unix (CA263) File Editing By Tariq Ibn Aziz

Using the vi Editor (continued)

1. Turn on line numbering when you want to work with a range of lines and refer to the line numbers to specify text

2. To turn on line numbering, use the :set number command

Page 22: Introduction to Unix (CA263) File Editing By Tariq Ibn Aziz

Copying or Cutting and Pasting

• You can use the yy command in vi to copy a specified number of lines

• To cut the lines from the file and store them on the clipboard, use the dd command.

Page 23: Introduction to Unix (CA263) File Editing By Tariq Ibn Aziz

Printing Text Files

• Sometimes you want to print a file before you exit the vi editor.

• Type !lpr and then type the name of the file you want to print.

• You might have two printers, lp1 and lp2.To print the file accounts to lp2, enter :!lpr -P lp2 accounts and press Enter.

Page 24: Introduction to Unix (CA263) File Editing By Tariq Ibn Aziz

Canceling an Editing Session

• You might have to cancel editing session.

• To cancel session, enter :q! and press Enter.

Page 25: Introduction to Unix (CA263) File Editing By Tariq Ibn Aziz

Getting Help in vi

• While you are in vi, press Esc, type :!man vi, and press Enter

• Type q and press Enter to go back into your editing session.

Page 26: Introduction to Unix (CA263) File Editing By Tariq Ibn Aziz

Chapter Summary• Bytes are computer characters stored using

numeric code (e.g., ASCII)

• The vi editor is a popular choice among UNIX/Linux users to edit text files