[Next Chapter] [Top page] [Previous Chapter]


What is vi

Vi is a screen editor. This means that you can see part of the file in a window on the screen, and editing operations can be controlled by moving a cursor around the text on screen.

Vi works in a different way from the editing functions of modern word processors. It's effective use requires a considerable amount of expertise on the part of the user. The user must have the ability to remember and manipulate opaquely named one-letter commands that can be combined in an arbitrary variety of different ways.

Vi is a screen-based version of ex. It's lack of user-friendliness is largely a result of this. In many ways it still works like a line editor, with complicated commands typed in by the user.

The main enhancements on ex are the window, which enables you to constantly view part or all of the file, the visible cursor and the commands that can be issued without moving to the command line. Once you have learned to start vi, you will probably not need to use ex again. Everything that you have learned with ex, you can do with vi. What is more, with vi you have a window and the possibility to use interactive commands. The only time that you might want to use ex now is if you have trouble running a screen-based utility on your terminal.

Using vi

The next section lists the commands needed to start and use vi. In this section, the key concepts underpinning the use of vi are explained so that you can understand what is happening when you use it.

The first thing to understand is that there are three modes:

command mode:

insert mode

last line mode (or command line mode)

You start in command mode. The commands listed below for moving the cursor and changing the file are entered in command mode. To enter a command simply type it at the keyboard. What you type will not appear anywhere on screen. To abandon a command you have started, you can type <ESC>. If you are not sure which mode you are in at any time you can type <ESC> and return to command mode. When you leave the other modes you return to command mode. Insert mode is used to enter text. Insert mode is entered by issuing one of a variety of commands that involve entering text. Insert mode must be exited in order to issue more commands. A common mistake made is to attempt to enter a command while in insert mode, which results in the command appearing on screen as part of the text.

Last line mode is entered from command mode, and enables the user to type a command on the last line of the screen. Any ex command can be used in this way, simply by typing ':' followed by the command. The current line will be that where the cursor is positioned.

When you start vi you will see a screen similar to the one below. If you are starting a new file, or the file you are editing is less than 18 lines long, then the empty lines in the window will be marked by the '~' (tilde) character.

This is a small file called 'vi.prac'.
This is the second and last line.
"vi.prac" 2 lines 103 characters
A typical vi screen

Note that is necessary to press return at the end of each line of text that you enter. Otherwise, vi will interpret all of your text as a single line!


Create a new file, enter several lines of text and save it.

Edit an existing file that you have, making several changes.

vi reference

vi modes

  • command Normal and initial state. <ESC> cancels partial command
  • insert entered by the following commands: a, A, i, I, o, O, c, C, s, S, R. Terminates with <ESC> (or ^C).
  • last line entered by :, /, ? or !. Input is read and echoed at the bottom of the screen. Commands executed by <RETURN> or <ESC>, terminated by ^C.
  • Entering and leaving vi

  • % vi file edit file
  • % vi +n file edit starting at line n
  • % vi + file :edit starting at end
  • % vi +/RE/ file edit starting at RE
  • % view file read only mode
  • ZZ exit from vi, saving changes (same as :wq)
  • ^Z stop vi process, for later resumption
  • Some simple commands

    The following are examples of some compound commands, using the operators listed later.

  • dw delete word
  • de delete word leaving punctuation
  • dd delete line
  • 4dd delete 4 lines
  • xp transpose characters
  • cwtext<ESC> change word to text
  • File manipulation

    The following are all last line mode commands, so must be preceded by a colon.

  • w save changes
  • wq save and quit
  • q quit
  • q! quit, discarding changes
  • e file edit file
  • e! re-edit current file, discarding changes
  • w file write to file
  • w! file overwrite file
  • ! command execute shell command, then return
  • f show current file and line
  • Positioning within the file

  • ^F forward one screenful
  • ^B back one screenful
  • ^D scroll down half screen
  • ^U scroll up half screen
  • nG go to line n (last line default)
  • /RE/ go to next occurrence of RE
  • % find matching bracket
  • Marking

  • `` return to previous cursor position
  • mx mark position with x
  • `x go to mark x
  • Line positioning

  • H top line of window (home)
  • M middle line of window
  • L last line of window
  • + next line, at first non-white character
  • - previous line, at first non-white character
  • <RETURN> same as +
  • j next line, same column (same as down arrow)
  • k previous line, same column (same as up arrow)
  • Character positioning

  • 0 beginning of line
  • ^ first non-white in line
  • $ end of line
  • <SPACE> forward (same as right arrow)
  • fx find x forwards in current line
  • Fx find x backwards in current line
  • ; repeat last find command forwards
  • : repeat last find command backwards
  • n| go to column n
  • Words, sentences, paragraphs

  • w forward to start of next word (delimited by non-alphanumeric character)
  • b back to start of last word
  • e forward to end of next word
  • W as w, with word delimited by blank only
  • B as b, with word delimited by blank only
  • E as e, with word delimited by blank only
  • ) forward to start of next sentence
  • ( Back to start of next sentence
  • } Forward to start of next sentence
  • { Back to start of last sentence
  • Corrections during insert

  • H erase last character (or your usual delete key)
  • W erase last word
  • \ escape character
  • <ESC> ends insert; back to command mode
  • C ends insert
  • Insert and replace commands

  • a append after cursor
  • i insert before cursor
  • A append at end of line
  • I insert before first non-blank
  • o open line below current line
  • O open line above current line
  • rx replace single character with x
  • R replace characters
  • Operators

    The following can be doubled to apply to a line and also preceded by a number to indicate a number of lines. They can be combined with positional commands (e.g.d$ to delete to end of line.)

  • d delete
  • c change
  • y yank
  • Miscellaneous operations

  • x delete character
  • X delete character to left of cursor
  • C change rest of line (same as c$).
  • D delete rest of line (same as d$)
  • J join lines
  • Y yank (paste) lines
  • Yank and put

  • p put back after cursor
  • P put back before cursor
  • "xp put from buffer x
  • "xy yank to buffer x
  • "xd delete to buffer x
  • Undo, redo and retrieve

  • u undo last change
  • U restore current line
  • . repeat last command
  • "np retrieve nth last delete
  • [Next Chapter] [Top page] [Previous Chapter]