Vi Editor

VI is a full screen text editor.

Ways to start the editor

% vi filename
% vi -R filename
% view filename
% vi -r filename
% vi

Ways to save

:w
:w filename
:w! filename
:w >> filename
:address w filename address can be added to any of the above in the same way

 

Addressing examples

72 just line 72 :72w this
5,10 lines 5 thru 10 inclusive :5,10w that
1,. lines 1 thru current inclusive :1,.w top
.,72 lines current thru 72 inclusive :.,72w middle
.,$ lines current thru end inclusive :.,$w bottom
1,$ lines 1 thru end inclusive (default for w) :1,$w all :w all
.,.+9 lines current thru 10 after current :.,. +9w ten.lines
^G or :f displays current file information and

current line number

Multiplier used with other commands

h, j, k, l move the cursor one space in that direction

5h moves the cursor 5 to the left almost the same as hhhhh

3j moves the cursor down 3 lines almost the same as jjj

x deletes the one character the cursor is currently on

5x deletes that one character and the 4 that follow it almost the same as xxxxx

dd deletes the one current line

D deletes to the end of the current line can't modify with the use of a multiplier

Scopes in larger units used for movement, deletion or other

w,W beginning of next word
b,B beginning of current word or previous word
e, E end of current word or next
(, ) beginning and end of sentence
{, } beginning and end of paragraph
^, $ beginning and end of line
H, L beginning and end of screen

 

Examples:

dW deletes to the beginning of the next space delimited word
{ moves the cursor to the beginning of the current paragraph if you are not
de deletes to the end of the current word if you are not already there only

changing using scope

cW Allows you to remove to the beginning of the next space delimited word and then replace it until you press <esc>
c) allows you to remove to the end of the current sentence and then replace it until you press <esc>
cc allows you to remove the current line and then replace it until you press the <esc>

 

Movement only commands

:$ move to last line in buffer
:# move to line number given
^D, ^U down or up 1/2 screen
^B, ^F backward or forward 1 whole screen

Yanking and putting also using the scopes from above yank puts a copy of text "yanked" into a pasting buffer for further use

It does not modify the current work buffer

yW will put a copy of the text to the beginning of the next space delimited word into the pasting buffer
y} will put a copy of all text between the current point of the buffer and the end of the current paragraph into the pasting buffer
yy will put a copy of the current line into the pasting buffer

every time a delete or yank is done, the contents of the general pasting buffer changes

P,p will paste from the pasting buffer before or after the text currently in the work buffer

Named buffers

named with a " (quote) followed by a lower case alphabetic character and then can be used with either delete or yank

26 of them plus the general purpose pasting buffer only maintained within the current vi session (once you quit they are gone)

"fdd deletes the current line from the work buffer and places a copy of the line deleted into the f pasting buffer in addition to the general purpose pasting buffer
"ky} does not change the work buffer, but puts a copy of the text to the end of the current paragraph into the k pasting buffer in addition to the general purpose pasting buffer
"mp puts the text from the m pasting buffer into the work buffer after the cursor
"y6yy yanks the current line and the 5 that follow it and puts all of the 6 lines of text into the y pasting buffer in addition to the general pasting buffer

Searching in vi

/string command to search forward from point of cursor
?string command to search backward from point of cursor

 

Meta-characters:

\< beginning of word
\> end of word
^ beginning of line
$ end of line
. wildcard for 1 character
[ ] to select class of characters
* multiplies character before it
/b.*o b followed by any number of characters and then an o
/\<of\> the whole word of will not match to off
/[srd]$ matches to s, r or d at the end of a line
n find next in the same direction
N find next in the opposite direction

 

Substituting in vi

:[address]s/srchstring/replstring[/g]

default address is the one current line

default repetition is to do only the first on a line need to add the /g for all on each line search string may have meta-characters

Replacement string may not

:s/a/e replaces the first a on the current line with an e

:1,$s/a/e replaces each first a on a line with an e

:1,$s/a/e/g replaces all a's in the buffer with e's

Selective Changes

use regular / to find the first occurrance of the string make the change if you want to

use the n to find the next

use the . to repeat the change

. repeats the last command given in vi that made a change to the buffer

Editing another file

replaces the current work in the buffer does not destroy named buffers

:e file1 replaces contents of the current buffer (assuming no change) with the contents of file1
:e! file1 replaces contents of the current buffer (even if there was a change)
:e! a major undo --- makes the buffer match the file it is associated with

Adding another file to the buffer

:[address]r [filename]

adds the contents of the named file or the file associated with the buffer to the current buffer does not destroy named buffers current position of the cursor

:3r file1 brings in the contents of file1 at line 3

:r brings in the file associate with the buffer at the current line of the buffer

Shell access

:sh starts an interactive shell - a new layer ^D or exit to return
:! command allows you to do one shell command without changing the buffer
:!ls displays the listing of the directory
!! command allows you to bring the output of one shell command into the current buffer
!!who brings in the list of current users

Markers in the work buffer

26 markers available within each work buffer (associated with that session) named for the 26 lower case letters of the alphabet

  1. move to one end of the block
  2. place the marker by using the command m and the name of the marker
  3. ma mb mc .... my mz

  4. from anywhere else in the work buffer, refer to that place or the area between the current position and that place by using an ' apostrophe followed by a letter

examples

- put a marker into the work buffer by using the command mf

then move somewhere else in the file and get back to that position

by using the 'f

- put a marker into the work buffer by using the command mm

then move to somewhere else in the work buffer and delete

the text between the line of the current position of the cursor

and the line indicated by the marker (inclusive) by d'm

-put a marker into the work buffer by using the command md

-the text into a named buffer by "ay'd

-put the marker into the work buffer by using the command mq

then move to somewhere else in the work buffer and use

a shell utility to sort the lines between by using the !'qsort

bill

jan

amy

tom

fred

SET UP OF THE VI EDITOR

 

3 ways to personalize operation

1) have a file named .exrc in current or home directory

2) set the options as a last line command

3) have a shell variable named EXINIT which contains setup directions

every variable has an opposite rather than turning a variable on and off so the only command needed is set

set number displays line numbers in the current buffer
set nonumber does not display line numbers in the current buffer
magic/nomagic Meta characters are "magic" or not
showmode/noshowmode not on ULTRIX displays mode in bottom left corner
flash/noflash Screen flashes rather than beep
ignorecase/noignorecase case sensitivity in search
list/nolist Displays some hidden characters such as tab and eoln
wrapscan/nowrapsc n do searches continue from top at bottom when reach the other end
tabstop=value specifies size of tab
autoindent/noautoind nt regularly spaced tabstops
shiftwidth = value tells how far apart those tab stops are to be
wrapmargin = value tells to do auto wrapping of the line actually does put in the hard return
 

Back Home Up Next