Part 1 - Creating a simple file
The best way to learn vi is by using it. With our 'vi reference guide' and 'frequently asked questions', there should be little else you need to become an expert in the editor. However, many peoples' first experience with vi is when they are in a rush to get something done, which can be problematic when you don't have time to learn the editor. This tutorial will get you to the stage, in about 10 minutes, where you can run vi, load a file, make basic edits, and save it again. That may be all you'll need for now. When you need to move onto more advanced territory, use our 'vi reference guide' to learn additional commands.
Running vi
To run vi and create a new file, simply run the command 'vi' from any shell prompt:
$ vi
Alternatively, to load an existing text file into vi, run the command 'vi [filename]', from the shell prompt:
$ vi myfile.txt
Your file will be loaded into vi, and the cursor will be placed at the beginning of the first line. Note that an empty line is shown as a tilde (~).
Command Mode and Insert Mode
vi is different from many editors, in that it has two main modes of operation: command mode, and insert mode. This is the cause of much of the confusion when a new user is learning vi, but it is actually very simple to understand.
When you first load the editor, you will be placed into command mode. To switch into insert mode, simply press the 'i' key. Although nothing will change on the screen to indicate the new mode, any thing that you type from now on will appear in the screen - this is what you are used to if you have ever used any other editor, or word processor. Try typing a few lines of text. When you press 'return' or 'enter' a new line will be created, and you may continue typing.
When you have finished typing, you may return to command mode. This is done by pressing your 'Esc' key. In command mode, key presses do not appear on the screen, but instead are used to indicate various commands to vi.
At first, you may often mistake command mode and insert mode. For example, you may think you are in insert mode, and start typing your text, when in fact you are in command mode, and each keypress you make will issue a command to vi. Be careful - you may accidentally modify or delete parts of your file.
If you are unsure which mode you are in, press 'Esc'. If you were in insert mode, you will be returned to command mode. If you were already in command mode, you will be left in command mode (possibly with a 'beep', to indicate that you were already in command mode).
An Exercise
Try this simple exercise to get used to the two modes:
1. Load vi (if you haven't already), by typing:
$ vi
(don't forget - the $ is the system's prompt, and may be different on your system. You only type the 'vi' part, shown in bold.)
The screen should show a blank file, with each blank line represented by a tilde (~), for example:
~
~
~
~
~
By default you are in command mode.
2. Switch to insert mode, by pressing the 'i' key. Then type some text, using ENTER or RETURN to start new lines. For example:
Hello. This is my first session in the vi editor.
This is the second line of text.
~
~
~
3. When you have finished entering your sample text, press 'Esc' to return to command mode.
4. Now we will learn a useful command: to save the file. The command for this is ':w' (note the colon before the 'w'). After the 'w', put a space, and the name you want to store the file as. For example:
:w firstfile
Type it now. Notice how the text ':w' appears at the bottom on the screen. When you have finished the command, press ENTER or RETURN. You should see a confirmation that the file has been saved, which may include the number of lines in the file, and possibly the file size.
5. To finish this simple introduction to the editor, we will learn one final command: How to close the editor. The command for this is ':q' (again, with a colon before the 'q'). Don't forget you need to be in command mode. If you're not already in command mode, or you're not sure which mode you're in, press 'Esc' now. Then issue the command:
:q
You should be returned to the UNIX prompt.
A final note:
Sometimes, you may have made changes to a file that you do not want to save. This may be because you have decided the changes are incorrect, or you have become confused using vi (not usual at first!), and incorrectly made some changes, maybe by typing into command mode instead of insert mode. To exit vi without saving, and ignoring any warnings about unsaved data, use a variation of the ':q' command, with an exclamation mark after it:
:q!
This will return you to the prompt, without saving any changes to the file, and with no warnings about unsaved data. Use this command carefully.
Ready to move on?
Before you move onto the next segment, it may be worth just playing with what you've already learned. Practice loading vi, either with an existing file, or with no filename so it creates a new file. Try going into insert mode, inserting some text, going back into command mode, saving the text, going back into insert mode, etc. You should be able to do the following:
Load vi with no parameter, so it creates a new file.
Load vi with a filename, so it loads that file. Try loading the file you saved in the exercise above.
Switch into insert mode using the 'i' key, and insert some text.
Switch into command mode using the 'Esc' key, and save the file using the ':w filename' command.
Exit vi using the ':q' or ':q!' commands.
Part 2 - Some basic editing commands
In the previous segment, you learned how to load vi, and switch between command mode and insert mode and back again. You also learned how to save a file, and exit vi. In this segment, we will look at some basic editing commands. By the end of this segment, you will know enough to use vi to create and manipulate files.
The cursor keys
Probably the most basic and most commonly used commands are those used to move the cursor around the screen. The cursor is the 'pointer' to the current position in the text, and is usually represented as a flashing underscore character.
To move the cursor around the screen, you must be in command mode. On some systems, you may use the standard 'cursor keys' on your keyboard to move the cursor. However, on many systems, this will not work. This mainly due to historical reasons, dating back to when not all terminals had cursor keys. If cursor keys do not work on your system, then use the following keys instead:
h - Cursor leftj - Cursor downk - Cursor upl - Cursor right
This may seem obscure at first, but you'll be surprised how quickly you get used to using these keys - you may even find yourself using them on terminals that do have working cursor keys!
Exercise: Load vi. Create a new file. Enter a few lines of text. Then go into command mode, and practice moving the cursor around the screen. Do your 'real' cursor keys work? Or do you have to use the other keys listed above? Try the keys above anyway. Go back into insert mode and insert some more text. Note that you can insert text wherever you put the cursor. Don't forget to go back into command mode before trying to move the cursor again. When you've finished, go into command mode, and use the ':q!' command to exit vi.
This exercise may also help you to understand the difference between command mode and insert mode better. When you are in insert mode, pressing any key, including h, j, k or l does what you'd expect:- it puts that character in the file in the current position. However, in command mode, the keys have a different function - to move the cursor. Nearly all keys have a different function in command mode - see our reference guide for the complete list. A common mistake for newcomers to the editor, is to start typing when in command mode. Of course, the exact consequences of this depend on which keys you press, but this nearly always has undesirable results!
Deleting a character
While you are in insert mode, you can delete a character that you have just typed by using the 'backspace' key - this is similar to most other editors and word processors. But there is a difference: You may only delete characters that you have entered in this session of insert mode. To understand this, try the following exercise:
Exercise: Load vi, and go into insert mode. Enter the following text: 'abcdefg'. Then press 'Esc' to return to command mode. Move the cursor back, using the left arrow or 'h' key, until the cursor points to the letter 'd'. Then press 'i' to go back into insert mode, and type 'hijklmn'. Now try to delete some of the characters using the backspace key. You will find that you can delete the characters that you have entered since being insert mode, but not those entered previously. So the 'abc' will remain undeleted. When you've finished, go into command mode, and use the ':q!' command to exit vi.
So how do we delete any characters that were entered in previous insert mode sessions? The answer is to go back into command mode, place the cursor over the character to be deleted, then press the 'x' key. This causes the character under the cursor to be deleted from the file. Any characters to the right of it will be shifted left one to take its place.
Exercise: Load vi, and go into insert mode. Enter the text '0123456789'. Then return to command mode, and use the cursor keys to go back until the cursor is pointing to the character '3'. Press 'x'. What happens? Press 'x' twice more. You can now see how to delete a character in any position. When you've finished, go into command mode, and use the ':q!' command to exit vi.
Repeating a command
What if we wanted to delete a whole batch of characters? For example, if we had 20 characters to delete, it would take 20 keypresses. vi provides a simple way of performing this - in fact, many commands can automatically be run a given number of times.
To repeat a command, simply type the number of times to run the command before the command itself. For example, to delete 20 characters from a file, starting from the current position, using the command '20x'. Note that the command does not appear at the bottom of the screen, so you have to keep track of what you're typing yourself.
Exercise: Load vi, and go into insert mode. Enter a few lines of text, each containing at 30-40 characters, or more. Then return to command mode. Move the cursor to the beginning of the first line, then (still in command mode), type '20x'. Note what happens. Note that other commands, including cursor commands can be repeated in the same way. Go down the the second line in your file, and type '5l'. Notice how the cursor move 5 places to the right (or to the end of the line, whichever comes first). When you've finished, go into command mode, and use the ':q!' command to exit vi.
Deleting a line
Just one more command to learn for now. We've seen how to delete a single character with the 'x' command. There is a quicker way to delete an entire line: with the 'dd' command.
Exercise: Load vi, and go into insert mode. Enter several lines of text, preferably at least 10. Each can just be a few characters long. Then return to command mode. Move the cursor up to the top of the screen (hint: use repeat commands like '20k' to make this easier.) Delete the line the cursor is on by pressing 'dd'. Then try the repeat version of that command - for example, type '5dd' to delete the next five lines. When you've finished, go into command mode, and use the ':q!' command to exit vi.
Ready to move on?
In this segment, you've learned the most common commands you'll ever use in vi. You now know how to move the cursor (including moving it large amounts at a time, by repeating the commands), and how to delete characters and lines. You've learned how to repeat commands to make bulk editing easier.
By this point in the tutorial, you already know enough to complete most basic editing tasks. For example, you could now load an existing file, and make basic edits to it, and save it again. You could also create a new file from scratch, for example, a letter to a friend, or a list of phone numbers and addresses.
At this point, we recommend that you practice your current skills for a while. Try creating a few files, and practice the basic operations of moving around, inserting and deleting. Make sure you're really happy with how to save a file, and reload it later.
At any time, you can refer to our vi reference guide - in fact, we suggest you bookmark it in your browser, so you can easily get to it whenever you need it. Don't be overwhelmed by the number of commands vi offers - you'll find you quickly learn the ones you need on a regular basis, and can quickly look up other commands in the reference guide whenever you need to. Bookmark it now, and get used to using it whenever you need a vi command which you can't remember. There's no better way of expanding your knowledge of vi, than by using it for real tasks as part of your day-to-day work.