Vi for Everyday Use


Introduction


This document is meant to serve as a simple introduction to vi. I've noticed that many people fumble through vi only when they have to, and use other editors, mainly point-and-click, for every day use. As a result, most people don't understand the power and the ease that vi can provide, and when forced to use vi, are barely functional.

Within this document, I'll try to cover the basics in moving within a document, the various editing commands and methods, setting simple environment variables, and I'll discuss Vim, Vi IMproved.

Before I get into the commands used, a few things must be made clear. First of all, vi has two distinct modes of action: command mode and insert mode. From within command mode almost all commands are issued and movement is performed. Vi automatically places the user in command mode upon entering vi, otherwise pressing the escape key will put the user in command mode.

Alternatively, insert mode is used to type any content which the user wants in the document. There are several different ways to enter insert mode, each of which has a specific purpose and action. The insert modes are defined in pairs, always hinting at an order by which the commands are organized. These will be documented later.

Moving Within the Document


Movement within a document is performed not by the now-typical point-and-click method (which I firmly believe is inferior, anyway), but rather by keyboard commands. I find this is more useful, since the hands can remain on the keyboard, rather than reaching for the mouse.

Character Movement

h
Left one character
j
Down one line
k
Up one line
l
Right one character
space
Right one character

Text Block Movement

w
Forward one word
b
Backward one word
e
End of word
(
Beginning of current sentence
)
Beginning of next sentence
{
Beginning of current paragraph
}
Beginning of next sentence

Line Movement

0
First position in a line
$
Last position in a line
^
First non-blank character in a line
+
First character of next line
return
First character of next line
-
First character of previous line
H
Top line of screen
M
Middle line of screen
L
Last line of screen

Screen Movement

CTRL-f
Forward one screen
CTRL-b
Backward one screen
CTRL-d
Forward one-half screen
CTRL-u
Backward one-half screen
CTRL-e
Scroll one line forward
CTRL-y
Scroll one line backward
CTRL-l
Redraw screen (no scrolling)

Line Number Movement

CTRL-g
Display current line number
nG
Move to line n in file
:n
Move to line n in file
G
Move to last line in file

Search Movement

/text
Search forward for text
n
Repeat previous search
N
Repeat previous search in opposite direction
?text
Search backward for text
%
Find match of current parenthesis, brace or bracket

Inserting Text


a
Append after cursor
A
Append at end of line
c
Begin change operation
C
Change to end of line
i
Insert before cursor
I
Insert at beginning of line
o
Open a line below cursor
O
Open a line above cursor
r
Replace single character
R
Begin overwriting text (extended replace)
s
Substitute a single character
S
Substitute entire line

Basic Syntax of a Vi Command


Vi commands have a simple general form:

[n] operator [m] object

Note that if n operations are performed on m objects, the action is performed n*m times.

The important basic operators are:

c
Begin a change
d
Begin a deletion
y
Begin a yank (copy)

It is probably important to note that a deletion is akin to the traditional cut command in most editors, where the deleted text is placed in a buffer which can be later placed, replaced or otherwise manipulated.

Objects that are acted upon are the same as the movement commands they represent. Some examples include:

d^
Delete back to beginning of line
2cw
Change next two words
c$
Change to end of line
y}
Yank entire paragraph

When concerned only with the current line, some shortcuts are define to make things quicker. These are cc, dd and yy, which refer to change line, delete line and yank line, respectively. These shortcuts can also be used with the general command syntax, as shown:

2yy
Yank 2 lines
5cc
Change 5 lines
7dd
Delete 7 lines

There are a few text editing commands that don't really fit anywhere very well, but are still handy to know:

J
Join two lines
:j!
Join two lines, preserving blank spaces
<<
Shift this line left one shift width
>>
Shift this line right one shift width

Interaction Commands


Most of the following commands are useful in making vi interact with the operating system that it runs underneath. This includes saving files, opening files, spawning shells, and quitting vi.

Saving and Exiting

ZZ
Quit vi, writing file only if changes were made
:x
Quit vi, writing file only if changes were made
:wq
Quit vi, writing file
:w
Write file
:w filename
Write file to filename
:w!
Write file (overriding file protection)
:w! filename
Overwrite filename with current file
:w %.new
Write file to file.new
:q
Quit vi
:q!
Quit vi discarding edits
:e!
Return to last saved version of file
:e#
Edit alternate version of file

Accessing Multiple Files

:e file
Edit file, current file becomes alternate
:n
Edit next file
:n files
Specify new list of files.
:args
Display list of files to be edited
:rew
Rewind list of multiple files

Interacting With UNIX

:r file
Read in contents of file after cursor
:r !command
Read in output from command after current line
!command
Run command, then return
!!
Repeat last system command
:sh
Spawn subshell, return upon EOF
CTRL-z
Suspend vi process (use fg to restart process)
:so file
Read and execute commands from file

Macros


Most of what can be done with macros is beyond the scope of this document, but there should be some mention of the power that is implied with a few simple commands.

:ab fn fullname
Use fn as abbreviation for fullname
:unab fn
Remove abbreviation fn
:map c sequence
Map character c as sequence of commands
:unmap c
Remove map for character c
:map
List characters that are mapped

The following characters are not normally mapped in command mode by vi, although I typically use g to transport the cursor to the home position:
Letters: g K q V v
Control Keys: ^A ^K ^O ^T ^W ^X
Symbols: _ * \

Environment Variables


Adjustment of the environment that you use can easily make the difference when choosing your default editor. Like most other editors (or operating systems, in the case of emacs), the enviroment is quite configurable, by way of the :set command, which is used as follows:

:set x
Enable option x
:set nox
Disable option x
:set x=val
Assign val to option x
:set
List changed options
:set all
List all options
:set x?
Show value of option x

There are many options that can be changed with the :set command. This table should summarize the important ones:

Option Default Description
autoindent
ai
noai In insert mode, indent each line to the same level as the line above or below.
autoprint
ap
ap Display changes after each editor command.
autowrite
aw
noaw Automatically write file if changed before opening another file or executing a UNIX command.
beautify
bf
nobf Ignore all control characters during input (except tab, newline and formfeed).
errorbells
eb
errorbells Sound bell when an error occurs.
hardtabs=
ht
8 Define boundaries for terminal hardware tabs.
ignorecase
ic
noic Disregard case during a search.
list nolist Prints tabs as ^I and end of lines with $
mesg mesg Allow system messages to display on terminal while in vi.
number
nu
nonu Display line numbers on left of screen during editing.
shiftwidth=
sw
8 Define number of spaces in backward tabs when using autoindent function.
showmatch
sm
nosm When ) or } is entered, cursor displays matching ( or {. If matching brace does not exist, the error bell is sounded.
showmode noshowmode When in insert mode, displays the type of insert being made.
tabstop=
ts
8 Define number of spaces that a TAB indents during editing session.
wrapmargin=
wm
0 Define right margin. If greater than zero, automatically inserts carriage returns to break lines.

The :set command can also be written into a startup file, ~/.exrc, which is read every time vi is used. This will allow users to override defaults, and thereby customize their environment.

As an example of this, I offer my own .exrc file:

:set autoindent
:set autoprint
:set autowrite
:set nomesg
:set redraw
:set shiftwidth=3
:set showmatch
:set showmode
:set tabstop=3
:set wrapmargin=5
map g 1G

VIM: Vi IMproved


I'm only going to touch on vim in this document, and point any interested people to the homepage for additional information.

Vim affords all the luxuries and comforts of vi, but, like the name implies, is improved and expanded on. Vim offers to the user split screen editing, mouse action and a limited graphical interface. Features that might not be noticed but are available are expanded limits, unlimited undos and redos and text formatting.

The source code for vim is freely available and compiles easily on virtually everything. It is available in CEB 540, and the adventurous can compile it in CEB531.

Vim is started much like vi, but has a few options available to it.

vim -g
Vim will start in graphical mode, offering mouse action and menus.
vim -o files
Vim will start will the screen split into enough screens to display all of the files being opened.

By typing :help upon entering vim, the user is brought to a hyperlinked text file that will instruct the user in the many many features that they may not be familiar with. These include the basic vi commands that are summarized above. Some of the helpful new options to explore are:

:sp filename
Splits screen and opens filename in new screen
CTRL-w CTRL-w
Rotates cursor through split screens
CTRL-w CTRL-r
Rotates screens when split screen
CTRL-r
Redo

I'm not going to list all the useful commands that vim offers, since they are many. I encourage any adventurous people to try vim. It, like vi, can be used on dumb terminals, but offers more flexability.


Greg Bodnar
Devendra Singh