Logo of Science Foundation Ireland  Logo of the Higher Education Authority, Ireland7 Capacities
Ireland's High-Performance Computing Centre | ICHEC
Home | News | Infrastructure | Outreach | Services | Research | Support | Education & Training | Consultancy | About Us | Login

Vi tutorial

Introduction to Vi

vi (pronounced "vee eye") is an editor you can find at almost every Unix installation. It is initially somewhat hard to get used to, but it has many powerful features. The aim of this tutorial is to get you started using the vi editor. This tutorial assumes no vi experience, so you will first be exposed to the ten most basic commands. These fundamental commands are enough to perform the bulk of your editing needs, and you can expand your vi vocabulary as needed.

Using vi

To invoke vi, type the letters vi followed by the name of the file you wish to create. You will see a screen with a column of tildes along the left side. vi is now in command mode. Anything you type will be understood as a command, not as text to be input. In order to input text, you must type a command. The two basic input commands are the following:

  • i: insert text to the left of the cursor
  • a: append text to the right of the cursor

Since you are at the beginning of an empty file, it doesn't matter which of these you type. Type one of them, and then type in the following text (a poem by Augustus DeMorgan found in The Unix Programming Environment by B.W. Kernighan and R. Pike):

Great fleas have little fleas<Enter>
 upon their backs to bite 'em,<Enter>
And little fleas have lesser fleas<Enter>
 and so ad infinitum.<Enter>
And the great fleas themselves, in turn,<Enter>
 have greater fleas to go on;<Enter>
While these again have greater still,<Enter>
 and greater still, and so on.<Enter>

Note that you press the Esc key to end insertion and return to command mode.

Cursor movement commands

  • h: move the cursor one space to the left
  • j: move the cursor one space down
  • k: move the cursor one space up
  • l: move the cursor one space to the right

These commands may be repeated by holding the key down. Try moving around in your text now. If you attempt an impossible movement, e.g. pressing the letter k when the cursor is on the top line, the screen will flash, or the terminal will beep. Here are more movement commands.

  • w: move to the start of the next word
  • e: move to the end of the next word
  • E: move to the end of the next word before a space
  • b: move to the start of the previous word
  • 0: move to the start of the line
  • ^: move to the first word of the current line
  • $: move to the end of the line
  • <CR>: move to the start of the next line
  • -: move to the start of the previous line
  • G: move to the end of the file
  • 1G: move to the start of the file
  • nG: move to line number n
  • <Cntl> G: display the current line number
  • %: to the matching bracket
  • H: top line of the screen
  • M: middle line of the screen
  • L: bottom of the screen
  • n|: more cursor to column n

To move the cursor a number of positions left.

  • nh: move the cursor n positions left

The commands that deal with lines use the modifier to refer to line numbers. The G is a good example.

  • 1G: Move the cursor to the first line

has a large set of commands which can be used to move the cursor around the file. Single character movement through to direct line placement of the cursor. vi can also place the cursor at a selected line from the command line.

vi +10 myfile.tex

This command opens the file myfile.tex and places the cursor 10 lines down from the start of the file.

Try out some of the commands in this section. Very few people can remember all of them in one session. Most users use only a subset of the above commands.

Modifying Text

The aim is to change the contents of the file and vi offers a very large set of commands to help in this process. This section will focus on adding text, changing the existing text and deleting the text. At the end of this section you will have the knowledge to create any text file desired.

When entering text, multiple lines can be entered by using the RETURN key. If a typing mistake needs to be corrected and you are on the entering text on the line in question. You can use the BACKSPACE key to move the cursor over the text. The different implementations of vi behave differently. Some just move the cursor back and the text can still be viewed and accepted. Others will remove the text as you backspace. If the text is visable and you use the ESC key when on the line you have backspaced on the text after the cursor will be cleared. Use your editor to become accustomed to its behaviour.

  • a: Append some text from the current cursor postion
  • A: Append at the end of the line
  • i: Insert text to the Left of the cursor
  • I: Inserts text to the Left of the first non-white character on current line
  • o: Open a new line and adds text Below current line
  • O: Open a new line and adds text Above the current line

vi has a small set of delete commands which can be enhanced with the use of command modifiers.

  • x: Delete one character from under the cursor
  • dw: Delete from the current position to the end of the word
  • dd: Delete the current line.
  • D: Delete from the current position to the end of the line

On occasions you may need to undo the changes. The following commands restore the text after changes.

  • u: Undo the last command
  • U: Undo the current line from all changes on that line

not only allows you to undo changes, it can reverse the undo. Using the command 5dd delete 5 lines then restore the lines with u . The changes can be restored by the u again.

Searching and replacing text

vi has a number of search commands. You can search for individual characters through to regular expressions.

The main two character based search commands are f and t.

  • fc: Find the next character c. Moves RIGHT to the next.
  • Fc: Find the next character c. Moves LEFT to the preceding.
  • tc: Move RIGHT to character before the next c.
  • Tc: Move LEFT to the character following the preceding c.

If the character you were searching for was not found, vi will beep or give some other sort of signal.

vi allows you to search for a string in the edit buffer.

  • /str: Searches Right and Down for the next occurance of str.
  • ?str : Searches Left and UP for the next occurance of str.
  • n: Repeat the last / or ? command
  • N: Repeats the last / or ? in the Reverse direction.

When using the / or ? commands a line will be cleared along the bottom of the screen. You enter the search string followed by RETURN.

The search and replace command allows regular expression to be used over a range of lines and replace the matching string. The user can ask for confirmation before the substitution is performed. It may be well worth a review of line number representation in the ed tutorial.

  • :<start>,<finish>s/<find>/<replace>/g General command
  • :1,$s/the/The/g Search the entire file and replace the with The.
  • :%s/the/The/g% means the complete file. (Same as above).
  • :.,5s/^.*//g Delete the contents from the current to 5th line.

The search command is very powerful when combined with the regular expression search strings. If the g directive is not included then the change is performed only on the first occurrence of a match on each line.

File saving

  • :w save (write to disk)
  • :q exit

Make sure you are in command mode by pressing the Esc key. Now type :w. This will save your work by writing it to a disk file.

The command for quitting vi is q. If you wish to combine saving and quitting, just type :wq. There is also a convenient abbreviation for :wq - ZZ. Since much of your programming work will consist of running a program, encountering a problem, calling up the program in the editor to make a small change, and then exiting from the editor to run the program again, ZZ will be a command you use often. (Actually, ZZ is not an exact synonym for :wq - if you have not made any changes to the file you are editing since the last save, ZZ will just exit from the editor whereas :wq will [redundantly] save before exiting.)

If you just want to start all over again, you can type :q! (remember to press the Esc key first). If you omit the !, vi will not allow you to quit without saving.


vi offers powerful commands that many more modern editors do not or can not offer. The cost for this power is also the main argument against vi; the commands can be difficult to learn and read. With a little practice and time, the vi command set will become second nature.