Tips Linux Explorers   All Things Linux Forum   Great Linux Links   LinuxClues.com   Hometown   Email 



VI ( Revisited )


There has been a Tip a while ago about the vi-editor and emacs, but because of popular demand here is a more elaborate version of the vi Tip, but first a quote from previous Tip:

QUOTE (Bruno @ The Tips 2003)
All configuration files in Linux are written in plain English, easy to read and to adapt. We use a text-editor to write or make changes to those files.
The two most popular, powerful and “difficult” editors that you will find in every distro are Vi and Emacs. They both have “syntax-highlighting” to make writing and editing code easy.

NOTE: Vi and Emacs fans fight bitter religious wars over which is better




The vi-editor . . the exercise
( Vim is an iMproved version of Vi )

Because many people have problems with the vi-editor I will try to set up a lesson-exercise-detailed explanation about the bare basics.

To be able to change most config files that your system depends on, the vi-editor is your best tool, not really simple to learn, but very effective and safe.

Sure other editors like "kedit" are easier to manage, but there are situations where you will have no GUI and Vi is the only one that works without the GUI in text-mode, so you will have to learn how to handle it. ( Yes you could use emacs too, but I prefer Vi )

The biggest problems for novice users are:

1). The difference between "insert-mode" and "command-mode"
With the vi-editor you can do two things, edit text and give commands . . . . to switch between the two modes two keys are important:
The “i” key to put vi in "insert mode" ( modern vi versions will then show the text "INSERT' on the bottom line ) and the Esc key to put the editor back in "command-mode" ( to save the file ). As soon as you hit the Esc key the text "INSERT" on the bottom line will be gone.

2). Correctly saving a file:
If you make a mistake when saving a file, or close vi before the file is saved, you will end up with a swap-file ( mostly marked with the .~ extension ) and the original file. The original one will not have the changes you made and the swap file is useless. Trying to reopen the original one will result in an error message. The best/easiest way to resolve this is to manually delete the .swap ( or .~ ) file. After that you can open the original file again and the error message will be gone.

Resuming:
After opening a file in vi you press the i key, and only then you can start writing/editing. When you are finished writing, you press the Esc key to put vi back in command mode and give the command ZZ to save the file. ( Also the command :wq <Enter> will do the same job, take what you prefer as long as it works. )



EXERCISE:

Now we will do an exercise to be sure you can handle vi as easy as eating French Fries  ( please follow all instructions to the end )

To make a new file called "tessst" you type in a console after the prompt:

CODE
$ vi tessst


You will get an empty consoles screen as vi starts with an empty file. Vi always starts in "command-mode" so to put it in "insert-mode" we have to give the command:

"i"

In most modern versions of vi you will see the text "INSERT" on the bottom line of the console. ( if you´re not sure just hit the i-key again )
Now type:

The quick brown fox etc. etc.

After typing the text you go back to the "command-mode" by pressing the Esc-key.

< Esc >

And you will see the text "INSERT" disappear. ( again if you´re not sure just hit Esc again )
Now hold down the Shift key ( !! not the Ctrl !! ) and give the command ZZ

ZZ ( or :wq < Enter > )

Now vi should close and you should get your prompt back in the console.
We will check if the text we wrote was correctly saved:

CODE
$ cat tessst
( cat is for reading only )

This should show: ¨The quick brown fox¨ line.

Now we will open the same file again:

CODE
$ vi tessst


You see it does not make a new file this time but it opens the existing one ( use a new name, it will open a new file; use an existing name, it will open the existing file )
We put vi in "insert-mode" again:

"i"

and are able to add another line of text:

The stupid dog did not notice the clever fox

Now we save the file again:

< Esc >
ZZ

And again we have a look to see if the changes were saved:

CODE
$ cat tessst


Again we open the file to add a 3rd line:

CODE
$ vi tessst


"i"

I think I´ve got the idea now . .

< Esc >
ZZ

And check the file again:

CODE
$ cat tessst


Now to finish off we are going to have a look ( GUI ) in your home directory . . . there should only be ONE file called "tessst" . . . if there are more of them and even with the extension tessst.swap or tessst.~ you know you made mistakes and will have to do the exercise again !  Delete all the tessst files and start reading again at the top of this post . . .
If you made No mistakes, you passed the test and deserve a pat on the back !

NOTE: Have a look Here how you can tweak vi to your own preferences.


QUOTE (Owyn @ Sep 28 2003)

Rute pointed me to "vimtutor". A worthwhile exercise. Teachs by doing.

I found this to be a usefull help for Vim.



Here is another really good tutorial: First Steps: VI
And a good overview of all the commands: Advanced Vi Cheat Sheet



Bruno


Disclaimer: for those who know their way around vi, I know there is an "Ex-mode" too and 10 pages to fill with additional commands . . . . but I did try to keep things ultra simple for the ones doing their first steps in our all time favorite vi-editor.


-- Oct 2 2003 ( Revised Mar 31 2006 ) --


Tips Linux Explorers   All Things Linux Forum   Great Linux Links   LinuxClues.com   Hometown   Email