Linux Online Advertisement
[ Register ]

[ Applications ]
[ Documentation ]
[ Distributions ]
[ Download Info ]
[ General Info ]
[ Book Store ]

Advertisement

[ Courses ]
[ News ]
[ People ]
[ Hardware ]
[ Vendors ]
[ Projects ]
[ Events ]
[ User Groups ]
[ User Area ]

Programming Perl (3rd Edition)

[ About Us ]
[ Home Page ]
[ Advertise ]

Intermediate Level User Linux Course

Using the Vi editor

All about Vi

This is probably the most popular text editor for Linux. Even if you don't like it, you may end up using it quite often. If you need to make a quick change to a file, you can't beat 'vi'. This is not meant to be an exhaustive guide to vi. This is just meant to show you how to use the most common (and useful) commands. Let's start by opening a file.

NoteSometimes you need Vi
 

I had an unpleasant surprise once. A friend of mine who had installed Linux had somehow changed the default editor from vi to joe. He called to tell me that his crontab entries didn't do anything. One more reason to get to know vi. Crontab is designed for vi and may not work if you use certain alternative editors.

Vi basics

Example 1. Open file with vi

vi /etc/hosts.allow

Of course, just opening the file gets you nowhere, unless you only want to read it. That can be just as easily done with less, so we'd better look at what to do with 'vi' beyond just opening the file.

Adding text

To add text, you would type the following:

Example 2. Adding text

ESC + i
(i for insert)

And there you have it. Now you can type away. Of course, it doesn't really do us too much good if you're not able to save what you've typed. So let's save it, shall we?

Saving

Example 3. Saving text

ESC + w
(w for write)

Closing the file

Now, to finish you would type the following:

Example 4. Close file

ESC + q 
(q for quit)

Of course, there is a lot more that you may need to do. Let's have a look.

Vi for budding power users

Again, my aim is not to do into a treatise on vi, but here are a few more commands that you might need to do a little more heavy lifting.

Removing Lines

You may find that you need to remove an entire line from a file. Just place the cursor at the beginning of the line and type:

Example 5. Remove a line

ESC + d 
(d for delete)

Changing your mind

Sometimes you wish you hadn't done something. With vi, you can undo what you just did.

Example 6. Oops! Can I undo that?

ESC + u 
(u for undo)

Changing your mind (again)

Again, you have second thoughts. With vi, there are no regrets.

Example 7. No worries

ESC + q! 
(q! for quit and I *really* mean it!)

The exclamation point (!) is used in vi to override default settings. If you make changes to a file, vi is going to ask you if you want to save them. 'q!' means that you want to quit without saving.

Where did I put that word?

Did you misplace a word in your file. You can find it easily with vi

Example 8. Find a word

ESC + /[word] (slash for umm.. slash, I guess.)

If you're looking for the word nonplussed in your text (as in: 'vi is so easy I am nonplussed') you would type:

ESC /nonplussed
and it will find every instance of the word nonplussed.

Can I change that word?

Maybe you don't want to use the word nonplussed. Perhaps it's better to use a more well-known word. You can use vi to change the word

Example 9. Change the word

ESC + /[word] 
(slash for umm.. slash, I guess.)

First you could use the

/nonplussed
to look for that word. When you find it, you would then type
ESC : s/nonplussed/amazed/
to replace the word on that line.

If you were sure that you wanted to replace all instances of that word in the whole text, you could type this

ESC :%s/nonplussed/amazed/g
and nonplussed would be changed to amazed throughout the text.

If you want to get some control over what you replace - that is you want to used both nonplussed and amazed, then you would add gc to the end:

ESC :%s/nonplussed/amazed/g
. Vi will now ask you for confirmation.

Vi configuration settings

There are some basic vi configuration setting that you should be aware of if you want to use this text editor comfortably.

Word Wrapping

If you don't want your words running off the page into oblivion, you can set the word wrapping feature in vi

ESC : set wm=30

This is a good wrap for me personally. It sets the wrap to 30 spaces from the right so it makes it tight. This means that the bigger the number, the sooner you get a line break. If you send something via email, then this tight wrap ensures that it will get there without the lines being broken into stair-steps.

NoteVi as an email editor
 

What did you say? Vi and email? Yes! You can use vi as an email editor. This is most commonly done with the email client mutt

More Vi

This just scratches the surface of what you can do with vi. Here I've tried to show you what might be the most useful features and setting of vi. It should be enough to get some basic work done with this ubiquitous editor for Linux.



Comments: feedback (at) linux.org
Advertising: banners (at) linux.org
Copyright Linux Online Inc.
Compilation ©1994-2008 Linux Online, Inc.
All rights reserved.