Skip to Main Content

How Docs

General

Use the UNIX Text Editor, vi

NOTE: This document refers to software or hardware that is not currently supported by the University of Wyoming Information Technology Client Support Services. Although the information is believed to be correct, be aware that since no support is available, this information may be out-of-date or possibly no longer valid.

Introduction

vi differs significantly from your familiar word processors. vi is technically not a word processor. vi is a text editor. Instead of using menus or special function or control keys, you use the regular keyboard keys to perform tasks such as editing, deleting, saving, etc. For example, if you want to delete a character, you type an x. To insert something into the file, you have to type an i first. After doing so, everything that you then type will be inserted into the file until you press the <Esc> key. While you are in insert mode you cannot move around in the file or use other editing commands. You must press the <Esc> key first. When you are not in insert mode, you can move the cursor using your arrow keys, or by typing the h, j, k, and l keys.

Creating a New File

To create a new file, enter the following at the shell prompt (replace filename with the name of the file you want to edit or create): frontier:˜> vi filename

After vi starts up, type an i to enter insert mode. The i will not appear on the screen. Then type the text of your file. In insert mode you can use the Tab key or <Ctrl>+t to indent, and <Ctrl>+d to unindent. You cannot use the arrow keys to move around or make corrections while you are entering your text, however, you can use the Backspace key to back up and retype words on the current line. If you press the <Esc> key to exit from insert mode you can then move around using the arrow keys or use any of the commands listed below. To get out of the vi editor and save the file, type a colon (:) to go to the command line at the bottom of the screen, type an x (for exit), and then press <Enter>.

Basic vi Commands

Note: Any number typed before a command will repeat that command the indicated number of times.

Key

Action

i  insert text before the cursor
a  append text after the cursor.
A append text after the end of the line.
x delete the character under the cursor. 5x deletes 5 characters.
dd delete a line (3dd deletes 3 lines.)
r replace the character under the cursor with next character you type.
R overwrite text (esc to quit overwriting.)
u undo the last change.
O open and input new lines, above current line (esc to get out.)
o open and input new lines, below current line (esc to get out).
:x save the file and exit from vi.
:q! quit without saving the file (changes will be lost).

Scrolling Commands

The following commands will allow you to scroll forward or backward through your file, but they cannot be used with d or y to delete or copy text.

Key

Action

<Ctrl>+f scroll forward a screen
<Ctrl>+u scroll backward a half screen
<Ctrl>+d scroll forward a half screen
<Ctrl>+b scroll backward a screen (-)
z followed by a comma (,) a period (.), or a dashrepositions the screen with the current line at the top, middle, or bottom of screen, respectively.

Motion Commands

Key

Action

w or W move to next word or after the next space
b or B move back a word or before the previous space
$ (dollar sign) move to end of line
0 move to beginning of line
_ (underscore) move to first character on the line (ignoring leading tabs and spaces)
G move to the last line of the file. 1G goes to line 1, 2G to line 2, etc. <Ctrl>+G display the current line number
fx move to move to the first x on the line (or whatever character)
;
(semicolon)
move to next occurrence on the line of the character named in the last f command
` `
(two backquotes)
move back to where you were before the last moving command
/ (slash) string move to the next occurrence of string in the file
?
(question mark)
string move backwards to the previous occurrence of the string in the file
n repeat last search in same direction
N repeat last search in opposite direction

Motion commands are useful for deleting or copying blocks of text. If you type a d followed by one of the above moving commands you will delete text up to the place you move to. For example, dG will delete text up to the end of the file. To put the text you last deleted back into the file, type a p, or a P (see Other Useful Commands below). To copy a block of text without deleting it (yank), type a y followed by a moving command, for example, yG.

Other Useful Commands

Key

Action

yy yank line (copy the line ), 4yy would yank (copy) 4 lines
p paste the last deleted or yanked text into the file at the cursor position
P paste the last deleted or yanked text into the file (ahead of cursor)
dd delete line. A d followed by any moving command in the list above will delete up to that point. For example, dw would delete the next word.
cw change word (type the replacement word or words and then press <esc>)
J join two lines together (place cursor on the first of the two lines)
U undo the changes you made to the current line
. (period) redo the last non-motion command
>> shift line to the right.
<< shift to the left

Global Replacement

To change all occurrences of a string of text to another string you can move down to the command line by typing a colon (:) and then use the substitute command (s). For example, to change all occurrences of the string "old text" to the string "new text" on lines 1 through 17 you would type: :1,17s/old text/new text/g

The /g at then end of the command tells vi to replace every occurrence it finds on a line, instead of just the first one. You can also use a $ to indicate the last line of the file (:1,$s/old text/new text/g). Note: The percent sign (%) can be used as a substitute for 1,$ in all commands.

Exit and Other Command Line ("colon") Commands

Key

Action

:x or :wq write (save) the file and quit (see note below)
ZZ write (save) only if changes have been made and quit (no colon necessary)
:w or :w file write (save) the file, but do not quit
:3,20w file write lines 3 through 20 onto the file named file
:r file read the contents of the file named file into the current file
:q! quit without saving

Note: Be careful not to use the colon uppercase X command (:X). This will prompt you for a key for encrypting your file. If you enter a key then your file will be encrypted when you save it and you will need to remember what the key was in order to work with your file again.


 

Reviewed: 0706 By: CD

Additional help with the installation and configuration of
UW-supported software is available:
Faculty/Staff
Contact the IT Help Desk at 766-HELP (4357), option 1
Email UserHelp@uwyo.edu
Contact your IT user consultant
(www.uwyo.edu/infotech/services/helpdesk/uc/)

Students
Email ASU-IT@uwyo.edu
Contact the IT Help Desk at 766-HELP (4357), option 1
Come to the student computer lab in the lobby of the
Information Technology Center.

Footer Navigation

University of Wyoming
 
1000 E. University Ave. Laramie, WY 82071 // UW Operators (307) 766-1121 // Contact Us