Wednesday, May 28, 2008

Vi Command in Unix.

A)Entering vi
---------------------------

vi filename - The filename can be the name of an existing file or the name of the file you want to create.

view filename - Starts vi in "read only" mode. Allows you to look at a file without the risk of altering its contents.

B)Exiting vi
-----------------------

:q - quit - if you have made any changes, vi will warn you of this, and you'll need to use one of the other quits.
:w - write edit buffer to disk
:w filename - write edit buffer to disk as filename
:wq - write edit buffer to disk and quit
ZZ - write edit buffer to disk and quit
:q! - quit without writing edit buffer to disk

C)Positioning within text
-----------------------------------------
By character

left arrow - left one character
right arrow - right one character
backspace - left one character
space - right one character
h - left one character
l - right one character

By word
w - beginning of next word
nw - beginning of nth next word
b - back to previous word
nb - back to nth previous word
e - end of next word
ne - end of nth next word

By line

down arrow - down one line
up arrow - up one line
j - down one line
k - up one line
+ - beginning of next line down
- - beginning of previous line up
0 - first column of current line (zero)
^ - first character of current line
$ - last character of current line

By block
( - beginning of sentence
) - end of sentence
{ - beginning of paragraph
} - end of paragraph

By screen
CTRL-f - forward 1 screen
CTRL-b - backward 1 screen
CTRL-d - down 1/2 screen
CTRL-u - up 1/2 screen
H - top line on screen
M - mid-screen
L - last line on screen

Within file

nG - line n within file
1G - first line in file
G - last line in file

D)Inserting text
--------------------------------

a - append text after cursor *
A - append text at end of line *
i - insert text before cursor *
I - insert text at beginning of line *
o - open a blank line after the current line for text input *
O - open a blank line before the current line for text input *

* Note: hit ESC (escape) key when finished inserting!

E)Deleting text
-----------------------------

x - delete character at cursor
dh - delete character before cursor
nx - delete n characters at cursor
dw - delete next word
db - delete previous word
dnw - delete n words from cursor
dnb - delete n words before cursor
d0 - delete to beginning of line
d$ - delete to end of line
D - delete to end of line
dd - delete current line
d( - delete to beginning of sentence
d) - delete to end of sentence
d{ - delete to beginning of paragraph
d} - delete to end of paragraph
ndd - delete n lines (start at current line)

F)Changing text
--------------------------------

cw - replace word with text *
cc - replace line with text *
c0 - change to beginning of line *
c$ - change to end of line *
C - change to end of line *
c( - change to beginning of sentence *
c) - change to end of sentence *
c{ - change to beginning of paragraph *
c} - change to end of paragraph *
r - overtype only 1 character
R - overtype text until ESC is hit *
J - join two lines

* Note: hit ESC (escape) key when finished changing!

G)Copying lines
-----------------------------

yy - "yank": copy 1 line into buffer
nyy - "yank": copy n lines into buffer
p - put contents of buffer after current line
P - put contents of buffer before current line

H)Moving lines (cutting and pasting)
----------------------------------------------

ndd - delete n lines (placed in buffer)
p - put contents of buffer after current line
P - put contents of buffer before current line

I)Searching / Substituting
---------------------------------------

/str - search forward for str
?str - search backward for str
n - find next occurrence of current string
N - repeat previous search in reverse direction

The substitution command requires a line range specification. If it is omitted, the default is the current line only. The examples below show how to specify line ranges.

:s/old/new - substitute new for first occurrence of old in current line
:s/old/new/g - substitute new for all occurrences of old in current line
:1,10s/old/new - substitute new for first occurrence of old in lines 1 - 10
:.,$s/old/new - substitute new for first occurrence of old in remainder of file
:.,+5s/old/new - substitute new for first occurrence of old in current line and next 5 lines
:.,-5s/old/new - substitute new for first occurrence of old in current line and previous 5 lines
:%s/old/new/g - substitute new for all occurrences of old in the entire file
:%s/old/new/gc - interactively substitute new for all occurrences of old - will prompt for y/n response for each substitution.

J)Miscellaneous commands
-----------------------------------

u - undo the last command (including undo)
. - repeat last command
xp - swap two adjacent characters
m[a-z] - set a marker (a - z)
'[a-z] - go to a previously set marker (a - z)
:!command - execute specified UNIX command
:r filename - read/insert contents of filename after current line.
:1,100!fmt - reformat the first 100 lines
:!fmt - reformat the entire file

5 comments:

  1. i am really impressed by the class of matrial found on your website.
    keep the good work going.

    Kashif.
    Markham Ontario.

    ReplyDelete
  2. Hi
    This article is very good. Very nice and good for newbiees too

    Regards
    MADAN KUMAR T

    ReplyDelete
  3. This article is really helpful. Thanks a lot!!

    ReplyDelete
  4. Amazing! All vi commands in one posts. I bookmarked it and while I typing any vi command, I just open it. Typing becomes so easy :)

    ReplyDelete
  5. Best article to learn VI editor

    ReplyDelete