VI Editor
There are two main modes to the vi editor: text insertion mode and vi command mode. Unlike
most other editors, the vi editor does not start you out in text insertion mode; you start in the vi
command mode. You can move back and forth between the two using the escape key.
vi command mode will allow you to move around in your text, enter the text insertion mode,
replace text, move/copy blocks of text, etc. There is also a large group of commands that can be
entered from the colon (:) prompt, such as: leaving the vi editor (with or without saving your
text), searching for text strings, copying blocks to text, etc. Please note: You do not hit the
return key after you enter a vi command.
text insertion mode will only allow you to insert text. There are several ways to get into text
insertion mode from the vi editor, depending on where you want to start inserting text. The only
way to get out of text insertion mode and back into command mode is by use of the “esc” key.
Please note: While you are in text insertion mode you cannot move up/down lines to insert text.
If you need to add text to another location than you cursor is located, you must leave text
insertion mode (“esc”), move to the new location, and re-enter text insertion mode.
                   Moving the Cursor
                                                            x         delete character
         by character                                       dd        delete line
h                one character to the left                  dw        delete word
l                one character to the right                 D         delete rest of line
n                moves to n column of line                  d)        delete rest of sentence
                                                            xp        transpose 2 characters
         by word
w                   forward to start of word                            Inserting Text
e                   forward to end of word
b                   backward to start of word               a         append text after cursor
                                                            A         append text at end of line
         by line                                            I         insert text before cursor
0 or ^              forward to start of line                I         insert text at beginning of line
$                   forward to end of line                  o         open a blank line below the cursor
j                   down one line                           O         open a blank line above the cursor
k                   up one line
ENTER               forward to start of next line                      Replacing Text
         by line number                                     r         replace single character
lG                goto line 1                               R         Replace multiple characters
nG                goto line n                               c         (c followed by a space) change
G                 goto last line of file                              character
                                                            cw        change word
         by screen                                          cc        change line
H                 top of screen                             C         change rest of line
M                 middle of screen                          c)        change rest of sentence from cursor
L                 last line of screen                       s         substitute character
^d                down half screen                          S         substitute line
^u                up half screen
^f                forward one screen
^b                backward one screen
^e                screen up one line
^y                screen down one line
                     Deleting Text
                                                        1
           Line Move and Copy Commands                          :set nu           display line numbers
                                                                :set nonu         remove line numbers
yy                  yank line to buffer
yw                  yank word to buffer                                     Saving Text and/or Leaving vi
dd                  delete line to buffer
dw                  delete word to buffer                       :w                write
p                   put buffer below cursor                     :wq               write and quit
P                   put buffer above cursor                     :x                write and quit
“cy                 yank buffer to c                            :q                quit
“cp                 put buffer c                                :q!               quit and ignore changes
                                                                ZZ                write if changed and quit
           Searching for a Character String                     :r filename       read in file filename
                                                                :w filename       write to file filename
/string             forward search for string                   :n,m w filename   write lines n through m
?string             backward search for string                                    to filename
n repeat            last search in same direction
N repeat            last search in reverse direction                 Moving a Section of Text: Cut and Paste
               Global search and replace                        a:      Move the cursor to the beginning of the
                                                                        block to be moved
                    :g/string1/s//string2/g                     b:      Use ndd to delete n lines.
                              replaces all occurrences          c:      Move the cursor to the place you want to
                              of string1 with string2                   insert the cut text.
                                                                d:      Use p to paste below the cursor or P to paste
               Miscellaneous Commands                                   above the cursor.
J                   join the line below the cursor to the
                    current line
:!                  execute a shell command and stay
                    in vi
:set                auto-indent
:csh                open a shell
^d                  close shell and return to vi
:r                  read results of UNIX command
                    into the buffer
              Leading Command Counts
           All commands that do not begin with a colon
           (:) can be preceded by a number to perform
           multiple repetitions of the command. For
           example, 22y will yank 22
           lines into the buffer and 7cw will change 7
           words.
                 Repeating a Command
           Press the period (.) to repeat the last
           command.
                      Undoing Edits
u                   undo last change
U                   undo all changes on the same line
               Displaying Line Numbers