Unix Manuals

vi Tutorial

Part 2 - Some basic editing commands

NOTICE - New forum for all UNIX learners, users and programmers - click here

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 left
j - Cursor down
k - Cursor up
l - 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.


Learning the vi Editor
cover

Buy it from Amazon - click on the image for more details


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.

You now have two choices:

If you are following the UNIX® tutorial, you have now seen all you need to see of vi for now. Click here to proceed to the next segment of the UNIX tutorial, which will introduce the filesystem.

If you are following the vi tutorial, then you need to proceed to the next segment of the vi tutorial, which covers some more advanced editing tasks, such as cut and paste, and search and replace. This is currently under development - please email us if you would like to be notified of when it is ready.

UNIX Tutorials Contents Page.

vi Reference


This web site is provided entirely free of charge. However, if you would like to make a donation, which will encourage the development of new material, we would very much appreciate it - please click on the button below - thank you!

Home


© Copyright 2000-2003, Tom Reader, All Rights Reserved.
UNIX is a registered trademark of The Open Group in the US and other countries.
For more information on the use of the UNIX trademark, click here.
The contents of this site are not connected with or endorsed by The Open Group in any way.