About VI Editor

Editor, vi tutorial vi Editor, Learn vi, About vi editor

 

 

 

 

 

vi is Found on Nearly Every Unix Computer
vi is the standard Unix editor
vi is Powerful and Fast
Your terminal displays a section of the file you are editing
vi can do anything you want
You don’t need to remove your fingers from the standard typing keys-the keys themselves give commands to vi
vi Stays Out of Your Way
vi has no menus
vi commands are short Starting vi Open a file with vi.
Type: vi myfile.txt If myfile.txt does not exist, a screen will appear with just a cursor at the top followed by tildes(~) in the first column.If myfile.txt does exist, the first few line of the file will appear.The status line at the bottom of your screen shows error messages and provides information and feedback, including the nameof the file.

Modes:

Command Mode:

  • Command mode is the mode you are in when you start (default mode)
  • Command mode is the mode in which commands are given to move around in the file, to make changes, and to leave the file
  • Commands are case sensitive: j not the same as J
  • Most commands do not appear on the screen as you type them. Some commands will appear on the last line: : / ?

Insert (or Text) Mode:

  • The mode in which text is created. (You must press at the end of each line unless you’ve set wrap margin.)
  • There is more than one way to get into insert mode but only one way to leave: return to command mode by pressing
  • When in doubt about which mode you are in, press Basic Cursor Movement
    kUp one line
    jDown one line
    hLeft one character
    lRight one character (or use )
    wRight one word
    bLeft one word

NOTE: Many vi commands can take a leading count (e. g., 6k, 7e).

Entering, Deleting, and Changing Text

iEnter text entry mode
xDelete a character
ddDelete a line
rReplace a character
ROverwrite text, press to end

Setting Basic Options in vi:

Displaying Line Numbers

:set nuDisplay line numbers
:set nonuHide line numbers

Setting Right Margin

:set wm=numberSet Wrap Margin number of spaces from right edge of screen
:set wm=10Set Wrap Margin 10 spaces from right edge of screen
:set wm=0Turn off Wrap Margin

Exiting vi

To exit you must be in command mode-press if you are not in command mode

You must press after commands that begin with a : (colon)

ZZWrite (if there were changes), then quit
:wqWrite, then quit
:qQuit (will only work if file has not been changed)
:q!Quit without saving changes to file

Basics Summary:

A Basic vi Session

To enter vi, type: vi filename
To enter insert mode, type: i
Type in the text: This is easy.
To leave insert mode and return to command mode, press:
In command mode, save changes and exit vi by typing: :wq
You are back at the Unix prompt.

Intermediate VI:

More On Cursor Movement

eMove to end of current word
$Move to end of current line
^Move to beginning of current line
+Move to beginning of next line
Move to beginning of previous line
GGo to last line of the file
:nGo to line with this number (:10 goes to line 10)
dScroll down one-half screen
uScroll up one-half screen
fScroll forward one full screen
bScroll backward one full screen
)Move to the next sentence
(Move to the previous sentence
}Move to the next paragraph
{Move to the previous paragraph
HMove to the top line of the screen
MMove to the middle line of the screen
LMove to the last line of the screen
%Move to matching bracket: ( { [ ] } )

Entering Text Mode

iInsert text before current character
aAppend text after current character
IBegin text insertion at the beginning of a line
AAppend text at end of a line
oOpen a new line below current line
OOpen a new line above current line

Commands and Objects

FormatExample
operator number objectc2w
number operator object2cw

Operators

cchange
ddelete
yyank

Objects and Locations

wone word forward
bone word backward
eend of word
H, M, Ltop, middle, or bottom line on screen
), (next sentence, previous sentence
}, {next paragraph, previous paragraph
^, $beginning of line, end of line
/pattern/forward to pattern

Replacing and Changing Text

rReplace only the character under the cursor.
(Note: using r you remain in command mode.)
RBeginning with the character under the cursor,
replace as many characters on this line as you
want. (You are in overtype mode until you
press
cwBeginning with the character under the cursor,
change a word to whatever you type. (You are
in insert mode until you press )
c$Beginning with the character under the cursor,
Cchange a line to whatever you type. (You are
in insert mode until you press )

Deleting Text

Delete a character
dwDelete an alphabetic word and the following space
(6dw deletes six words)
dWDelete a blank-delimited word and the following space
ddDelete a line (6dd deletes six lines)
d$Delete all characters to the end of the line.
d}Delete all characters to the end of the paragraph.
:5,30dDelete lines 5 through 30

Deleted text goes into a temporary buffer that is replaced each time you delete (or copy) more text. The current contents of

the buffer can be put back into your file.

Copying and Pasting Text

yyCopy (yank) the current line
6yyCopy (yank) six lines, beginning with the current line
ywCopy the current word
pPut the text after the cursor position
PPut the text before the cursor position

Copied text goes into a temporary buffer that is replaced each time you copy (or delete) more text. Only the current contents of the temporary buffer can be put back into your file. As a result, when you use copy (y), use the put (p) command immediately.A yank and put procedure using colon commands:

:5,10y Copy lines 5-10

Move cursor

:put Put after cursor

Other Useful Commands

.Repeat last command
n.Repeat last command n number of times
JJoin next line to current line
uUndo last single change
URestore current line
~Change letter’s case (capital to lower and vice versa)

Temporary Buffer

Deleted or copied text goes into a temporary unnamed buffer. The contents of the temporary buffer may be retrieved by using the p or P commands.

pPut words from temporary buffer after cursor or
put lines from temporary buffer below current line
PPut words from temporary buffer before cursor or
put lines from temporary buffer above current line

Lettered Buffers

There are 26 lettered buffers (a-z). Contents of a lettered buffer are saved until you copy or delete more characters into it, or until you quit your current vi session.

“ayyCopy (yank) a line into buffer a
“AyyAppends to buffer a
“a10yyCopies 10 lines into buffer a
“a10ddDeletes 10 lines of text into buffer a
“apPut contents of lettered buffer a below the current line

Both temporary and lettered buffers last only for the current vi session. Copying, Deleting, or Moving Text Using Line Numbers These commands start with a colon (:) and end with a or g shows the line number of the current line The basic form of colon commands is

:beginning_line, ending_line command destination

where destination is the line after which you want the text placed.

:5,10 co 105Copy lines 5-10 to the line after 105
:5,20 m $Move lines 5-20 to end of file
:7,300 dDelete lines 7-300 (to buffer)

Searching for Text

/textSearch forward (down) for text (text can include spaces
and characters with special meanings.)
?textSearch backward (up) for text
nRepeat last search in the same direction
NRepeat last search in the opposite direction
fcharSearch forward for a charcter on current line
FcharSearch backward for a character on current line
;Repeat last character search in the same direction
%Find matching ( ), { }, or [ ]

Substitutions

The simplest way to do substitutions over a range of lines, or throughout the file, is to use the s colon command. The basic form of this command is the following:

:n1,n2s/old/new/gc

n1is the beginning line
n2is the ending line number
smeans to substitute text matching the pattern (old) with text specified by(new)
g (global)is optional. It indicates you want to substitute
all occurrences on the indicated lines. If you use
g, the editor substitutes only the first occurrence
on the indicated lines.
c (confirm)is optional. It indicates you want to confirm
each substitution before vi completes it.
:%s/old/new/gSubstitutes old with new throughout the file
:.,$s/old/new/gSubstitutes old with new from the current
cursor position to the end of the file
:^,.s/old/new/gSubstitutes old with new from the beginning
of the file to the current cursor position
:&Repeats the last substitute (:s) command

Advanced VI Tutorial:

Writing to and Reading from Files

:w fileWrite current file to file
:w>>fileAppend current file to file
:5,10w fileWrite lines 5 through 10 to file
:5,10w>>fileAppend Lines 5 through 10 to file
:r fileRead a copy of file into current file
:!lsSee a list of files in your current directory

More About Options:
From Command Mode-within vi for the current file only

:set allDisplay all options
:setDisplay current settings of options
:set nooptionUnset option
:set aiSet Auto Indentation during text entry
:set icSet Ignore Case during searches
:set nuShow line Numbers
:set smShow Matching ( or { when ) or } is entered
:set wm=10Set Wrap Margin 10 spaces from right edge of screen

Customizing vi Sessions:
Options can be set four ways:

  • During a vi session
    • :set nu
  • In a .exrc file in your home directory.
    Sample contents of a .exrc file
  • set nu
  • set ai
  • set wm=10
  • In a .exrc file in a subdirectory.
  • By setting the EXINIT environmental variable.
    Example of setting the EXINIT environmental variable
  • setenv EXINIT “set nu ai ic”Order of Precedence
  • If a .exrc file exists in the current directory, vi reads it when beginning a session.
  • If no .exrc file exists in the current directory, vi checks the home directory for a .exrc file. If such a file exists,vi reads it when beginning a session.
  • If no .exrc file is found, vi uses its defaults.
  • Values set in the EXINIT environmental variable override any values set in a .exrc file. Creating a .exrc File At the system prompt, type: vi .exrcType the following commands, each on a separate line:
  • set ai
  • set ic
  • set nu
  • set wm=8Do not leave blank lines at the beginning or end of the .exrc file.When you are finished, type: Abbreviations & Mapping. Abbreviations are text strings that automatically expand into larger strings during insert mode.:ab UW University of WashingtonMapping defines a single key to execute a sequence of keystrokes when the single key is pressed in command mode. In thefollowing example,the @ key is mapped to replace the current word with “University of Washington”. The v allows youto enter the key into the command sequence.:map @ cwUniversity of Washington vMapping can also be used to call commands external to vi, such as sort or fmt. In the following example, the @ sign is mapped to the sort command, so that the current paragraph (indicated by the }) will be sorted. The v allows you to enter the key into the command sequence. The second completes the map command.:map @ !}sort vNote: You can also put abbreviation and mapping commands in your .exrc file.

Tips And Tricks:

  • Find the line that readseditor=Change it to readeditor=viWrite and quit the file. (ZZ or :wq)vi-ing More Than One FileYou can edit more than one file at a time with vi.

    From The Unix Shell Prompt

    vi file1 file2 vi two (or more) files at the same time

    From Command Mode

    :nMove to file2 from file1
    :rewRewind back to file1
    :e!Restore original file1 file2 (start all over)
    ZZSave and quit file. (Must be done for each file.)

    Moving the Left Margin

    When you print a file you may want the left margin moved to the right. This leaves room for a three-hole punch.

    :1,$>Move entire file 1 shift width (eight spaces)
    to the right
    :1,$<Move entire file eight spaces to the left
    :%s/^/ /gInsert any number of spaces at the
    beginning of each line in the entire file.
    Simply press the space bar the
    desired number of times.
    :20>>Moves next 20 lines over 1 shift width.

    Issuing Shell Commands From vi

    You can issue a single shell command while in the vi editor. For example, to list the files in your directory (ls), follow these steps:

    Press d to return to vi editing.

    Double Spacing a File

    Occasionally, you may want a double spaced version of your file for editing or review.

    :wWrite changes to your file (just in case).
    :!lsList contents of your current directory on the screen.Press to return to vi.You can issue many shell commands by temporarily leaving the vi editor.From Command Mode
    :wWrite changes to your file.
    :shReturn to the shell to enter a number of commands
    without leaving vi.
    :woriginal.backup Save a backup copy of the original file
    :%! sed GDouble space the entire file.
    :1,5! sed GDouble space the lines from 1-5.

Original Author: M.S.Verman

POSTS YOU MAY LIKE -:)

Share This Post

Shares