15
Basic VI Tran, Van Hoai Faculty of Computer Science and Engineering HCMC Uni. of Technology [email protected]

Basic VI

Embed Size (px)

DESCRIPTION

Basic VI. Tran, Van Hoai Faculty of Computer Science and Engineering HCMC Uni. of Technology [email protected]. vi - always available. Default editor in UNIX come along with all UNIXs Original name is Visual Editor Full screen (on terminal) - PowerPoint PPT Presentation

Citation preview

Page 1: Basic VI

Basic VI

Tran, Van Hoai

Faculty of Computer Science and Engineering HCMC Uni. of Technology

[email protected]

Page 2: Basic VI

Tran, Van Hoai2010

Ba

sic

VI

vi - always available

Default editor in UNIXcome along with all UNIXs

Original name is Visual EditorFull screen (on terminal)Some new variants with advanced operating

commands Basic commands implemented in all

variants of vi

Page 3: Basic VI

Tran, Van Hoai2010

Ba

sic

VI

Operation modes

2 modescommand: receive actions on edited file insert: insert text into file

<ESC> to exit insert mode

UNIX and vi are case-sensitive

Page 4: Basic VI

Tran, Van Hoai2010

Ba

sic

VI

Get in & out

* vi filename open file for editing

* :x<Return> write modified content and quit

:wq as above

:q just quit

* :q!<Return> quit without writing

Page 5: Basic VI

Tran, Van Hoai2010

Ba

sic

VI

Moving the cursor

cannot use mouse designed for Qwerty keyboard, can use

non-arrow keys

* j,k,h,l down, up, left, right

0, $ beginning, end of current line

:0 first line of file

:$ last line of file

:n<Return> to line n

Page 6: Basic VI

Tran, Van Hoai2010

Ba

sic

VI

Undo

* u undo last action

toggle the last action cannot go more than one step

Page 7: Basic VI

Tran, Van Hoai2010

Ba

sic

VI

Insert and add text

put vi into insert mode Press <ESC> to exit insert mode

* i,a insert text before/after cursor, until <Esc> hit

A append text to end of current line, until <Esc> hit

Page 8: Basic VI

Tran, Van Hoai2010

Ba

sic

VI

delete text

work in command mode

* x delete single character under cursor

* dd delete entire current line

D delete the remainder of the line, starting with current cursor position

Page 9: Basic VI

Tran, Van Hoai2010

Ba

sic

VI

cut and paste

work in command mode

yy copy the current line into buffer

Nyy copy next N lines

p paste the buffer after the current line

Page 10: Basic VI

Tran, Van Hoai2010

Ba

sic

VI

search text

work in command mode

/string search forward for occurrence of string in text

?string search backward for occurrence of string in text

:.= returns line number of current line at bottom of screen

Page 11: Basic VI

Tran, Van Hoai2010

Ba

sic

VI

save and read files

:r filename<Return>

read file named filename and insert after current line(the line with cursor)

:w<Return> write current contents to file named in original vi call

:w newfile<Return>

write current contents to a new file named newfile

:12,35w smallfile<Return>

write the contents of the lines numbered 12 through 35 to a new file named smallfile

:w! prevfile<Return>

write current contents over a pre-existing file named prevfile

Page 12: Basic VI

Tran, Van Hoai2010

Ba

sic

VI

Replacing patterns (1)

Character Pattern

\ turn off special meaning

\n reuse the text matched by the nth subpattern previously saved by \( and \). Numbered from 1 to 9

& text match search pattern

~, % reuse previous replacement pattern

\u convert first character of replacement pattern to uppercase

\U convert entire replacement pattern to uppercase

\l, \L same; to lowercase

\e turn off previous \u or \l

\E turn off previous \U or \L

Page 13: Basic VI

Tran, Van Hoai2010

Ba

sic

VI

Example (6)

Command Result

s/.*/( & )/ add space and parentheses

s/.*/mv & &.old/ ?

/^$/d delete blank lines (vi, g/^$/d for all lines)

%s/ */ /g turn one or more spaces into one space

%s/.*/\L&/ lowercase entire file

%s/yes/No/g replace yes to No

%s/Yes/~/g replace yes to No (previous replacement)

s/\(F\)\(ORTRAN\)/\1\L\2/g

FORTRAN to Fortran

Page 14: Basic VI

Tran, Van Hoai2010

Ba

sic

VI

Applications

Pattern Matched text

grab a specific HTML tag

[0-9]\{1,3\}\. ???? IP address

Email address

Valid dates (day-month-year)

WeWe, does not match Wee

<TAG\b[^>]*>\(.*?\)</TAG>

[A-Z0-9._%-]+@[A-Z0-9.-]+\.[A-Z]{2,4}

Page 15: Basic VI

Tran, Van Hoai2010

Ba

sic

VI

text processing utilities

is NEXT