Vi Editor On Unix

From ITwiki

Revision as of 11:11, 31 May 2012 by Mforbes (Talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

Contents

Introduction

The VI editor is a screen-based editor used by many UNIX users. The VI editor has powerful features to aid programmers, but many beginning users avoid using VI because the different features overwhelm them. This handout is written to help beginning users get accustomed to using the VI editor, but also contains sections relevant to regular users of VI as well. Examples are provided, and the best way to learn is to try these examples. There's no better way than to experience things yourself.

Conventions

In this document, the following convention will be used:

^X denotes a control character. For example, if you see: ^d in this document, that means you hold down the control key and then type the corresponding letter. For this example, you would hold down the control key and then type d.

Starting the VI Editor

The VI editor lets a user create new files or edit existing files. The command to start the VI editor is vi, followed by the filename.

Example:

to edit a file called myfile, you would type vi myfile at the UNIX prompt and then return. If the file you specified does not exist, the VI editor will tell you that it is a new file, like this: "newfile" [New file]

If you started VI without a filename, the bottom line of the screen will just be blank when VI starts.

The command ^G will show the current status.

Getting Out of VI

Now that you know how to get into VI, the VI editor has two modes and in order to get out of VI, you have to be in command mode. Hit the key labeled Escape or Esc (If your terminal does not have such a key, then try ^[, or control-[.) to get into command mode. If you were already in the command mode when you hit Escape, don't worry. It might beep, but you will still be in the command mode.

The command to quit out of VI is :q. The command to quit out of VI without saving is :q!. Of course, normally in an editor, you would want to save the changes you have made. The command to save the contents of the editor is :w. You can combine the above command with the quit command, or :wq. You can specify a different file name to save to by specifying the name after the :w.

For example:
To save the file you were working as another filename called filename2, you would type:
:w filename2 

and return.

To quit VI editor without saving the changes, type

:q! 

in the command mode.

The Two Modes of VI

VI editor has two modes: command and insert.

The command mode allows the entry of commands to manipulate text. The insert mode puts anything typed on the keyboard into the current file.

VI starts out in command mode. There are several commands that put the VI editor into insert mode. The most commonly used commands to get into insert mode are a and i. Once you are in insert mode, you can get out of it by hitting the escape key.(Hitting escape while you are already in command mode doesn't take the editor out of command mode. It may beep to tell you that you are already in that mode.)

How to Type Commands in Command Mode

The command mode commands are normally in this format: (Optional arguments are given in the brackets)

   [count] command

The commands described in this section are those which are used most commonly in the VI editor.

The count is entered as a number beginning with any character from 1 to 9.

For example:

The x command deletes a character under the cursor. If you type 23x while in command mode, it will

delete 23 characters after the current cursor

Some Simple VI Commands

Here is a simple set of commands to get a beginning VI user started. There are many other convenient commands, which will be discussed in later sections.

a
enter insert mode, the characters typed in will be inserted after the current cursor position.

If you specify a count, all the text that had been inserted will be repeated that many times.

Example:
  • In order to insert the " the", hit key "a" in the command mode, then type "the".
i
enter insert mode, the characters typed in will be inserted before the current cursor

position. If you specify a count, all the text that had been inserted will be repeated that many times.

Example:

In order to insert the sentence " I am learning vi !!" twice, hit the key "I", and type:

       2 I am learning vi!!
h
move the cursor to the left one character position.
j
move the cursor down one line.
k
move the cursor up one line.
r
replace one character under the cursor. Specify count to replace a number of characters
u
undo the last change to the file. Typing u again will re-do the change.
x
delete character under the cursor. Count specifies how many characters to delete. The characters will be deleted after the cursor.

Cutting and Yanking

The command commonly used command for cutting is d. This command deletes text from the file. The command is preceded by an optional count and followed by a movement specification. If you double the command by typing dd, it deletes the current line. Here are some combinations of these:

D
Delete to the end of the line from the current cursor position.
X
Delete the character before the cursor.
x
Delete character under the cursor. A count tells how many characters to delete. The characters will be deleted after the cursor.

Pasting

The commands to paste are p and P. The only differ in the position relative to the cursor where they paste. p pastes the buffer after the cursor position, while P pastes the buffer before the cursor position. Specifying count before the paste command pastes text the specified number of times.

Word and Character Searching

The VI editor has two kinds of searches: string and character. For a string search, the / and ? commands are used. When you start these commands, the command just typed will be shown on the bottom line, where you type the particular string to look for. These two commands differ only in the direction where the search takes place. The / command searches forwards (downwards) in the file, while the ? command searches backwards (upwards) in the file. The n and N commands repeat the previous search command in the same or opposite direction, respectively.

Example:

In order to search the word "the": you want to type the following in the command mode:
/the

Jump To a Specific Place with the File

Example:

In order to jump to the line 20 from current place you can type in the command mode:
20G

Recovering Your Work When Something Goes Wrong with Your Terminal

The VI editor edits a temporary copy of your file, and after the editing is complete, or when you tell it to save, it puts the contents of the temporary copy into the original file. If something goes wrong while you are editing your file, the VI editor will attempt to save whatever work you had in progress, and store it for later recovery. (Note: If VI dies while you were working on any file, it sends you an email message on how to recover it. The -r option stands for recovery. If you were editing the file vitalinfo, and you accidentally got logged out, then the -r option of the 'vi' editor should help. The command would look somewhat like this: vi -r vitalinfo. After using the -r option once, though, you MUST save what you have recovered to the actual file... The -r option only works once per failed VI session.

References

http://www.eng.hawaii.edu/Tutor

You can also check with the man page on UNIX using the command:

man vi
Personal tools