Saturday, November 11, 2006

Vi

What is VI?
A screen based Unix text editor.

Why VI?
Because its basic and it always come with any unix based operating system. In other words its always there!

Warning: Vi can be extremely confusing to use for a beginner but it is very powerful once mastered.

Basic Vi commands
How to get Vi to work?
On the Unix command prompt type
     vi filename
Where filename is the name of the file you want to open or create

There are basically 3 different types of mode in Vi
1) Insert mode --> to get into text mode just hit the i key
2) Command mode --> to get into command mode from text mode just hit the Esc key
3) Visual mode --> to get into this mode hit the v key, this keys allows you to highlight portions of your code


Vi commands
Undo Command
u - undo the last command.

Cursor Positioning Commands
j - Moves cursor down one line, same column.
k - Moves cursor up one line, same column.
h -Moves cursor back one character.
l - Moves cursor forward one character.
$ - Moves cursor to end of current line.
:n - Moves cursor to beginning of line n.

Text Insertion Commands
i - Inserts text before cursor. Terminated by the escape key.
I - Inserts text at the beginning of the line. Terminated by the escape key.
DEL - Overwrites last character during text insertion.
ESC - Stops text insertion.

Text Deletion Commands
x - Deletes current character.
dd - Deletes current line.
dw - Deletes the current word.
d) - Deletes the rest of the current sentence.
D, d$ - Deletes from cursor to end of line.
P - Puts back text from the previous delete.

Cut and Paste Commands
yy - Puts the current line in a buffer. Does not delete the line from its current position.
p - Places the line in the buffer after the current position of the cursor.

Exiting vi
ZZ - Exits vi and saves changes.
:wq - Writes changes to current file and quits edit session.
:q! - Quits edit session (no changes made).

Code folding
-
Highlight the block of code you want to fold first, once highlighted you can hit the following key combination:
zc - creates the fold
zo - opens fold
zd - deletes fold

Split screen
To split a screen type this command in vi :sp where filename is the other file you want to open. If you just type :sp it will split the screen with the current file you are in

For vertical split, type :vs this splits it vertically

To navigate through the splits first hit Ctrl-W and then the arrow key to the direction of the split

No comments: