Home    About Us    Products    Services   Contact Us  

Linux Text Editors

The Vi Editor

To start, create a new file named cat.joke by typing vi cat.joke

You'll see a screen that looks like this:

|

~

~

~

~

~

~

~

~

~

"cat.joke" [New file]

Adding New Text to Your File
Your cursor (the vertical bar at the top of the figure below is in the upper-left corner of the screen, and the message at the bottom tells you that a new file called cat.joke was just created. The tilde characters in the first column are just placeholders for empty lines. Now press the letter a to enter Input mode and type the lines shown in here. Press enter at the end of each line to go on to the next.

Anu: Knock, knock...

Bibb: Who's there?

Anu: The Interrupting Cat.

Bibb: The Interrupting Cat wh...

Anu: MOOOOOO!

|

~

~

~

~

"cat.joke" [New file]

Saving Your Work
So far, so good--let's save this little masterpiece. You're still in Input mode, so press esc to enter Command mode; then type ZZ (to put your file to sleep). You won't see any Z's on the screen, but after you've entered the second Z, your file will disappear, your Linux command prompt will return, and you'll see this message, indicating that your file was successfully saved:

"cat.joke" 6 lines, 113 characters.

Congratulations--you've just survived your first encounter with vi. You know that the a command switches to Input mode, esc gets you back to Command mode, and ZZ saves the file, but you'll have to expand this limited repertoire to get any real work done.

Common vi Commands
Have a look at this list of common vi commands (there are many more, but these will at least allow you to get some basic work done). Then we'll do one more exercise before moving on.

Note: As with all of Linux, vi commands are case sensitive.

Entering Input Mode

a Add text after the cursor.
i Insert text before the cursor.
R Replace text starting at the cursor.
o Insert a new line after the current one.

Entering Command Mode

esc Switch from Input mode to Command mode.

Exiting or Saving Your File

:w Write file to disk, without exiting editor.
ZZ Save the file and exit.
:q! Quit without saving.

Trying Out Some vi Commands
Here's another example to try out some of the vi commands. Enter the following command to fire up vi again, and you should see the file as we left it in the last example:

vi cat.joke

Changing Text
Let's change Bill's name to Biff on the second line. To do so, use the arrow keys to position your cursor on the third character of line 2 (the letter "b" in Bibb); then press x twice (to delete the two b's). Now press i (to enter Input mode) and then type ff to complete the change from Bibb to Biff.

Tip: Be careful about pressing the arrow keys while you're in Input mode. In some versions of vi you can position the cursor only in Command mode. You could also have used the R command to do this job of replacing text, so use it to change the other Bibb now. Press esc to enter Command mode; then type /Bibb to search for the word Bibb. The cursor should move to line 4, right to where Bibb is located.

Now position your cursor on the third character (the letter "b"), press R to replace the characters, and type ff. Both Bibbs should now be Biffs.

Adding and Deleting Lines
Press esc to enter Command mode; then press the o key to add a new line. You're in Input mode again, so you can type whatever you like on this new line. But that would ruin the joke, so delete this new line by pressing esc and then entering the dd command. The line you just added should go away.

Quitting without Saving Your Changes
"Bibb" just doesn't have that wholesome ring to it, so let's forget about all the changes we've made in this editing session and exit vi without saving the file. Make sure you're in Command mode, enter the :q! command, and then press enter. Your Linux prompt should return, and the cat.joke file will be just as it was before.

Parting Words about Vi
Using vi can be frustrating, but it really isn't rocket science once you get used to the concept of the two modes and get the hang of when it's okay to move your cursor or enter text. If you're ever unsure about which mode you're in, simply press esc once or twice, and you can be sure you're in Command mode. There are some powerful (but arcane) commands that diehard vi users use to get things done quickly in this relic-of-the-sixties text editor. The man vi command will tell you a lot more about vi if you decide you want to become proficient.