Engineering Network Services - CSU

Engineering Network Services
 

Unix Information

Vi Reference

Introduction
Vi ("vee eye") is a powerful, full-screen editor for use on UNIX systems.

There are various ways to invoke vi: vi options name1 name2 ... edits (or creates if it does not exist) the named file. If more than one file is named, they will be edited in order. One of the options: +command enters the editor after executing the command. The default command is 'G' so that vi+100 file starts editing at line 100. 

The Display Screen
The display on the screen has some characters that do not exist in your actual file. For example, '~' stands for lines past the current end of file. When you insert text these symbols go away. The other character that is shown on the screen and not in your file is the '@'. This stands for a line that has been removed from your file (usually by deleting). 

The VI Modes
There are three modes in which you might find yourself while editing in vi.

  1. Command mode - This is the first mode you enter when you invoke vi. In this mode you can execute all of the commands used to modify text listed below. It is also in this mode where you can move the cursor around in the text you have entered. Most vi commands may be prefixed with a number in order to specify how many times the command is performed.
    Example: if you want to go to line 102 type: 102G, or if you want to delete six words type: 6dw. One way to save the file and exit vi is to enter :wq in this mode.
  2. Insert mode - this is entered by commands such as i, a and o. In insert mode you may enter new text into your file where the cursor is pointing. To get back to the command mode from the insert mode, hit the escape <esc> key. The escape key is also used to clear part of a command you no longer want to execute.
  3. Command line mode - this is entered by using the : (colon), / (forward slash), or the ? (question mark) commands. These commands are listed below and cause the cursor to move to the bottom of the screen. After typing in the command you want, hit escape or <return> to execute the command or ^C (control-C) to ignore the command. 

Cursor Movement Commands
These move the cursor in the command mode:

h move left: one character
j move down one line
k move up one line
l move right one character
^F move forward one screen
^B move backward one screen
^U scroll up half a screen
^D scroll down half a screen
G go to line (the end of the file if no number prefix)
//pattern find next occurrence of "pattern"
?pattern find previous occurrence of "pattern"
n repeat the last '/' or '?'
N reverse direction and repeat last '/' or '?'
H go to top line of screen (home)
L go to last line of screen
M go to middle line of screen
+ or 'return' go to the first character of next line
- go to the first character of previous line
0 (zero) go to beginning of current line
$ go to end of current line
^ go to first non-blank character of current line
'number'| (as in 3|) go to column number
w go to next word. If 'W' go to next word surrounded by blanks.
b go back one word. If 'B' go back to word surrounded by blanks
e go to end of current word. If 'E' go to end of current word before blank
) go to beginning of next sentence
( go back a sentence
} go forward a paragraph
{ go back a paragraph
% go to matching bracket
m['a'..'z'] mark current position (for use with the following command)
['a'..'z'] go to marked position
   

Inserting Information
   Ways to enter the insert mode:

i start inserting at the current cursor position
I start at the beginning of the current line
a start one after the current position
A start at the end of the current line
o open a line below and start inserting
O open a line above and start inserting
   

Insert Mode Commands

^H deletes the last character
^W deletes the last word
^D moves back over auto-indentation
<Esc> (the escape key) returns you to command mode
   

Editing Commands
In the command mode there are line and character manipulation commands as follows:

rx replace current character with x
R replace multiple characters starting at cursor
x delete current character
X delete character before current
   

Text Commands

cc blank out current line and go into input mode
C> change text from cursor to end of the line
dd delete current line
D delete from cursor to the end of the line
s replace a character with new text
S same as 'cc' above, but does not blank out current line
J join next line to the end of the current line
>> shift lines right
<< shift lines left
   

Screen Adjusting Commands

^L clears and redraws the screen removing all @ lines
^R retypes the screen removing all @ lines
^G shows the name, current and total number of lines in this file
z redraws the screen and places the current line at the top of the screen. z- places the current line at the bottom and z. places the current line at the center
^E scroll window down one line
^Y scroll window up one line
   

The Undo and Repeat
One of the nicest features of the vi is the ability to undo the last command you performed. So, for example, if you delete something that you really didn't want to, just type the letter u and the deleted material returns. The 'u' works with all commands and can be undone itself which returns to the condition before the first 'u'.  Another nice feature is the ability to repeat the last command. All that it takes is to type a . (period). This repeats the last command you executed at the current cursor position. 


 
layout image
layout image

This document last modified Monday January 25, 2016


Engineering Network Services home page link College of Engineering home page link