Command Mode
Open File
· vi filename - edit filename starting at line 1
·
vi –r filename -
recover filename that was being edited when system crashed
Exit File
·
:q - To exit Vi
·
:wq or :x - To exit Vi after saving the edited file
·
:q! -
To exit Vi without saving the edited file
Move the cursor
·
j - ↓ one line
·
k - ↑ one line
·
h - ← one character
·
l - → one character
·
0 - beginning of
current line
·
$ - end of
current line
·
w - beginning of
next word
·
b - beginning of
previous word
·
nG (1G, 10G..) - beginning of nth
line
·
G - beginning of
last line
·
H - Top of the
screen
·
M - Middle of the
screen
·
L - Bottom of
the screen
Screen Manipulation
·
^f - ↓
(forward) one screen
·
^b - ↑(backward)
one screen
·
^d - ↓(down)
half screen
·
^u - ↑(up)half
screen
·
^l - redraw the
screen
·
^r - redraw the
screen removing deleted lines
Searching Text
·
/string -
Search forward occurrence of string from
the current position
·
?string -
Search backward occurrence of string from
the current position
·
n - Move to next
occurrence
·
N - Move to next
occurrence in opposite direction
Searching and Replacing Text(Substitute)
Search for the word ‘find’ and
replace it with the word ‘new’
·
:s/find/new/ - current line only
·
:1,10s/find/new/- line 1 to 10 only
·
:1,.s/find/new/ - line 1 to current line
·
:1,$s/find/new/ - line 1 to last line (entire file)
·
:-5,+5s/find/new/ - 5lines beyond the current line to 5
lines after current line
Determining line numbers
·
:.= - Line number of
current line
·
:= - Total number
of Lines
Saving and Reading Files
·
:r test - Read file named test and insert after the current line
·
:w - Write the
current content of the file to the original file
·
:w nwfilename - Write the current content of the file to a new file called nwfilename
·
:4,9w newfile -
Write the content from line 4 -9 to a new file named newfile
·
:w! existfile -
Write the current contents over a existing file named existfile
Manipulating Text
Undo the last edition action
·
u - Undo the last
action
Inserting or adding text
·
i -
Insert text before cursor
·
I -
Insert text at the end of the line
·
a - Append text after the cursor
·
A - Append text at the beginning of line
·
o - Put a new line below current line
·
O - Put a new line above current line
changing text
·
r - Replaces the
character under cursor – no esc needed
·
R - Replace the
characters starting from the current position- till esc
·
cw - Change the
current word starting from the current position – till esc
·
cNw - Change the N words starting with the current position – till esc
·
C - Replace the
content in the current line after the current position
·
cc - Replace the
entire line
·
Ncc - Replace next N
lines starting with the current line
changing text
·
x - Delete a single character under cursor
·
Nx - Delete N characters starting from the cursor
·
dw - Delete the content of the current word after the cursor
·
dNw - Delete the content of the next N words starting after the cursor
·
D - Delete the remainder of the current line after the cursor
·
dd - delete entire current line
·
Ndd - delete N lines, beginning with the current line
Copying and pasting text
·
yy - Copy the
current line
·
Nyy - Copy the next N
lines including the current line
·
p - Paste the
copied lines after the current line
·
P - Paste the
copied lines before the current line