Using the vi Editor

MSU Moorhead
Information Technology
or email: support@mnstate.edu

 

The vi editor is a command-driven screen editor available on email. It is primarily used for entering computer programs to be run on that system. The vi editor has the following operational modes:

Editor Commands

The following is a summary of the most frequently used features and commands. For more extensive help, enter the command man vi from the email prompt.

vi filename Gets you into the vi editor, there are two modes for the vi editor, command and insert. You can move the cursor, cut and paste, run system commands etc. as described below.
ESC Hitting the escape key when in the insert mode will put you back into the command mode.
Note: you must be in command mode to execute each of the commands in this section.
ZZ save the file just edited and get you out of the vi editor.
:q! exit the editor without saving any changes you made.
:w save the changes you made to, but leaves you in command mode.
:w file write the editing buffer contents to the named file.
:w! file overwrite the named file with the editing buffer contents.
h move cursor left
j move cursor down
k move cursor up
l move cursor right
w move cursor right one word
b move cursor left one word
0 (zero) go to beginning of the line
$ go to the end of the line
^d forward 1/2 screen
^f forward 1 full screen
^u back 1/2 screen
^b back 1 full screen
nG go to line number n
1G (one) go to beginning of file
$G go to end of file
H to top of screen
M to middle of screen
L to bottom of screen
i insert in front of cursor
a append after cursor
o open line under cursor (remains in insert mode)
O open line above cursor (remains in insert mode)
r replace character at cursor (remains in command mode)
R overwrite from cursor (remains in insert mode)
/string search forward for string
/ repeat the search in forward direction
?string search backward for string
? repeat the search in backward direction
:10,20 w! junk write lines 10-20 inclusive to file named junk
:r junk insert file named junk after the line with the cursor
:f give name of current file in editor
dd delete current line
ndd delete n lines beginning with current line
dw delete current word
ndw delete n words beginning with current word
x delete current character
p "put" buffer in at line below cursor
u undo the previously executed command
cw puts a '$' at the end of the word at the cursor and lets you change one word

Cut/Copy and Paste Text

Use the following subcommands in Command mode. An incomplete subcommand can be canceled by pressing Esc.

Note: There are 26 named buffers that one can use for cutting or copying and pasting and other functions. The buffers are named a..z (lower case).

To Cut and Paste Text

  1. Move cursor to first line to be cut.
  2. Enter command ndd, where n is the number of lines to be cut.
  3. Move cursor to the line before which you will paste.
  4. Enter command p to "put" the paste buffer in after the line on which the cursor rests.

To Copy and Paste Text to/from a buffer

  1. Move cursor to first line to be copied.
  2. Enter command "anyy, where a is the name of the buffer and n is the number of lines to be copied.
  3. Move cursor to the line before which you will paste.
  4. Enter the command "ap to "put" the contents of buffer a in the line following the cursor.

Setting Options

The vi editor allows you to customize options so that you can use the editor for a specific task. Use the set command to set or change an option. To view the current setting of options, enter :set all while in vi Command mode.

:set nu set line numbers
:set nonu no line numbers
:set ai set auto-indent
:set noai no auto-indent
:set window=n set n lines in the screen window
:set all displays settings of all options that can be set
:sh Temporarily suspends editing and puts you in the system command mode (not the editor command mode) so you can run system commands. To get back into the editor just hit ^d.
:set sw=n Set shiftwidth to n. With this option ^d will shift the cursor n columns to the left when you are in insert mode.

Abbreviations

You can define abbreviations for long phrases that you use often. vi then automatically expands these abbreviations whenever you enter them while in insert mode. Only single-byte characters can appear in the abbreviation and phrase; they cannot contain Asian multi-byte characters.

To define an abbreviation, enter:

:abbr phrase

where abbr is the abbreviation you specify for the longer text specified by phrase. For example, to specify the abbreviation imho for the phrase In my humble opinion, enter:

:abbr imho In my humble opinion

 


an equal opportunity educator and employer

Updated 08/15/06 by support@mnstate.edu