The vi Editor


The vi Editor

The vi editor is by far one of the most used editors in the Unix community today. The vi editor has been around for a long time and continues to find new fans at an increasing rate. It's a streamlined, highly functional tool that does not require much memory. It can be said that the vi editor is Unix's most universal editor. Pronounced vee-eye, vi isn't a user-friendly editor. In fact, one of the hardest things to learn and master in Unix is file editing with the vi editor. The power of the vi editor comes from its low overhead and high functionality.

To use the vi editor, you only need to open it up using the vi command. To see the vi editor in action, do the following:

1.

Select a file you want to edit with the vi editor.

2.

Issue the vi command as vi <filename>.

Create Files with Ease Using vi! You can open the vi editor without specifying a file you want to edit. This will then create a new file for you that once saved, will show you that the vi editor is a good tool to use.


To see the vi editor in action editing a preexisting file, type the following command:

 > vi /etc/HTMLLAB/index.htm 

This will open up the vi editor and the file opened will be the website's home page, index.htm. You may not have this same file; if not, use the find command to locate a file you would like to edit or view with the vi editor and open it.

Now that you have a file opened, you can use a plethora of commands to edit the file. If you do have an HTML document open, you need to know how to edit HTML code to create changes on a website. If you have a file open, you may be able to read a help file for a specific application installed on your system. No matter what you choose, you will find the vi editor has a wide array of commands that can be used within it to work with the open files. One thing you have to consider is what mode the vi editor is working in. There are two modes that the vi editor operates in. The vi editor either uses command mode or insert mode.

  • Command mode You can control things such as cursor position, deleting characters, and saving files.

  • Insert mode You can insert characters.

Now that you know how vi operates, let's use it. To open a file is easy, but to actually edit it and then save it is a whole different set of tasks that we need to learn and master. You have to know how to edit files and that comes from mastering the vi editor's basic operation. Mastering the vi editor comes only from mastering the keyboard shortcuts used to operate the vi editor. Table 8.1 shows the most common manipulation keyboard shortcuts you will use.

Table 8.1. Common vi Keyboard Actions

Mode

Key(s)/Key Combination(s)

Action

Command

l

Move right

 

h

Move left

 

j

Move to the next line

 

k

Move to the previous line

 

Put cursor on the character to delete and then press the x key

Delete a character

 

Press the d key twice

Delete an entire line (including to delete an empty line)

 

Position cursor on the line to append and press A

Append the end of a line

 

i (before the character under the cursor) or a (after the character under the cursor)

Changes to insert mode

 

:w Return

Save the file

 

:w<filename>

Save the file to a new name

 

:q Return

To exit vi

 

:q! Return

Quit without saving

Insert mode

Esc key

Changes to command mode

 

Backspace and Delete keys

Backspaces or deletes, but only for data just inserted


This is not a complete list; we could probably fill this little pocket guide full of 10 minute lessons with what you can do with the vi editor from the keyboard. Perhaps it would be beneficial to visit your local library and take out a book on the vi editor to learn more ways to make it work for you, not against you.

Remember that this book can only cover so much so it is important that you be careful editing files and stepping beyond the basics in the current Unix environment you are in. Set up a test lab if possible and explore from there; you will find it easier to learn and work that way.

This is an example of how you can use the vi editor to edit a file named test. The two commands you need to perform this exercise are Return, which will move to a new line of text, and Esc, which is how you escape. Now, make a new file called test:

 > vi test This is a new file I created! Check it out, Isn't it snazzy? I thought it was 

By doing this, Unix will respond back to you with

 "test" [New file] 4 lines, 61 characters 

If you remember how to use the cat command, you can quickly view the contents of the new file you created call test.

 > cat test This is a new file I created! Check it out, Isn't it snazzy? I thought it was 

As you can see, the vi editor can be helpful to you for editing a plethora of different file types. Make sure you remember that learning the vi editor takes a lot of time and practice. I hope that you continue to use it; if not, then maybe you might like to try emacs.



    SAMS Teach Yourself Unix in 10 Minutes
    Sams Teach Yourself Unix in 10 Minutes (2nd Edition)
    ISBN: 0672327643
    EAN: 2147483647
    Year: 2005
    Pages: 170

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net