Menu

UNIX TUTORIALS - Unix - The vi Editor

Unix - The vi Editor

ADVERTISEMENTS

Starting the vi Editor:

CommandDescription
vi filenameCreates a new file if it already does not exist, otherwise opens existing file.
vi -R filenameOpens an existing file in read only mode.
view filenameOpens an existing file in read only mode.

ADVERTISEMENTS

Moving within a File:

CommandDescription
kMoves the cursor up one line.
jMoves the cursor down one line.
hMoves the cursor to the left one character position.
lMoves the cursor to the right one character position.

ADVERTISEMENTS

CommandDescription
0 or |Positions cursor at beginning of line.
$Positions cursor at end of line.
wPositions cursor to the next word.
bPositions cursor to previous word.
(Positions cursor to beginning of current sentence.
)Positions cursor to beginning of next sentence.
EMove to the end of Blank delimited word
{Move a paragraph back
}Move a paragraph forward
[[Move a section back
]]Move a section forward
n|Moves to the column n in the current line
1GMove to the first line of the file
GMove to the last line of the file
nGMove to nth line of the file
:nMove to nth line of the file
fcMove forward to c
FcMove back to c
HMove to top of screen
nHMoves to nth line from the top of the screen
MMove to middle of screen
LMove to botton of screen
nLMoves to nth line from the bottom of the screen
:xColon followed by a number would position the cursor on line number represented by x

Control Commands:

CommandDescription
CTRL+dMove forward 1/2 screen
CTRL+dMove forward 1/2 screen
CTRL+fMove forward one full screen
CTRL+uMove backward 1/2 screen
CTRL+bMove backward one full screen
CTRL+eMoves screen up one line
CTRL+yMoves screen down one line
CTRL+uMoves screen up 1/2 page
CTRL+dMoves screen down 1/2 page
CTRL+bMoves screen up one page
CTRL+fMoves screen down one page
CTRL+IRedraws screen

Editing Files:

CommandDescription
iInserts text before current cursor location.
IInserts text at beginning of current line.
aInserts text after current cursor location.
AInserts text at end of current line.
oCreates a new line for text entry below cursor location.
OCreates a new line for text entry above cursor location.

Deleting Characters:

CommandDescription
xDeletes the character under the cursor location.
XDeletes the character before the cursor location.
dwDeletes from the current cursor location to the next word.
d^Deletes from current cursor position to the beginning of the line.
d$Deletes from current cursor position to the end of the line.
DDeletes from the cursor position to the end of the current line.
ddDeletes the line the cursor is on.

Change Commands:

CommandDescription
ccRemoves contents of the line, leaving you in insert mode.
cwChanges the word the cursor is on from the cursor to the lowercase w end of the word.
rReplaces the character under the cursor. vi returns to command mode after the replacement is entered.
ROverwrites multiple characters beginning with the character currently under the cursor. You must use Esc to stop the overwriting.
sReplaces the current character with the character you type. Afterward, you are left in insert mode.
SDeletes the line the cursor is on and replaces with new text. After the new text is entered, vi remains in insert mode.

Copy and Past Commands:

CommandDescription
yyCopies the current line.
ywCopies the current word from the character the lowercase w cursor is on until the end of the word.
pPuts the copied text after the cursor.
PPuts the yanked text before the cursor.

Advanced Commands:

CommandDescription
JJoin the current line with the next one. A count joins that many lines.
<<Shifts the current line to the left by one shift width.
>>Shifts the current line to the right by one shift width.
~Switch the case of the character under the cursor.
^GPress CNTRL and G keys at the same time to show the current filename and the status.
URestore the current line to the state it was in before the cursor entered the line.
uUndo the last change to the file. Typing 'u' again will re-do the change.
JJoin the current line with the next one. A count joins that many lines.
:fDisplays current position in the file in % and file name, total number of file.
:f filenameRenames current file to filename.
:w filenameWrite to file filename.
:e filenameOpens another file with filename.
:cd dirnameChanges current working directory to dirname.
:e #Use to toggle between two opened files.
:nIn case you open multiple files using vi, use :n to go to next file in the series.
:pIn case you open multiple files using vi, use :p to go to previous file in the series.
:NIn case you open multiple files using vi, use :N to go to previous file in the series.
:r fileReads file and inserts it after current line
:nr fileReads file and inserts it after line n.

Word and Character Searching:

CharacterDescription
^Search at the beginning of the line. (Use at the beginning of a search expression.)
.Matches a single character.
*Matches zero or more of the previous character.
$End of the line (Use at the end of the search expression.)
[Starts a set of matching, or non-matching expressions.
<Put in an expression escaped with the backslash to find the ending or beginning of a word.
>See the '<' character description above.

Set Commands:

CommandDescription
:set icIgnores case when searching
:set aiSets autoindent
:set noaiTo unset autoindent.
:set nuDisplays lines with line numbers on the left side.
:set swSets the width of a software tabstop. For example you would set a shift width of 4 with this command: :set sw=4
:set wsIf wrapscan is set, if the word is not found at the bottom of the file, it will try to search for it at the beginning.
:set wmIf this option has a value greater than zero, the editor will automatically "word wrap". For example, to set the wrap margin to two characters, you would type this: :set wm=2
:set roChanges file type to "read only"
:set termPrints terminal type
:set bfDiscards control characters from input

Starting the vi Editor:

$vi testfile

|
~
~
~
~
~
~
~
~
~
~
~
~
"testfile" [New File]    

Replacing Text:

:s/search/replace/g