VI 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.
Sometimes 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 Open file with vi
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:
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 the file
ESC + w
(w for write)
Closing the file Now, to finish you would type the following:
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:
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.
ESC + u
(u for undo)
Changing your mind (again) Again, you have second thoughts. With vi, there are no regrets.
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
ESC + /word
If you're looking for the word nonplussed in your text (as in: 'vi is so easy I am nonplussed') you would type:
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
First you could use the
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
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:
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 WrappingIf 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.
Vi 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.