HOME

Introducing VI

Vi Commands Used To Edit Files Positioning The Cursor
Appending Commands
Deleting Text
Changing Text
Insert mode
Copying and Pasting Text
Saving Changes and Quiting
Displaying and Hiding Line#
Searching and Replacing
Refreshing The Screen And Including Files

CommandMeaning
vi File NameOpen or Create File
viOpen New File To Be Named
vi -r File NameRecover Crashed File
view File NameOpen File Read-Only
vipwEdit the /etc/passwd file, setting locks and doing appropriate processing.
There are three modes of operation:Command Mode
Insert Mode
Last Line Mode

Vi Commands Used To Edit Files Positioning The Cursor

CommandMeaning
hMove left one character
jMove down one line
kMove up one line
lMove right one character
wMove right one word
WMove right one word (Past Punctuation)
bMove left one word
BMove left one word (Past Punctuation)
eMove to end of current word
ReturnMove down one line
BackspaceMove left one character
HMove to top of screen
MMove to middle of screen
LMove to bottom on screen
Control-fScroll forward one screen
Control-dScroll forward one-half screen
Control-bScroll backward one-half screen
GGo to last line of file
1GGo to first line of file
21GGo to 21st line of file

Appending Commands

CommandMeaning
aAppend text after cursor
AAppend text at line end
iInsert text before cursor
IInsert text at beginning of line
oInsert line below cursor
OInsert line above cursor

Deleting Text

CommandMeaning
xDelete Character at the cursor
XDelete character to the left of the cursor
dwDelete word (or part of word to right of cursor)
ddDelete line containing the cursor
DDelete part of line to right of cursor
dGDelete to end of file
d1GDelete from beginning of file to cursor
:5,10dDelete lines 5 through 10

Changing Text

CommandMeaning
CWChange word (or part of word) to right of cursor
ccChange line
cChange from cursor to end of line
sSubstitute string for characters from cursor forward
rReplace character at cursor with one other character
r ReturnBreak line

Insert Mode

CommandMeaning
JJoin current line and line below
xpTranspose character at cursor and character to the right
~Change case of letter (upper or lower )
u or :uUndo previous command
Undo all changes to cuurent line

Copying and Pasting

CommandMeaning
yYank or copy line
YYank or copy line
pPut yanked or deleted line below current
PSame as above

Saving Changes and Quitting

CommandMeaning
:wSave changes (write buffer)
:w new fileWrite buffer to named file
:wqSave changes and quit Vi
:q!Quit without saving changes

Displaying and Hiding Line#

The SET command is used from last-line mode to control these options, as indicated in the table.
CommandMeaning
:set nuShow line numbers
:set nonuHide line numbers; setting is case sensitive
:set icSearches should ignore case
:set noicSearches should be case sensitive
:set listDisplay invisible characters

Search and Replace

CommandMeaning
/stringSearch for string
?stringSearch backward for string
nFind next occurrence of string in search direction
NFind previous occurrence of string in search direction
:%s/old/new/gSearch and replace

Refresh the Screen and Including Files

CommandMeaning
Control-1Clear (refresh) scrambled screen
:r file nameInsert (read) file after cursor
:34r file nameInsert file after line 34