chapter 3: the unix editors ascii and vi editors

24
Chapter 3: The UNIX Editors ASCII and vi Editors

Upload: kerry-wilcox

Post on 30-Dec-2015

245 views

Category:

Documents


4 download

TRANSCRIPT

Chapter 3: The UNIX Editors

ASCII and vi 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

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.

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

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

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)

ASCII Characters

Example

• THE QUICK GREY FOX JUMPED OVER THE LAZY COWS.

• od -xc fox.txt

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

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:

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

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

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'

Insert mode

• i (insert text before the cursor)

• a (insert text after the cursor)

• A (insert text at end of line)

• cw (change word)

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 $

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

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)

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

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

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

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

Latex

• A script language for editing documents

• Eample:

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

• latex tutorial

• dvips –o tutorial.ps tutorial.dvi

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

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.