Monday, October 8, 2007

VI Editor Tips and Tricks

Here are some VI and Ex commands, Will try and cover most commands that are important and a couple of new and funky ones.This page is categorised as:

  • VI Options
  • VI Moving around
  • VI Basic Editing
  • VI Pattern Matching
  • Ex Commands
  • Some Cool Things

1. VI options
A VI editor is a screen editor (Secretly married to another editor Ex, Both these editors are virtually indistinguishable, Ex is a line editor though, just for information, I wont dig too much details though, i want to keep this material short and smart).

A VI editor can be invoked in following ways:

Open an existing file to edit or create a new file
vi filename

Open an existing file to edit at the last line
vi + filename

Open an existing file to edit at the nth line
vi +n filename

Open an existing file to edit at the pattern
vi +/pattern filename
2. VI Moving Around
Its quite hard to categorize VI commands as one always needs to come before the other, But i will still try and stick to the categories, whenever you need something jump to appropriate section and look up for a command, if you think there something missing here, feel free to add it in here.

To move to the COMMAND mode:

esc

ARROW Keys:

move left
h
move down
j
move up
k
move right
l

Move by WORD:

Move to the start of next word
w
Move to the start of previous word
b
Move to the start of nth word forward from current cursor position
nw
Move to the start of nth word backword from current cursor position
nb

Move by SENTENCE:

Move to the start of next sentence
)
Move to the start of previous sentence
(
Move to the start of nth sentence forward from current cursor position
n)
Move to the start of nth sentence backword from current cursor position
n(

Move by PARAGRAPH:

Move to the start of next paragraph
}
Move to the start of previous sentence
{
Move to the start of nth sentence forward from current cursor position
n}
Move to the start of nth sentence backword from current cursor position
n{

MORE Ways:

Move to the beginning of the line:
0
Move to the End of the line:
$
Move to the first non-white space charecter in the line:
^
Move the cursor to the top of the screen:
H
Move the cursor to middle for the screen:
M
Move the cursor to the End of the Screen:
L
Go to the bottom of the page:
ctrl - G

SCROLLING:

Scrolling back one window (Remember B for back):
Ctrl - B
Scrolling back half window (Remember U for up):
Ctrl - U
Scrolling forward one window (Remember F for forward):
Ctrl - F
Scrolling back half window (Remember D for down):
Ctrl - D

3. VI Basic Editing

VI comes with a rich set of commands for editing, indenting, altering, creating files, I might have left out a lot of commands from here, especially ones from VIM (VI improved), honestly this is so because my experience is mostly on VI and not on VIM, so if anyone wants to add to this list, feel free to do so.

Basic stuff:

Insert text under the current cursor position:
i
Insert text into the beginning of the current line:
I
Append text after the current cursor position:
a
Append text at the end of the current line:
A
Open a new line under the current line:
o
Open a new line above the current line:
O

Cut, Copy and Paste:

Yank the whole line:
yy
Yank a word:
yw
Yank n lines:
nyy
Yank n words:
nyw
Delete the charecter under the cursor, store in in the general buffer:
x
Delete n charecters under the cursor:
nx
Delete a whole line and store it in general buffer:
dd
Delete n lines:
ndd
Delete words:
dw
Delete n words:
ndw
Delete from the current cursor position to the end of line:
D
Delete charecter before current cursor position:
X
Paste the contents of the the buffer after the current cursor position:
p
Paste the contents of the buffer before the current cursor position:
P

A note on VI Buffers:


VI buffers can visualised to function similar to MS Word's clipboard, understanding clipboard is a key to effective use of VI and get rid of common repetitive typing, VI has 26 buffers, great!! but what are they? They are like little memmory units where you can store chunks of your text. Look at this like, there's one buffer for each alphabet in English language so each buffer is name a,b,c,d ...so on until z.


yeah, so this would mean you can store 26 chunks of text at anytime and paste it anywhere you want, now thats some advanced stuff that the normal editors cannot handle, and one of the reason why VI is so popular, its light, fast and functional, a little weird though but totally lovable.


so if you want to store some three lines of code somewhere then go to command mode type something like this

"a2yy

so this would mean 2 lines of text is stored in buffer a, in order to get it back you can simply use

"ap

Thats simple ... huh??

So any of the following commands should work to store pieces of data in a buffer.

Store two words in the buffer b
"b2yw


Store all the text from current cursor position to the end of the line in buffer c
"cD


Okay thats it about VI buffers, if you wanna add something here, feel free to add it and if you have any questions or need more clarity then ask me to add that information in here.


4. Ex Commands


The following are Ex commands that you may use in VI Editor

Quit VI
:q


Quit without saving
:q!

Save the file:
:w


Save and Quit:
:wq


Write to the filename specified:
:w filename


The good old find and replace:
:(range)s/pattern/replace/options

Show number of the lines:
:set nu


AUto-indent the code:
:set ai


Number of lines per window:
:set wi=n


Tab to space:
:set tabstop=n


Set as Read-Only:
:set ro


Show current line number:
:.=


Show total number of lines:
:=


Copy and paste:
:(range) co (destination)

Delete:
:(range) d

Substitute:
: (range) s///options (count)




1 comment:

Anonymous said...

Interesting to know.