Thursday, 12 July 2007

vi Tutorial - Part I -

I. Getting into Vi

You can start using VI or creating a new file , by the below command:

vi filename RETURN

Ex: vi config.txt

If the file is new, vi displays a column of ~ (tildes) in the first column. The blinking cursor is placed at the beginning of the file.


II. Typing In Your Text

VI has two modes

i) Insert mode

ii) Command mode

Insert Mode is the mode that you use to write/type characters to the file, and VI does not interact with what you type and just added it to the file buffer. When you are in "command mode" the keys you press cause actions like moving from place to place in your file and making changes to what you have typed. Please note that when you start vi you will be in "command mode" by default.


III. What the $!#^*& happened??!!

Undo commands:

  • u — undo last command ONLY
  • U — undo all changes to the current line you are working on

IV. Inserting Text

VI incorporates several methods of inserting text into a document. There are three different methods you can use while you are in COMMAND mode. Typing an 'i' (for INSERT mode) keystroke allows you to insert characters into the document at the left edge of the cursor. As mentioned above, typing the (or escape) keystroke allows you to exit (or escape) from INSERT mode back into COMMAND mode. Since there are many different places where a person might want to insert text, there are keystrokes other than 'i', that place the editor directly into INSERT mode, after moving the cursor. These are outlined below.

Insert commands - These are the only commands which will allow you to enter text before the BEGINNING of a line.

i

Inserts text to the left of the cursor.

I

Inserts text at the beginning of the line, no matter where the cursor is positioned on the current line.

Append commands - These are the only commands which will allow you to enter text AFTER the END of a line.

a

Begins inserting after the character (append) on which the cursor is positioned.

A

Begins inserting at the end of the current line, no matter where the cursor is positioned on that line.

Open commands

o

Begins inserting text on a new, empty line that is opened for you, below the current line. This is the only command that will allow you to insert text BELOW the LAST line of the file.

O

Begins inserting text on a new, empty line that is opened for you, above the current line. This is the only command that will allow you to insert text ABOVE the FIRST line of the file.

There are other commands that place you in INSERT mode. These commands are used to perform substitutions of text. That is, the deletion of old text and the insertion of new text, all in a single operation. These commands will be discussed later because the are actually variations on the change command.

1 comment:

BISO said...

Thanks a lot Samer. I can't wait for part 2

FEEDJIT Live Traffic Feed