Linuxables: Your First Steps with Linux vi

368

 

I want to preface this article by saying, very simply, if you are a programmer, a hard-core vi fan, someone looking to figure out how to make more efficient your vi macros, Linus Torvalds, or Richard M. Stallman – turn away now. Avert your eyes! If you do not, the introductory-level information you will receive here might very well steal, burn, or otherwise permanently damage your soul.

You’ve been warned.

Now, back to our regularly-scheduled Linuxable. 

We continue our look into the Linux text editor by highlighting one of the powerhouses of the bunch – vi. The vi editor came about in 1976 at the hands of Bill Joy. This first release was used for an early BSD UNIX. The name vi is a derivation of the command visual, which was used to switch from the line editor ex to visual mode. So vi = visual. Sort of.

In its very basic sense, vi is much like Nano – only much, much, much more powerful. But we aren’t going to venture into power-user territory. For that you can read any number of the billions upon billions of tomes that cover such topics as buffers, pattern matching, macros, syntax highlighting, ex scripts, and more. What we are going to do is to make it so the new user is able to use vi to edit simple text (hence, Linuxable). Why is this necessary? There are some distributions that do not ship with the more user-friendly text-based editors (such as Nano). And because there are times when it is necessary to make use of the text-based editor, you might find yourself in a situation where vi is the only one available.

Basic Usage

The basic usage of vi is actually quite simple. You just have to know a few key combinations to use it. But even knowing the right key combinations will not help you if you do not understand one very important truth: The vi editor has two modes: editing mode and normal mode.

Normal mode is just for reading. Editing mode is for reading and writing. In other words, when in normal mode you can’t make any changes. What you might not know is that vi starts up in normal mode. So if you open up a text file with vi, unless you switch to editing mode, you can’t do anything to that file. How do you switch? Very simple…you hit the “i” key. Think of the “i” key as standing for “insert.”

So now that you are in editing mode you can make changes to you file. But how do you save your changes? That is the mystery. You can try every key combination you can think of, but none will work…unless…

…unless the correct key combination is preceded with the “:” character.  What the “:” character does is place vi in command mode. Anything typed after this will be considered a command. Now, let’s take a look at some of the more useful commands

Commands

  • w – write (as in save).
  • wq – write and then quit.
  • q – quit without saving (if no changes have been made).
  • q! – quit without saving (if changes have been made).
  • help – a nice help file to read.

Believe it or not, those commands will be just about all you need to get you around vi…for now. At least until you become a power-hungry, uber-geek user, who prides his or her self on never installing GUIs on any of their machines. EVER!

vi Trivia

There is an “improved” version of vi called vi (vi improved). 

If you use vim, you can find a fun Easter egg if you type help 42 (remember you have to be in command mode first). When you hit return, you will see:

What is the meaning of life, the universe and everything?  *42* Douglas Adams, the only person who knew what this question really was about is now dead, unfortunately.  So now you might wonder what the meaning of death is…

I’m sure you get the reference. šŸ˜‰