University of Rochester
   

Home
Web Help
Job Opportunities

IT Center
Class Tech
Computer Sales
Ed Tech
Site Licensing
Web Services


University IT Home

IT Notices

Policies

About Us

PMR

Leaders

Contact Us

vi editor

The vi (pronounced "vee eye") is available on all UNIX systems: other editors are not. Being able to use vi ensures that you will always have an editor available to you.

Starting vi and Opening Files

To start using vi, enter the command:

mail% vi filename

The filename can be the name of an existing file or the name of the file that you want to create.  
Examples:
To open an existing file for editing:

mail% vi intro

This opens the file "intro" for editing. This file already exists in the current directory.

To create a file to edit:

mail% vi part2

This command starts vi with a new file named "part2." This file does not exist in the current directory until it is written by using the write command.

To simply start vi:

mail% vi

This command places you in vi. You can then enter and edit text and write it to a file.

Text Buffer

While using vi to edit an existing file, you are actually working on a copy of the file that is held in a temporary buffer. If you invoked vi with a new filename, or no file name, the contents of the file only exist in this buffer. Saving a file writes the contents of this buffer to the permanent file and replace the contents of the file. You can write the buffer to a new file or to some other file. You can also decide not to write the contents of the buffer, and leave your original file unchanged.

Insert mode

This is the mode you use to type (insert) text into a buffer. There are several commands that you can use to enter this mode. List of insert commands: To do this ... Command Insert text after the cursor a Insert text before the cursor i Append text at the end of the current line A Insert text at the start of the current line I Open a new line below the current line o Open a new line above the current line O

Command mode

This is the mode you are in, whenever you begin to use vi. In this mode commands are used to move around and edit text objects such as words, sentences, and paragraphs. While not entering text you use vi in command mode and enter editing commands consisting of one or more characters. The syntax of an editing command is: operator object The operator is a character - such as d for delete - that describes the action to be carried out on the object: a word, sentence, paragraph or section. To return to command mode from insert mode press the ESC key. If you are already in command mode the terminal will beep at you (or make some similar sound).

Changing text around

There are several ways in which you can change the structure and the content of the file you are editing.

Repeat parts of the text:

You can re-use a piece of text in another part of the file by yanking it into a temporary buffer and then putting the contents of this buffer back into the file. The command y yanks the text into the temporary buffer. Combined with other commands that are used to move around text objects, the yank command can be used to pull any part of a file into the buffer. The command p pastes the contents of the temporary buffer back into the file immediately after the cursor. Examples of repeating parts of the text :

To yank the current sentence into the temporary buffer: y)
To yank the text from the present cursor position to the beginning of the current paragraph into the temporary buffer: y{
To place the contents of the temporary buffer after the current cursor position: p

Insert the contents of another file:

To insert the contents of another file use the command: :r filename
This inserts the file immediately after the current line.

Examples of inserting the contents of another file:

To insert a file immediately after the line containing the cursor: :r examples.txt
This inserts the contents of the file examples.txt immediately after the current line.

To insert the contents of another file after a specific line in the current file: 23:r toc.txt
This inserts the contents of the file toc.txt after line 23 of the current file. Line 24 is empty; the contents of the inserted file begin at line 25.

Cut and paste text:

Moving pieces of text around within a file is referred to as cut and paste. Doing this in vi is a three stage process:

1. Cut the text you want to move with one of the commands used to delete text (see below).
2. Move the cursor to where you want to paste the text into the file using one of the commands for moving around the text.
3. Paste the text into the file with the command: p

Moving around the text

There are a range of easy to use commands that provide a great degree of flexibility in how you move around the contents of the file you are editing. Many of these commands can be combined with a numerical value to extend their functionality.

Move the cursor along a line:

Press the ESC key to enter command mode before using these commands.

To move to..... ..............................Do this ...

next character..... ...............................l
previous character ...............................h
next word........................................ w
next n words .....................................nw
previous word ...................................b
previous n words ................................nb
end of current word............................. e
start of current line ..............................0(zero)
end of current line ...............................$

Move the cursor between lines:

Press the ESC key to enter command mode before using these commands.

To move........................................ Do this ...

next line down (same column).................... j
start of next line down .............................+
previous line (same column) ......................k
start of previous line ...............................-

Move between text blocks:

Press the ESC key to enter command mode before using these commands.

To move to......................................Do this ...

beginning of next sentence......................... )
beginning of previous sentence ....................(
beginning of next paragraph .......................}
beginning of previous paragraph ..................{

A full stop followed by two or more spaces is treated as the end of a sentence. All text to the next empty line is treated as belonging to the current paragraph.

Moving over the file:

Press the ESC key to enter command mode before using these commands.

To move to....................................... Do this ...

top of the file .........................................1G
line n of the file...................................... nG
bottom of the file .....................................G

Moving around the screen:

These are commands for moving around the screen you are viewing and for moving around the file using the screen view of the file. Press the ESC key to enter command mode before using these commands.

To move to....................................... Do this ...

top of the screen ......................................H
middle of the screen................................. M
last line of the screen................................. L
last line but n ..........................................Ln
forward one screen.................................. ^F
backward one screen ................................^B
forward half screen (down)........................ ^D
backward half screen (up).......................... ^U

Deleting Text:

Press the ESC key to enter command mode before using these commands.

To delete ............................................Do this

Characters:
current character .........................................x
previous character .......................................dh
Words and Lines:
current word .............................................dw
previous word........................................... db
entire line................................................ .dd
to end of line .............................................d$ or D
to start of line ............................................d0 (zero)
next n lines............................................... ndd
Sentences & Paragraphs:
to end of sentence ........................................d)
to beginning of sentence ................................d(
to end of paragraph...................................... d}
to beginning of paragraph ..............................d{

Undeleting text:

To get back a word or line that you have just deleted enter the command: u or p

Searching for text:

Press the ESC key to enter command mode before using these commands.

To search ................................................Do this ...

forward for a pattern ....................................../pattern <Return>
backward for a pattern ....................................?pattern <Return>
repeat previous search...................................... n
repeat previous search in ...................................N
reverse direction

Replacing text:

Press the ESC key to enter command mode before using these commands.

To replace .................................................Do this ...

pattern1 with pattern2 ...............................:s/pattern1/pattern2
on the same line

every occurrence of pattern1....................... :g/pattern1/s//pattern2/g
with pattern2

Tip for replacing text... Use global replacement in its interactive mode to avoid making changes that you don't intend. To do this add the c option to the end of the global replacement command:

:g/pattern1/s//pattern2/gc

vi will display the first occurrence of pattern1. To replace it with pattern2 you must enter y (yes) and press <Return>. If you don't want to replace this pattern press <Return> to go to the next occurrence.

Examples of replacing text:

To replace one word in the current line with another: :s/that/which
This replaces the first instance of the word "that" in the current line with the word "which."

To replace every instance of one word in the current line with another: :s/destroy/delete/g
This replaces every occurrence of the word "destroy" with the word "delete" in the current line.

To replace every occurrence of a word throughout the file, with another: :g/insert/s//add/g
This replaces every occurrence of "insert" with "add."

To interactively replace every occurrence of a word throughout the file, with another: :g/in conjunction with/s//with/gc

option used in conjunction with the ls command

This displays the first line containing an occurrence of the pattern "in conjunction with," and identifies this occurrence by underlining each character with a ^ (caret). Entering y (yes) replaces this instance of "in conjunction with" by "with" and proceeds on to the next. Pressing <Return> leaves this instance unchanged and proceeds to the next.

Customizing vi:

You can change the way vi operates by changing the value of certain options which control specific parts of the vi environment.

Displaying vi option values:
To display the current value of all options enter the command: :set all
To display the value of those options whose values have been reset from their default enter the command: :set

Change an option value temporarily.
To change a the value of an option temporarily: :set option_name or: :set option_name=value
This sets the value of the option until you quit vi.

Examples of setting vi options temporarily:

To set the wrapmargin: :set wm=5
This sets the wrapmargin to five characters. In insert mode, text will automatically wrap to a new line when the cursor is five characters from the right edge of the screen.

To display line numbers: :set number
This causes vi to display a line number (starting at 1) at the start of every line of text. Change an option value permanently: To make a lasting change, create a file named .exrc, containing the set commands, in your home directory. The next time you use vi these options will take effect and will remain in force until you edit the .exrc file to change them.

Examples of setting vi options permanently:

To set a number of options place the set commands in the file .exrc.

set ic
set number
set sh=/usr/ucc/bin/tcsh
set wm=5

This sets vi to:

1. ignore the case of characters in searches
2. display line numbers
3. use the TC shell to execute UNIX commands
4. wrap text five characters from the right edge of the screen

Options can also be set using the environment variable EXINIT.

setenv EXINIT 'set ic number sh=/usr/local/bin/Tcsh wm=5'

For the C and TC shell user, this sets the same options as in the example above. If there is a .exrc file owned by you in your home directory or the current directory, vi will take its option values from this and not from the EXINIT environment variable.

Saving Files and Exiting vi

There are several ways in which you can end your editing session, leave vi and return to the shell. ·

Quit vi:
To quit the vi editor:

:q


If you have edited (changed) the file you will be prompted with the message:

No write since last change

You can either save the changes or quit without saving any of the changes you have made.

Saving changes
To quit vi and save the contents of the buffer to the file that vi is being used to edit:

ZZ or :wq

Without saving changes
To quit vi without saving the contents of the buffer to the file:

:q!

Saving to another file
To quit vi and save the contents of the buffer to a new file:

:w filename
:q

Viewing a File

You can look at a file without the risk of altering its contents by using vi in "read only" mode. To do this, use the command:

uhura% view filename

The message [Read only] is displayed at the bottom of the screen. You then use the same commands as you would in vi to move around, search for text and quit view.

UNIX shell commands

You can run UNIX commands and see their output without leaving vi. You can also insert the output of a UNIX command into the file you that are editing. To run a single UNIX command use the command:

:!UNIX_command

You can start a shell from within vi and use it as you would your usual UNIX environment, then exit the shell and return to vi. To start up a shell enter the command:

:sh

The type of shell that is started is determined by the $SHELL variable. You can specify that some other shell is to be started by setting the vi shell option. Return to using vi by entering the command exit or Ctrl-D. To insert the output from a UNIX command into a file, immediately after the cursor:

:r!command

For example, this facility would be very useful if you were using vi to document a UNIX command and you wanted to include examples of the output from this command.

Recovering a file after a system crash

Suppose the system fails while you are editing a file! You can recover the file when you next log on to the system again by using the -r option to the vi command. For example:

uhura% vi -r help.xdh

This restores the vi buffer for the file help.xdh to exactly the state it was in immediately before the system failed. At this time, you can continue to edit the file or save the contents of the buffer to the file.

 

 

 

 

       

Text | Directory | Index | Contact | Calendar | News | Giving

Last Modified: Tuesday, 10-Jul-2007 15:49:00 EDT