Embed
Email

Vi Editor

Document Sample
Vi Editor
Shared by: HC120210114158
Categories
Tags
Stats
views:
0
posted:
2/10/2012
language:
pages:
32
Unix vi Editor



Command line full screen editor

Agenda

• Announcements:

• Review of homework

• Lecture on vi editor

• Exam review

Objectives

• To introduce the vi editor:

– edit mode (inline and ex commands)

• command syntax

• typical navigation commands

• typical delete commands

• typical modify commands

– insert mode

• entry

• exit

vi Editor Command Syntax

vi –options filename

Selected options:

– +[num] # start at line num, otherwise the last

line

– +/pattern # find the first occurrence of the

pattern

– -r # recover filename (from .filename.swp)

– -R # read only mode (does not allow changes)

vi Editor

• Found on almost all Unix systems.

• Good to know, as many commands use vi

syntax

• Contains two modes:

– edit – command line mode (default)

– insert – keystrokes are added to file (exited

via Esc key)

vi File Interaction

nothing (if file does not already exist)



FileC

vi FileA

:r FileC

Work

space

:w FileB (if file does not exist)1

:w! FileB (if file exists) 1

FileA



FileB

:q!

.FileA.swp

No changes



1. Note- :w[!] simply writes to the file, the file being edited is still the same

vi File Interaction (2)

Read-only mode

read only mode







vi -R FileA





Work

space

:w FileB (if file does not exist) 1

:w! FileB (if file exists) 1

FileA



FileB

ZZ or :q



No changes



1. Note- :w[!] simply writes to the file, the file being edited is still the same

vi File Interaction (3)

Recovery mode

recovery mode







vi -r FileA







.FileA.swp Work

space

:w FileB (if file does not exist)

:w or ZZ :w! FileB (if file exists)

FileA



FileB

ZZ,:q, or q!



No changes

1. Note: The swap file must be manually deleted (e.g. rm .FileA.swp) after recovery

has been accomplished.

Entering the vi Editor

Command Description

vi filename Edits an existing filename or creates a new one if

filename does not exist.

vi –r filename Edits filename using .filename.swp as the input . The

file .filename.swp must be deleted, using:

rm .filename.swp, after use.

vi –R filename Edits filename in read-only mode does not allow

changes to filename.

vi +/pattern filename Edits filename at the line containing the pattern.

File Interaction in the vi Editor

Command Description

:w Write to the file that your are editing without exiting the editor.

Good for intermediate saves.

:w def Write to a new file def, but stay in the existing file.

:w! def Write to an existing file def, but stay in the existing file.

:r def add (read) file def into the current file

:f Provides information on the file currently editing, including the

current line number, if it has been modified etc.

Exiting the vi Editor



Command Description

:q Exit (quit) without changes. Assumes no changes have been

made.

:q! Exit (quit) without changes. Assumes changes have been made.

ZZ or :x Save the file only if changes have been made, then exit the

editor

:wq Save the file and exit the editor. This command will save the file

even if no changes are made.

vi Editor

When Things Go Wrong

A common error is to accidentally leave the caps lock on

when issuing commands. The vi editor is case sensitive

and will not necessarily do what you want it to do if the

caps lock is on.



Another common error is to be in the incorrect mode when

typing. Sometimes we begin entering data when we are

not in insert mode. Nothing will happen until we type in one

of the insert mode characters (e.g. i, o, O, A, etc).



Related to this is when you forget to exit the insert mode

and try the edit commands. Be sure to hit the Esc key prior

to editing.

vi Editor

When Things Go Wrong

Remember to create the directory, prior to creating a

file using the vi editor. For example, if you wish to

create a new file called labin in the mydata directory

(that does not currently exist), you must do this:

1. mkdir mydata

2. vi labin

Attempting to do it all using the vi editor will result in

errors. If the directory mydata does not exist, do NOT

do this:

vi mydata/labin

vi Editor

When Things Go Wrong

A common error is to accidentally type the control-Z instead

of the shift-ZZ. The control-Z stops and terminates the

editor without saving the contents. If this occurs follow

these steps:

1. type in ps to see the active processes

2. kill the vi editor using the kill -9 pidnumber command

3. Upon reentry into the editor, you will be notified of the

recovery file .filename.swp . If you wish to save the

changes, use the R option and then :w to write the

changes out.

4. upon exiting the editor you will have to delete the

.filename.swp file.

edit mode

• The default edit mode also has two sub

modes

– in line - where the cursor moves within the

text

– ex commands - where the cursor moves to

the bottom of the screen. ex commands are

preceded with a colon (:), a forward slash (/),

or a question mark (?)

General Command Syntax

The general format for commands is

ncm

Where:

• n is an optional multiplier value

• c is the command

• m is an optional scale modifier

Examples:

3dw – delete 3 special character delimited words

dW – delete a single, space, delimited word

Insert Commands

i, Enter insert mode at this location, exit

insert mode

o, O Insert new line after/before current

line

a,A Append after cursor, end of current

line

I (capital i) Insert at beginning of line



NOTE: Insert commands do not have multipliers or scale modifiers

Two Reasons to Know the vi

Commands

1. Many of us will discover that learning all of these

commands is not important, because we can simply

enter input mode and proceed like most Window’s

style editors. This is NOT true!!!! Because the files

we are editing are small, navigation and changes

are fairly simple. In the real world these files can be

quite significant in size, often in the thousands of

lines. Knowing the editor commands will greatly

improve your ability to work with these large files.



2. Because these commands may appear on your test,

you might as well practice the commands, which

after a bit of use will greatly improve your editing

skills.

Delete Commands

ndm where modifiers include:



d Delete this line

w Delete special character delimited word

W Delete whitespace delimited word

} Delete to next paragraph

^ Delete to beginning of the line

/pattern Delete to first occurrence of pattern

n Delete to next occurrence of pattern

fa Delete up to and including a on this line

ta Delete up to (not including) a on this line

Delete Character Commands

nx Delete current [and n-1] character[s]

nX Delete previous n character[s]

Replace Commands

rx Replace current character with x

Rtext Replace text beginning at cursor

cd Replace based on dimension d (e.g.

cw replaces a word) use escape to

terminate replacement

Splitting and Joining Lines

• If you wish to split a line between words,

position yourself on a space between the

words to be split, then use the replace (r)

followed by the return key. This will

replace the space with a carriage return

forming a new line.

• If you wish to join or combine two lines go

to the upper of the two lines to be joined,

then key in the uppercase “J” for join.

Splitting and Joining Lines

Example:

We wish to split the line below between the

word “should” and “be”.

“This line should be split.”



1. Position the cursor here

2. Key in “r” followed by “enter”



To join the lines:

1. Position the cursor anywhere on this line

“This line should

2. Key in “J” followed by “enter”

be split.”

Navigation Commands

nc where c is:

h,j,k,l Move cursor left, down, up, or right characters

w,W Move to beginning of special character delimited

word, whitespace delimited word.

e,E Move to end of special character delimited word,

whitespace delimited word

b,B Move back to beginning of special character

delimited word, whitespace delimited word.

(,) Open or closed parenthesis, move to beginning, end

of current sentence.

{,} Open or closed curly braces, move to beginning, end

of current paragraph.

Current Line

Navigation Commands

0,$ First and last position

^ First non-blank position

n| n Column (| is the pipe symbol)

Search Commands

/text Search for text forward (wraps to

beginning)

?text Search for text backward

n Search for same text again

N Search for text reverse direction of

initial search

Copy/Paste Commands

[n]Y or Copy (yank) n lines

[n]ym Copy (yank) a portion of the file

determined by the measurement (e.g.

2yW will yank 2 space separated

words.

P,p Paste yanked or deleted data before

(P), after (p) cursor position



Note: to cut and paste use the

dm commands to delete then the

p or P to paste

Substitute/Replace Command

Format :[address] s /old/new/[g]



Where address is:

n A given line number

n,m For lines beginning with n ending with m

1,. For lines beginning with 1 ending with current line

.,$ For lines beginning with the current line to the end of the file

1,$ The whole file

or %





[g] the optional g stands for global (make multiple changes on each

line, otherwise only the first occurrence on each line will be changed)

Substitute/Replace Command

Important: By convention the delimiter for the

patterns is the forward slash (/). When using the

substitute command, make sure the delimiter is

not part of the old pattern or the new pattern.

For example if we wished to change the date

from mm/dd/yyyy format to mm.dd.yyy format,

we cannot use the forward slash (/) or the period

(.) as delimiters.

we could use something like this:

:% s $/$.$ g

where the dollar sign ($) is now the delimiter

Some “nice to know” features

• To execute a single Unix command from inside the editor

use:

– :!cmd

• To temporarily return to the shell use:

– :sh (type “exit” to return to editor)

• To repeat a colon command or to go back to earlier

colon commands use:

– : ↑ or↓(up arrow or down arrow)

• To show the name of the current file use:

– :f

vi Editor Review

• the vi editor:

– edit mode (inline and ex commands)

• command syntax

• typical navigation commands

• typical delete commands

• typical modify commands

– insert mode

• entry

• exit

Unix vi Editor



Command line full screen editor


Other docs by HC120210114158
registration form edu
Views: 1  |  Downloads: 0
Hancock County Middle School
Views: 1  |  Downloads: 0
Austin Film Festival
Views: 0  |  Downloads: 0
Developing a Learning Module
Views: 0  |  Downloads: 0
Todd Geerdes (SIMPLIFIED MEMO)
Views: 2  |  Downloads: 0
Wood Council RT Architectural Pattern Book
Views: 0  |  Downloads: 0
By registering with docstoc.com you agree to our
privacy policy

You are almost ready to download!

You are almost ready to download!