How Do I Use VI?

Article courtesy Pat Palmer for her CIT 593 students

In VI, you're always in one of three modes:

  1. navigation - moving the cursor around with the h, j, k, l keys
  2. insert - whatever you type gets inserted into the file
    (hit ESC to get back to navigation mode)
  3. command - you have entered a : character, preceding a command string
    (hit [return] or [esc][esc] to get back to navigation mode)

General Startup

To use vi: vi filename
To exit and save changes: :wq!
To exit without saving changes: :q!
To save changes without quitting: :w!
To get out of insert mode: [esc]

Cursor Movement

h move left (backspace)
j move down
k move up
l move right

NOTE: A number can precede any VI command to tell VI to repeat that command that many times.
EXAMPLES:

j moves down 1 line
3j moves down 3 lines
30j moves down 30 lines
10k moves up 10 lines

SHOW CURRENT LINE

:.=

MOVING AROUND QUICKLY

:# move to line #
:$ move to last line of file

EXAMPLES:

:1 moves to first line of file
:5 moves to 5th line of file

Inserting

i insert before cursor
o open a line below cursor and enter insert mode at beginning of line
O open a line above cursor and enter insert mode at beginning of line

Deleting (Yanking)

x delete character under cursor
dd delete (yank) line under cursor

EXAMPLE:

5dd yanks 5 lines
p puts them back (below current cursor)

Restoring (Putting)

p puts last thing yanked to right of cursor

Copying Code

yy (yank)'copies' line which may then be put by the p(put) command.
<-- Precede with a number for multiple lines

Put Command

brings back previous deletion or yank of lines

P bring back before cursor
p bring back after cursor

Find Commands

? finds a word going backwards
/ finds a word going forwards

Replace String

:1,$s/newstring/oldstring/g from columns 1 to last ($), substitute globally (all lines)
:1,$s/newstring/oldstring/ from columns 1 to last ($), substitute from cursor forwards
:1,10s/newstring/oldstring/g from columns 1 to 10, substitute globally (all lines)

SPECIAL COLUMN DESIGNATORS IN REPLACE:

$ last column on the current line
^ first column on the current line

Delete unwanted control characters from a Windows text file

:1,$s/^M//g from line 1 to last line ($), get rid of the Control-M (carriage return)

NOTE: to enter ^M above, first enter [control-V], to get into the mode expecting control char, followed immediately by [control-M]

Miscellaneous Commands

. repeat last command
u undoes last command issued
J join current line with the next line
^G display current line number

READING (IMPORTING) FILES

:r filename copies (reads) filename into current file after cursor

TO COPY PART OF ONE FILE TO ANOTHER FILE:

  1. vi file1 file2    (this puts into file1 first)
  2. copy lines from file1 (yank and put them back)
  3. from file1, enter :n! to move to next file
  4. put the lines into the next file

SHELL ESCAPE

:!'cmd' executes 'cmd' as a shell command.
© Computing and Educational Technology Services