UNIX Intro.86 10. vi vi is the standard UNIX text editor v Contents 1.Why use vi ? 2. vi Basics...

25
UNIX Intro. 10. 10. vi vi vi vi is the standard UNIX text is the standard UNIX text editor editor Contents Contents 1. 1. Why use Why use vi vi ? ? 2. 2. vi vi Basics Basics 3. 3. Moving Around Moving Around 4. 4. Inserting Text Inserting Text

Transcript of UNIX Intro.86 10. vi vi is the standard UNIX text editor v Contents 1.Why use vi ? 2. vi Basics...

Page 1: UNIX Intro.86 10. vi  vi is the standard UNIX text editor v Contents 1.Why use vi ? 2. vi Basics 3.Moving Around 4.Inserting Text.

UNIX Intro. 1

10. 10. vivi vivi is the standard UNIX text editoris the standard UNIX text editor

ContentsContents

1.1. Why use Why use vivi??

2.2. vivi BasicsBasics

3.3. Moving AroundMoving Around

4.4. Inserting TextInserting Text

Page 2: UNIX Intro.86 10. vi  vi is the standard UNIX text editor v Contents 1.Why use vi ? 2. vi Basics 3.Moving Around 4.Inserting Text.

UNIX Intro. 2

5.5. DeletionDeletion

6.6. Cut & PasteCut & Paste

7.7. File-related CommandsFile-related Commands

8.8. Text SubstitutionText Substitution

9.9. Recovering after a CrashRecovering after a Crash

Page 3: UNIX Intro.86 10. vi  vi is the standard UNIX text editor v Contents 1.Why use vi ? 2. vi Basics 3.Moving Around 4.Inserting Text.

UNIX Intro. 3

1. Why use vi?1. Why use vi?

very powerfulvery powerful useful simple subset of commandsuseful simple subset of commands portable (PCs, mainframes, etc.)portable (PCs, mainframes, etc.) designed for slow networksdesigned for slow networks full-screenfull-screen

Page 4: UNIX Intro.86 10. vi  vi is the standard UNIX text editor v Contents 1.Why use vi ? 2. vi Basics 3.Moving Around 4.Inserting Text.

UNIX Intro. 4

2. vi Basics2. vi Basics

2.1.2.1. Starting viStarting vi

2.2.2.2. Two ModesTwo Modes

2.3.2.3. The vi WindowThe vi Window

2.4.2.4. When to type When to type RETURNRETURN

2.5.2.5. Finishing a vi SessionFinishing a vi Session

Page 5: UNIX Intro.86 10. vi  vi is the standard UNIX text editor v Contents 1.Why use vi ? 2. vi Basics 3.Moving Around 4.Inserting Text.

UNIX Intro. 5

2.1. Starting vi2.1. Starting vi

vi filevi file Start editing Start editing filefile

Changes are stored in a buffer, so you must Changes are stored in a buffer, so you must save to change the file. save to change the file.

If the machine crashes, the buffer can If the machine crashes, the buffer can usually be recovered (see later).usually be recovered (see later).

Page 6: UNIX Intro.86 10. vi  vi is the standard UNIX text editor v Contents 1.Why use vi ? 2. vi Basics 3.Moving Around 4.Inserting Text.

UNIX Intro. 6

2.2. Two Modes2.2. Two Modes Command modeCommand mode

– move cursor, save, delete text, quit vi, etc.move cursor, save, delete text, quit vi, etc.

Input modeInput mode– for inserting textfor inserting text– start by typing i; finish with start by typing i; finish with ESCESC

– cannot quit, delete, etc. in this modecannot quit, delete, etc. in this mode– If in doubt, press If in doubt, press ESCESC a few timesa few times. This will put you . This will put you

back in command mode.back in command mode.

Page 7: UNIX Intro.86 10. vi  vi is the standard UNIX text editor v Contents 1.Why use vi ? 2. vi Basics 3.Moving Around 4.Inserting Text.

UNIX Intro. 7

2.3. The vi Window2.3. The vi Window

Bottom line is the Bottom line is the statusstatus line line

Some, Some, butbut not allnot all, commands are shown on , commands are shown on the status line.the status line.

Often you type a command and nothing Often you type a command and nothing appears on the screen!appears on the screen!

Page 8: UNIX Intro.86 10. vi  vi is the standard UNIX text editor v Contents 1.Why use vi ? 2. vi Basics 3.Moving Around 4.Inserting Text.

UNIX Intro. 8

ColonColon commands (e.g. commands (e.g. :q!:q!) and ) and searchsearch commands (e.g. commands (e.g. /text/text) require a ) require a RETURNRETURN..

Commands that start with a Commands that start with a letterletter (e.g. (e.g. ZZZZ, , GG) and control characters (e.g. ) and control characters (e.g. ^L^L) ) do do notnot require a require a RETURNRETURN

2.4. When to type 2.4. When to type RETURNRETURN

Page 9: UNIX Intro.86 10. vi  vi is the standard UNIX text editor v Contents 1.Why use vi ? 2. vi Basics 3.Moving Around 4.Inserting Text.

UNIX Intro. 9

2.5. Finishing a vi Session2.5. Finishing a vi Session

Get to command mode (press Get to command mode (press ESCESCs)s)

ZZZZ save changes to the file and quitsave changes to the file and quit(no (no RETURNRETURN))

:q!:q! quit without savingquit without saving(press (press RETURNRETURN))

Page 10: UNIX Intro.86 10. vi  vi is the standard UNIX text editor v Contents 1.Why use vi ? 2. vi Basics 3.Moving Around 4.Inserting Text.

UNIX Intro. 10

3. Moving Around3. Moving Around

3.1.3.1. Basic Cursor MovementsBasic Cursor Movements

3.2.3.2. Larger MovesLarger Moves

Page 11: UNIX Intro.86 10. vi  vi is the standard UNIX text editor v Contents 1.Why use vi ? 2. vi Basics 3.Moving Around 4.Inserting Text.

UNIX Intro. 11

3.1. Basic Cursor Movements3.1. Basic Cursor Movements

hh move cursor one place to leftmove cursor one place to left

jj down onedown one

kk up oneup one

ll right oneright one

ww move forward one wordmove forward one word

bb back one wordback one word

No RETURNrequired!

Page 12: UNIX Intro.86 10. vi  vi is the standard UNIX text editor v Contents 1.Why use vi ? 2. vi Basics 3.Moving Around 4.Inserting Text.

UNIX Intro. 12

3.2. Larger Moves3.2. Larger Moves

GG go to last linego to last line

<number>G<number>G go to line go to line numbernumber

10G10G

^G^G shows the current line numbershows the current line number

^F^F Forward a screenForward a screen

^B^B Back a screenBack a screen

Page 13: UNIX Intro.86 10. vi  vi is the standard UNIX text editor v Contents 1.Why use vi ? 2. vi Basics 3.Moving Around 4.Inserting Text.

UNIX Intro. 13

/text/text Search forward for Search forward for texttext

/func/func search for search for funcfunc

/printf(/printf( search for search for printf(printf(

//^̂foofoo search for search for foofoo at start of line at start of line

Type RETURN!

Page 14: UNIX Intro.86 10. vi  vi is the standard UNIX text editor v Contents 1.Why use vi ? 2. vi Basics 3.Moving Around 4.Inserting Text.

UNIX Intro. 14

4. Inserting Text4. Inserting Text

Move to insertion pointMove to insertion point

Switch to input mode:Switch to input mode: i i

Start typing; Start typing; BACKSPACE BACKSPACE or or DELETE DELETE

for deletionfor deletion

ESCESC finish; back in command modefinish; back in command mode

No RETURN

Page 15: UNIX Intro.86 10. vi  vi is the standard UNIX text editor v Contents 1.Why use vi ? 2. vi Basics 3.Moving Around 4.Inserting Text.

UNIX Intro. 15

Over a slow network, the screen may not Over a slow network, the screen may not refresh properlyrefresh properly

^L^L refresh screen (in command mode)refresh screen (in command mode)

Page 16: UNIX Intro.86 10. vi  vi is the standard UNIX text editor v Contents 1.Why use vi ? 2. vi Basics 3.Moving Around 4.Inserting Text.

UNIX Intro. 16

5. Deletion5. Deletion

Must be in command mode.Must be in command mode.

xx Delete Delete charactercharacter that cursor is on. that cursor is on.

dddd Delete current Delete current lineline..

DD Delete from cursor position Delete from cursor position to to end of lineend of line

uu UndoUndo last command last command

Page 17: UNIX Intro.86 10. vi  vi is the standard UNIX text editor v Contents 1.Why use vi ? 2. vi Basics 3.Moving Around 4.Inserting Text.

UNIX Intro. 17

:i,jd:i,jd Delete lines i to jDelete lines i to j:23,29d:23,29d Delete lines 23 to 29Delete lines 23 to 29

Special line numbers:Special line numbers:.. means the current line numbermeans the current line number

^̂ means line number 1means line number 1

$$ means last linemeans last line

:.,$d:.,$d Delete from current line Delete from current line to the end of file.to the end of file.

Page 18: UNIX Intro.86 10. vi  vi is the standard UNIX text editor v Contents 1.Why use vi ? 2. vi Basics 3.Moving Around 4.Inserting Text.

UNIX Intro. 18

6. Cut & Paste6. Cut & Paste

6.1.6.1. Cut & Paste MeaningCut & Paste Meaning

6.2.6.2. Cut & Paste with Deleted TextCut & Paste with Deleted Text

6.3.6.3. Moving TextMoving Text

Page 19: UNIX Intro.86 10. vi  vi is the standard UNIX text editor v Contents 1.Why use vi ? 2. vi Basics 3.Moving Around 4.Inserting Text.

UNIX Intro. 19

6.1. Cut & Paste Meaning6.1. Cut & Paste Meaning

CutCut commands remove text from the commands remove text from the screen, and store it in a bufferscreen, and store it in a buffer

PastePaste commands copy text from the commands copy text from the buffer to the screenbuffer to the screen

Page 20: UNIX Intro.86 10. vi  vi is the standard UNIX text editor v Contents 1.Why use vi ? 2. vi Basics 3.Moving Around 4.Inserting Text.

UNIX Intro. 20

6.2. Cut & Paste with Deleted 6.2. Cut & Paste with Deleted TextText

d or dd or D d or dd or D delete from screen and delete from screen and store text in a bufferstore text in a buffer

move cursor to new locationmove cursor to new location

pp paste contents of buffer paste contents of buffer to right of cursor posnto right of cursor posn

Page 21: UNIX Intro.86 10. vi  vi is the standard UNIX text editor v Contents 1.Why use vi ? 2. vi Basics 3.Moving Around 4.Inserting Text.

UNIX Intro. 21

6.3. Moving Text6.3. Moving Text Cut and Paste with moveCut and Paste with move

:i,j:i,jmmkk move lines i through j to move lines i through j to start after line kstart after line k

:3,8m10:3,8m10 move lines 3 to 8 to start after line 10move lines 3 to 8 to start after line 10

:20m.:20m. move line 20 to after the current linemove line 20 to after the current line

:1,.m$:1,.m$ move lines 1 through current line move lines 1 through current line to the bottomto the bottom

Page 22: UNIX Intro.86 10. vi  vi is the standard UNIX text editor v Contents 1.Why use vi ? 2. vi Basics 3.Moving Around 4.Inserting Text.

UNIX Intro. 22

7. File-related Commands7. File-related Commands

:w file:w file writes writes vivi contents to contents to newnew file file

:w >> file:w >> fileappends to fileappends to file

:w! file:w! file writes over filewrites over file

:w!:w! writes over input filewrites over input file

:r file:r file read in file; places it starting read in file; places it starting at current cursor positionat current cursor position

Page 23: UNIX Intro.86 10. vi  vi is the standard UNIX text editor v Contents 1.Why use vi ? 2. vi Basics 3.Moving Around 4.Inserting Text.

UNIX Intro. 23

8. Text Substitution8. Text Substitution

:s/old/new/g:s/old/new/g replace every replace every occurrence of occurrence of oldold by by newnew. . Dangerous!Dangerous!

:s/Hat/Haad/g:s/Hat/Haad/g

Page 24: UNIX Intro.86 10. vi  vi is the standard UNIX text editor v Contents 1.Why use vi ? 2. vi Basics 3.Moving Around 4.Inserting Text.

UNIX Intro. 24

:i,js/old/new/g:i,js/old/new/g replace every replace every occurrence of old by occurrence of old by new between lines i and jnew between lines i and j

:2,200s/Andy/Andrew/g:2,200s/Andy/Andrew/g

:1,.s/fc/function/g:1,.s/fc/function/g from line 1 to from line 1 to currentcurrent

:23,$s/pd/procedure/g:23,$s/pd/procedure/g from line 23 from line 23 to endto end

Page 25: UNIX Intro.86 10. vi  vi is the standard UNIX text editor v Contents 1.Why use vi ? 2. vi Basics 3.Moving Around 4.Inserting Text.

UNIX Intro. 25

9. Recovering after a Crash9. Recovering after a Crash

vi -rvi -r List files that can be recoveredList files that can be recovered

vi -r filevi -r file Recover Recover filefile. . You should make a backup of You should make a backup of filefile first:first:

cp file file.bakcp file file.bak