Vi

From OCFwiki

(Redirected from Vim)
Jump to: navigation, search

vi or vim is one of the most popular text editors on UNIX systems. While it is not the most intuitive text editor available, because vi is available on almost all UNIX systems, it is useful to know how to use it.

Contents

Introduction

Vi is useful for creating new text, revising an existing text, and appending text to an existing file. Although there are many commands available in vi, you will be introduced only to the commands for entering and exiting vi, moving the cursor, and inserting and deleting text. You must refer to a Unix manual for other commands.

The two modes of vi

The vi text editor has two distinct modes of operation, the edit mode and command mode.

The edit mode is the mode in which you type text. Each character appears on the screen as you type it.

In the command mode, it is impossible to type text. The command mode allows you to move the cursor around the text and to perform functions, such as deleting text and saving the file.

Entering the vi text editor

To create a new document in vi , simply type,

vi file_name

This command will display the vi editor screen accompanied by the name of the new document followed by the statement, "[New file]".

Example

After typing the following at the console,

% vi practice 

the new file practice is created, and the screen will look like this:

 =============================================================
 ~
 ~
 ~
 ~
 ~
 ~
 ~
 ~
 ~
 ~
 ~
 ~
 ~
 ~
 ~
 ~
 ~
 "practice" [New File]
 =============================================================

The Edit Mode

Once in vi, you are placed in the command mode. You cannot enter text in this mode. If you try, the error bell rings and the letters you try to type will not appear on the screen. This is because ordinary letters are interpreted as commands.

You must enter the edit mode to type text.

To do so, use one of the following commands. The vi text editor is case sensitive, which means the same letters in upper and lower case have different command responses.

The letters a, A, i, I, o, and O place vi into edit mode.

  • the a command moves the cursor to the right one space
  • the A command moves cursor to the end of the current line
  • the i command inserts text in front of the cursor
  • the I command moves cursor to beginning of the current line.

To correct mistakes while in the edit mode, use the backspace key to correct errors in the current line you are typing. As you backspace to correct the mistake, characters remain on the screen although they are erased in the computer's memory. This looks confusing but don't be worried by it. To correct mistakes in lines other then the current line, you must first enter the vi command mode. This will allow you to move the cursor to the line you want to edit (see Deleting Text.)

Leaving the Edit Mode and Moving the Cursor

To return to the command mode simply press the ESC key. (Hint: if you are not sure which mode you are currently in, press the ESC anyway. If you were already in command mode, you will stay in the command mode.) In the command mode, it is possible to move the cursor up, down, left or, right by using the arrow keys on the keyboard. However, no matter what type of keyboard you have, it is possible to use the vi commands, h, j, k, and l, all in lowercase, to control the cursor. These commands move the cursor as follows:

  • the j command moves the cursor up one line
  • the k command moves the cursor down one line
  • the h command moves the cursor backwards on a line, similar to the backspace key
  • the l command moves the cursor forward on a line, similar to the spacebar.

If you want to insert text, simply move the cursor to the desired area, while in the command mode, return to edit mode and type the text.

Deleting Text

It is only possible to delete text while in command mode. (See The Edit Mode for entering command mode.)

  • the dw command deletes the word the cursor is on.
  • the dt command deletes everything from the cursor to the next period on the current line. This is a good way to delete words to the end of the sentence without deleting the period.
  • the d( command deletes everything from the cursor to the beginning of the next sentence. After moving the cursor to the beginning of a sentence, you can use this command to quickly delete that sentence.
  • the dG command deletes everything from the cursor to the end of the file

Editing an Existing File

It is also possible to edit an existing file. In this case, enter by typing vi followed by the name of the file to be edited.

Example

% vi letter

In this case the user entered vi and opened an existing file entitled, "letter". The text of the file requested appears on the screen. At the bottom of the screen the file name, number of lines in the text, and number of characters in the text appear, as below, for example:

=============================================================
To whom it may concern,
I am interested in signing up for the data communication class that
the school of design is offering in the fall.  I am a student of the
colleges and need this class to fulfill my major.  If you could send
me the application form, I would greatly appreciate it.  It would be
best to send the form to me through the internet in a email message. 
My address is as follows

Jack Straw
576 St. Paul Blvd.
New York, NY 10021
~
~
~
~
~
~
~
~"letter"  10 lines, 387 characters
=============================================================

Saving Your Work and Exiting the vi Text Editor

To save your text, first enter the command mode. Once in command mode, it is possible to save and exit, or to exit without saving. Enter following commands:

  • To save your text,
:w 
  • To quit vi,
:q
  • To save and quit in one command,
:wq
  • To exit without saving,
:q!

These commands are examples of what are called last line commands. When they are used, the cursor moves to the bottom of the page. Your commands and the systems response are displayed there.

Conclusion

There are many other commands for the vi editor. Entire books have been written explaining the use of vi commands. The purpose of this brief guide is to introduce you to enough basic vi commands for you to do useful work. To learn about more advanced features, refer to a Unix manual or a book on vi.

See also

Personal tools