Learn Programming

vi editor

Introduction

The vi editor is a text editor. You can use it to create text files such as KornShell scripts.

Starting vi

There are a few different ways to start vi depending on what you want to do. To start it normally just type:

$vi

You will see something like the following on the screen:

|
~
~
~
~
~
~
~
~
~
~
/tmp/vi.J10242: new file: line 1

To open a file for reading type:

$vi filename.txt

If the file does not exist then the name of the file is still used but the file is not written to disk until you tell vi to write it.

Command mode and insert mode

vi has 2 modes called command mode and insert mode. You are in command mode when you start vi. In command mode you give vi commands and in insert mode you type text.

To change to insert mode press either i or the Insert key. You can now type some text. You need to type 3 lines of text for the examples that follow.

After you have typed something you have to change back to command mode by pressing ESC.

Saving

If you want to save the file and you did not give a file name when you started vi you must press : and then type n myfile.txt and press Enter to give your file a name. Make sure you are in command mode when you do this.

Now that you file has a name you must press : and type w and press Enter to write the file to disk.

Copy, Paste, Delete and Undo

All of the following commands need to be typed in command mode.

To copy a line of text press yy. To paste the copied text press p.

To delete a single character press x or the Delete key. To delete an entire line press dd.

If you want to undo the last thing you did press u.

Quitting

To quit vi press : and q. If you want to quit without saving then type q! instead of just q. To quit and save at the same time you must use wq. A quick way to quit and save is to press Shift + z + z.

Learn UNIX tutorial
UNIX console
vi editor
KornShell scripting