chapter 3: the unix editors

24
Chapter 3: The UNIX Editors ASCII and vi Editors

Upload: marin

Post on 14-Jan-2016

68 views

Category:

Documents


2 download

DESCRIPTION

Chapter 3: The UNIX Editors. ASCII and vi Editors. The vi Editor Objectives. After studying this lesson, you should be able to: Describe an ASCII text file Explain why operating system editors use ASCII files Create and edit simple documents using the vi editor. Program and Data. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Chapter 3:  The UNIX Editors

Chapter 3: The UNIX Editors

ASCII and vi Editors

Page 2: Chapter 3:  The UNIX Editors

The vi EditorObjectives

After studying this lesson, you should be able to:

– Describe an ASCII text file

– Explain why operating system editors use ASCII files

– Create and edit simple documents using the vi editor

Page 3: Chapter 3:  The UNIX Editors

Program and Data

• Executable program files contain pure binary or

machine language that the computer can

immediately use or execute

• Data contains information. May be text, numeric,

images, audio, video, …

• Today, we limits our attention only to text and

numeric data.

Page 4: Chapter 3:  The UNIX Editors

How files are stored?

• Both programs and data in UNIX are stored in files

• All information stored in files is in the form of binary digits

• A binary digit, called bit for short, consists of two numbers, 0 and 1

• The exclusive use of 0s (which mean “off”) and 1s (which mean “on”) as a way to communicate with the computer is known as machine language

Page 5: Chapter 3:  The UNIX Editors

ASCII Text Files

• To make information stored in files accessible, computer designers established a standard method for translating binary numbers into plain Englishtranslating binary numbers into plain English

• This standard used a string of eight binary numbers, called a byte, which is an acronym for “binary term”

• Each byte, or code, has been standardized into a set of bit patterns known as ASCII codes

• ASCII stands for the American Standard Code for Information Interchange

Page 6: Chapter 3:  The UNIX Editors

ASCII Text Files

• Standard encoding scheme used to represent characters in binary format on computers

• Was 7-bit encoding, so 128 characters can be represented. (Now is 8-bit encoding, including Arabic, French, German, etc.)

• 0 to 31 (& 127) are "control characters" (cannot print)– Ctrl-A or ^A is 1, ^B is 2, etc.– Used for screen formatting & data

communication• 32 to 126 are printable (95 printable symbols)

Page 7: Chapter 3:  The UNIX Editors

ASCII Characters

Page 8: Chapter 3:  The UNIX Editors

Example

• THE QUICK GREY FOX JUMPED OVER THE LAZY COWS.

• od -xc fox.txt

Page 9: Chapter 3:  The UNIX Editors

Is ASCII code enough?

• Is the ASCII code enough?– No. Chinese or Japanese language text (thousands of

symbols)• Unicode

– A 16-bit coding scheme (allows for how many characters?)

– Developed by consortium of major American computer manufacturers, primarily to overcome the chaos of different coded character sets in use when creating multilingual programs and internationalizing software.

• ISO 32-bit code– Developed by International Organization for

Standardization– Allows for (how many ?) characters

Page 10: Chapter 3:  The UNIX Editors

Using Operating System Editors

• Operating system editors let you create and edit simple ASCII files

• UNIX includes three editors:

– vi

– Emacs

– pico

• We only cover vi Editor:

Page 11: Chapter 3:  The UNIX Editors

vi Editor

• vi is not user-friendly! You have to remember all of the commands, in addition to which mode the editor is in.

• However, vi is very powerful and fast once you have mastered it.

• The vi editor remains the choice of most UNIX users

Page 12: Chapter 3:  The UNIX Editors

Using the vi Editor

• It is also a modal editor; that is, it works in two modes:

– Insert mode lets you enter text

–Command mode (default node) lets you enter commands to perform editing tasks, such as moving through the file and deleting text

Page 13: Chapter 3:  The UNIX Editors

Starting and Exiting vi

• Starting:– vi begin editing unnamed file

– vi file1.txt begin editing file1.txt

• Exiting (used only in command mode):– :q quit (assumes no changes made)

– :q! quit, discard any changes that were made

– :wq write the file, then quit

– ZZ write the file, then quit

– :w newname write the file to 'newname'

Page 14: Chapter 3:  The UNIX Editors

Insert mode

• i (insert text before the cursor)

• a (insert text after the cursor)

• A (insert text at end of line)

• cw (change word)

Page 15: Chapter 3:  The UNIX Editors

Navigating

• Although the <PageUp> and <PageDown> and arrow keys work on some systems, I would strongly discourage their use. The following commands are guaranteed to work properly on all systems.

• vi equivalent– Page Up ^F– Page Down ^B– Left Arrow h– Right Arrow l– Up Arrow k– Down Arrow j– Begin _– End $

Page 16: Chapter 3:  The UNIX Editors

Deleting

• delete current line dd • delete from cursor to end of line D • delete character under cursor x • delete character before cursor (backspace) X • Note that many of the above commands can be

preceded by a number, for example: – 9x delete 9 characters

– 3dd delete 3 lines

Page 17: Chapter 3:  The UNIX Editors

Setting Line Number

• Show line numbers :set nu

• Turn off line numbers :set nonu

• display current line number/file name ^g

• go to line number :1 (go to line 1) G (go to last line in file)

Page 18: Chapter 3:  The UNIX Editors

Copying

• copy current line to "clipboard“ yy

• paste contents of "clipboard" below current line p

• paste contents of "clipboard" above current line P

• copy current line, and next 4 lines to "clipboard“ 5yy

Page 19: Chapter 3:  The UNIX Editors

Searching for a Pattern

• You can search forward for a pattern of characters by typing a forward slash (/), typing the pattern you are seeking, and then pressing Enter

#include <iostream>using std::cout;int main(){ cout << "Hello, World, in C++." << endl; return 0;}~/World 6,19 All

Page 20: Chapter 3:  The UNIX Editors

Searching

• set case insensitive search :set ic• set case sensitive search :set noic• search forward (down) for "hello“ /hello• search backward (up) for "hello“ ?hello• search again, (same direction as original) n• search again, (opposite direction as original) N• search for "hello" at start of a line /^hello• search for "hello" at end of a line /hello$• search for "hello" or "Hello“ /[hH]ello• search for "int" as a word (i.e. not print or sprint) /\<int\>• search for "eat" but not "beat" or "neat“ /\[^bn]eat

Page 21: Chapter 3:  The UNIX Editors

Replacing

• replace "dog" with "cat" (first occurrence of dog) on the current line :s/dog/cat

• replace "dog" with "cat" on lines 1 -> 3 (first occurrence of dog on each line) of the file :1,3s/dog/cat

• replace "dog" with "cat" on lines 1 -> 3 of the file, every occurrence :1,3s/dog/cat/g

• replace "dog" with "cat" (every occurrence) for the entire file :1,$s/dog/cat/g

• replace "dog" with "cat" (every occurrence) for the entire file (alternative method) :%s/dog/cat/g

• replace "dog" with "cat" (every occurrence) for the entire file but confirm each replacement :%s/dog/cat/gc

Page 22: Chapter 3:  The UNIX Editors

Latex

• A script language for editing documents

• Eample:

• cp ~yzhu/public_html/cs251/tutorial.tex .

• latex tutorial

• dvips –o tutorial.ps tutorial.dvi

Page 23: Chapter 3:  The UNIX Editors

Chapter Summary

• Bytes are “computer characters” referred to as “codes”

• These codes have been standardized and are known as ASCII codes

• The vi editor remains the choice of most UNIX users

Page 24: Chapter 3:  The UNIX Editors

Chapter Summary Continued

• The vi editor is a modal editor, because it works in two modes: insert mode and command mode

• In the vi editor’s insert mode, characters you type are inserted in the file

• You have to remember all of the commands to be more efficient.