VI Reference


Go Home | Go Back to Tips Page



What is VI?

vi is the most versatile text editor for the UNIX operatins systems. VI is written by Bill Joy, yes, the guy who wrote the article on Wired Magazine discussing the terrible future of technology and mankind. By the way, he is chief scientist in Sun Microsystem.

FYI, vi stands for Visual Editor, which means you can see what you are doing with the text. Are you old enough to remember the edlin command in DOS? That's an example of non-visual text editor.

Go to Top


VI modes

When the vi was born, there were no GUIs and mice for the UNIX. You cannot use a mouse to choose a menu command, nor you can place the caret with a mouse. Then, how do you intereact with the computer?

Of course, the only way is throught the old faithful keyboard. However, there comes the problem. You must use the keyboard to issue commands to the computer, you must also use the keyboard to input the text you want to edit. How can your computer discern what you want to do? If you hit the "b" key, how your computer knows whether you want to input a letter "b" or issue a command represented by the "b" key?

Here comes the modes: under input mode, the keystrokes you hit are interpreted as text input; under the command mode, the keystrokes are interpreted as commands.

Go to Top


VI common commands

Alphabetical vi Quick Reference

keystroke(s) meaning of the command
# (a number) following command n times
: go to ex-mode
) next sentence
( previous sentence
} next paragraph
{ previous paragraph
]] next section
[[ previous section
0 beginning of line
$ end of line
^ first non-whitespace character
+ first character of next line
- first character of previous line
(spacebar) next character
(return key) next line
/ search forward
? search backward
, reverse direction of last f, F, t, or T
; repeat last f, F, t, or T
. repeat last command
` go to mard
' go to beginning of line with mark
`` return to previous mark or location before search
'' go to start of line of previous mark or location before search
~ switch case of current character
" store in register
@ execute command in register
! send next to command, replace output (eg !}fmt passes the current paragraph to the command fmt, and replace the output with whatever fmt return. )
!! send line to command
>> shift paragraph one shiftwidth to the right
<< shift paragraph one shiftwidth to the left
>% shift until matching (, [, or { to the right
<% shift until matching ), ], or } to the left
a append after the current location
A append at the end of the line
^a unused
b beginning of previous word
B beginning of previous word, ignore punctuation
^b scroll back one screen
c change until ...
C change to end of line
^c ends insert mode, unused in command mode (if defined as interrupt)
d delete until ...
D delete to end of line
^d scroll down half a window, moves to previous shiftwidth in insert mode
e end of word
E end of word, ignore punctuation
^e scroll screen down one line
f find ...
F find backward
^f scroll forward one screen
g unused
G ... Goto [default to end of file]
^g show status line
h left
H first line on screen
^h backspace in insert mode, left in command mode
i insert before current location
I insert before first non-whitespace character on line
^i tab in insert mode, unused in command mode
j down
J join next line with current line
^j down in command mode, creat newline in insert mode
k up
K unused
^k unused
l right
L last line on screen
^l redraw screen
m mard position into register
M middle of screen
^m carriage return
n repeat last search
N repeat last search, reverse direction
^n down in command mode
o open a line below the current line
O open a line obove the current line
^o unused
p put below current line
P put above current line
^p up in command mode
q unused
Q quit and run ex
^q unused
r replace current character
R replace characters until insert mode is left
^r redraw screen in command mode
s substitute
S substitute entire line
^s unused
t to ...
T backward to ...
^t moves to next shiftwidth
u undo last change
U undo changes to current line
^u scroll up half a windows
v unused
V unused
^v unused in command mode, quotes next character in insert mode
w beginning of next word
W beginning of next word, ignore punctuation
^w unused in command mode, back to beginning of previous word in insert mode
x delete current character
X delete previous character
^x unused
y yank
Y yank current line
^y scroll screen up one line
z reposition screen around line (return to top of screen, . to middle, - to bottom)
ZZ write (only if changes have been made) and quite
^z unused

Go to Top


Command mode input options (: commands)

keystrokes meaning of the command
:r read into current text
:r! read output from command into current text
:nr read in at line number
:! run command, then return
:sh goto shell
:so read and execute command from
:x write (only if changes have been made) and quite
:wq write and quit
:l1, l2w write between lines l1 to l2 to. If is not specified, assume entire file (making it :w)
:w >> append to. May use line numbers
:w! overwrite current file
:q quit
:q! quit, forget changes
:e edit without leaving vi
:e! forget changes since last write
:n edit next file
:e# edit alternate file (if :e is used, alternate is the original file
:args show files to be edited
:rew rewind list of files to top
:map m n create a macro (make m do n)
:map! m n create an insert mode macro (make m do n)
:unmap m destroy macro m
:unmap! m destroy insert mode macro m
:ab <1> <2> abbreviate - replace <1> with <2> whenever typed as a word
:unab <1> unabbreviate <1>
:cd cd to
:set set ...

Go to Top


Set Options

Notice:

  • multiple options maybe specified on one line
  • abbreviations in parenthesis may be used
  • :set ? display the value of the options
  • for options without a value, set no turns it off
Option Default value Meaninig
autoindent (ai) noai makes new lines automatically indent to the position as the line above or below
autoprint (ap) ap display changes after each command
autowrite (aw) noaw automatically save file before :n, :!
beautify (bf) nobf ignore all control characters during input (except tab, newline, formfeed)
directory= (dir=) /tmp name of the directory to store buffer
edcompatible noedcompatible use ed-like features on substitute
errorbells (eb) errorbells sound bell on error
exrc (ex) noexrc allow .exrc files outside home dir
hardtabs= (ht=) 8 set boundary for hardware tabs
ignore case (ic) noic ignore case in regex expressions
lisp nolisp turn on lisp mode
list nolist display all tabs, end of lines
magic magic enable more regex expressions
mesg mesg allows mesgs to be sent to terminal
number (nu) nonumber display line numbers in file
open open allows open and visual
optimize (opt) optimize optimizes throughput of text by not sending carriage returns when printing text
paragraphs= (para=) IPLPPPQPPLIbp sets the delimiters for { and }
prompt prompt command mode input gives : prompt
readonly (ro) noro cannot write unless ! is given
redraw noredraw redraw screen when edits are made
remap remap allows macros that point to other macros
report= 5 report changes if they effect > 5 lines
scroll 1/2 windows amount of screen to scroll when scroll down is received in command mode. also, number of lines printer by z. (z prints 2*scroll)
sections= SHNHH HU defines end of section for [[ and ]]
shell= (sh=) /bin/sh default shell. uses SHELL environment, if set
shiftwidth= (sw=) 8 characters to shift when using shift commands
showmatch (sm) nosm show matching {, }, (, ), [, or ]
showmode noshowmode show which mode you are in
slowopen (slow) do not update display immediately after insert
tabstop= (ts=) 8 sets tabstop length
taglength= (tl=) 0 number of characters significant for tags ( 0 means all characters )
tags= tag, /usr/lib/tags define pathname of files containing tags.
term= set the terminal type
terse noterse display shorter error messages
timeout (to) timeout keyboard maps timeout after one second
ttytype= set the terminal type
warn warn display "No wirte since last change" messages
window= (w=) number of lines in window in visual mode
wrapmargin= (wm=) 0 set the right margin. greater than 0 will word wrap n spaces from the edge of the screen.
wrapscan (ws) ws searches wrap around end of file
writeany (wa) nowa allow saving to any file

Go to Top


Setting up .exrc file

Like many programs under UNIX, vi has a rc file too. It is called .exrc. It will load automatically when you start vi. Any commands that can be used in command input mode (: commands) can be used in this file. In addition, the source command (so), abbreviations (ab), and macros can also be used. No blank lines are permitted in .exrc file. A line begins with a " character is a comment.

The following is an example of .exrc file. Note: you don't need to specify the : character, it is assumed in this file.

"***************************************************
" Ingore case on search
set ic
" set wordwrap 9 characters from the right
set wm=9
" show matching parenthesis
set sm
" set the shell to tcsh
set sh=/bin/tcsh
" a few abbreviations for my email address
ab tongtao Tao Tong - tongtao_98@yahoo.com
"***************************************************

Go to Top


Making VI programmer-friendly

  • :set ai will make it auto-indent for you.
  • :set sw=# to set the shiftwidth, then you can use the >>, << to shift a line right or left, respectively. You can even use the <% or >% to shift the {}, [], and () pairs. Note # is a number here.
  • :set sm will show matching ), }, ]

Go to Top


Go Home | Go Back to Tips Page