Using The Vi Text Editor
Using The Vi Text Editor
This document is intended to provide you with the basic information you need Introduction
to use the vi text editor from your WAM/Glue or OIT cluster UNIX account. This
document assumes that you are using your account from some kind of terminal Note: Access to WAM labs at
window — either by dialing in from home, by using a Mac or PC in a computer the University is limited. You
lab or office at the University, or by using a terminal window generated by the must have a valid University ID
“xterm” program on a UNIX workstation. Information on how to connect to to use the labs.
the OIT host computers with a terminal program is available on-line at http:
//www.helpdesk.umd.edu/topics/applications/terminal.
Vi is a full-screen text editor that is almost universally available on UNIX-based Note: For more about using
computer systems. This editor is available on the UNIX systems and the WAM the OIT UNIX cluster, refer to
workstations. Vi is useful for editing program files, entering data, composing Introduction to the OIT UNIX
mail messages, and plain text editing. System. It is available on-line
at http://www.helpdesk.umd.edu/
documentation/unix/cluster.shtml.
There are other text editors that can be used on UNIX systems; one of the most
You may also take the Peer
popular of these is pico. Pico is available on all of the UNIX systems maintained Training Course, Introduction to
by OIT. Another text editor available on UNIX systems is emacs. Documents
UNIX. Call 301.405.2941.
describing basic pico and emacs commands are available on-line at http:
//www.helpdesk.umd.edu/documentation.
In this document the word current, when used in reference to some unit of text, Conventions Used in
identifies the location of the cursor. For example: current character, current This Document
word, current line.
Note: UNIX is case-sensitive.
This document also uses several fonts to help clarify the meaning of the text: This means it makes a difference
whether you type commands or
fixed Literal commands you type appear in fixed format. filenames in UPPER or lower
italic Variables used with commands will appear in italic case. Thus FILE3 is different
format. from file3.
[] Optional variables will appear in square brackets.
To edit a file with vi, type any of the following commands at the UNIX system Starting Vi
prompt:
Entering Input Mode All of the following commands will change your mode from command to input:
Moving Around Within The following commands allow you to move around the current text appearing
the Current Text on your screen. Most of the commands can be preceded by a number, which
Window will move the cursor that number of characters, words, or lines. For example,
4j would move the cursor down four lines. When a number is not specified, one
space or line is assumed.
Please Note: Be careful when using the arrow keys on your keyboard as they
often do not work properly with vi and may cause unpredictable results.
2 University of Maryland
Using the Vi Text Editor
These commands allow you to change the portion of your document that Moving Around the
appears on your screen and to move around the document quickly. Entire Document
Ctrl-f move forward a screen
Ctrl-b move back a screen
Ctrl-u move up half a screen
Ctrl-d move down half a screen
num G move to the specified line (use Ctrl-g to display line number)
:num move to the specified line
G move to the last line in the file
:$ move to the last line in the file
Vi has search commands to find a particular section of your document quickly. Searching for Text
/ pattern search forward through the document for the next occurrence of
the pattern (or string of text)
? pattern search backward through the document for the next occurrence
of the pattern (or string of text)
n repeat search in the same direction
N repeat search in opposite direction
f char search forward from the cursor in the current line for a single
character (char)
F char search backward from the cursor in the current line for a single
character (char)
; repeat single character search in either direction (f or F)
Below is an example of vi’s searching feature. Pressing the ESC key, followed
by the slash (“/”) to indicate a search (which will take you down to the command Example Search
line), followed immediately by a searchstring (where searchstring in this example
is EDITOR), vi will position the cursor at the location of the search string.
Copying (“Yanking”) Vi allows you to copy or delete blocks of text and place them elsewhere in your
and Pasting Text document. The put commands below insert any text which has been put into the
buffer by the delete commands above or the yank (copy) commands below:
[num] yw copy num words into the buffer, starting with the current word
at the cursor
[num] yy copy num lines into the buffer, starting with the current line
[num] Y copy num lines into the buffer, starting with the current line
p put any text in the buffer after or below the cursor
P put any text in the buffer before or above the cursor
Below illustrates the copying feature. In this example, the cursor is positioned
before uncomment, typing 4yy yanks 4 lines into the buffer, then the cursor
moves to a destination position where the text is placed. Typing p, places the
contents in the buffer below the cursor.
# Run user environment customizations identified in your /.environment
# file. This is the place to include your own environment variables,
# tap commands, and other system wide setup commands. You can also
# cancel default behaviors listed above with “unsetenv” or “setenv”.
# ~/.environment is not sourced if NOCALLS is set (i.e., if
# the xlogin “SYSTEM”option is selected).
# The below line, if commented out, will cause X windows to automatically
# come up if logging in on the console.
setenv NO_X_WINDOWS 1
[num] cw change num words, starting at the cursor in the current word—
the word(s) will be replaced by any text you type until you press
Esc
C change the current line from cursor to end—the current line will
be replaced by any text you type until you press Esc
r replace the current character (you do not need to press Esc)
R edit the current line from cursor to end in typeover mode—you
will remain in typeover mode until you press Esc (you can press
Return to insert more lines before pressing Esc)
s replace the current character—the current character will be
replaced by any text you type until you press Esc
S replace the entire current line—the current line will be replaced
by any text you type until you press Esc
There are several ways to save your document and exit vi. Be sure to use the Saving Your File and
save command often when editing an important document. Quitting Vi
:w save changes (i.e., write) to your file
:w filename save changes (i.e., write) to the file specified
:q quit vi (when you haven’t made any changes)
:q! quit without saving changes
:wq or ZZ save changes to file and then quit
:! cmd execute a single command (cmd) and return to vi Note: The :! and :sh commands
: sh start up a new UNIX shell—to return to vi from the shell, type make it easy to execute UNIX
exit or Ctrl-d commands without exiting vi.
Vi has several options which affect the way vi functions and alter your editing
environment. These options can be set by hand from within vi (must be reset
every time you use vi), or they can be made permanent (i.e., set automatically)
by creating a .exrc file and including the commands there or setting the EXINIT
environment variable in your .login or .cshrc file. (Do not alter the .login or
.cshrc files.) Use the set all command to get a list of all of the options.
More Information? For technical assistance with the vi editor, one point of contact is the OIT Help
Desk. There are many ways to contact this resource:
Walk in: Computer & Space Sciences Building, room 1400, Monday -
Friday, 8:00 a.m. - 5:00 p.m.
Phone: 301.405.1500, Monday - Friday, 8:00 a.m. - 5:00 p.m.
Web site: http://www.helpdesk.umd.edu/
You can also submit a problem report to the OIT Help Desk by typing request
at the UNIX command prompt.
6 University of Maryland