vi Editor Cheat sheet

Page Contents


Opening File
Saving & Quitting
Switching to Shell & Run Commands
Modes – Command, Insert/Append & Exit
Deleting Text
Moving Cursor in file
Moving on Screen
Moving on Screen using Ctrl key
Go to desired line by line number
Changing Text
Copying, Pasting and Moving lines
Show/Hide Line Numbers
Searching – Ignore/Set case
Search and Replace

Opening File


CommandPurpose
vi Open new file to be named later
vi file_name Open an existing file or create a new file
vi -r file_name Recover crashed file
view filenameOpen file in read-only mode

Savings & Quitting


CommandPurpose
:qQuit
:q!Quit without saving the changes
:wSave
:wq or :wq!Save and Quit
:w filen_nameSave to file “file_name”
:e! Discard unsaved changes, and reopen the file.
:shSwitch to shell temporarily, and return back using Ctrl+d

Switching to Shell & Run Commands


CommandPurpose
:shSwitch to shell temporarily, and return back using Ctrl+d
:!cmd1 Execute the command cmd1

Modes – Command, Insert/Append & Exit


CommandPurpose
iInsert characters left of cursor
IInsert characters beginning of line
aInsert characters right of cursor
AInsert characters end of line
oInsert line below cursor
OInsert line above cursor
EscInsert Mode to Command Mode
Exit Mode to Command Mode

Deleting Text


CommandPurpose
xDelete character at the cursor
XDelete character to the left of the cursor
dwDelete word or part of word, to right of cursor
ddDelete current line
DDelete part of line to right of cursor
dG Delete to end of file
d1G Delete – beginning of file to cursor
:1,5d Delete lines 1 to 5

CommandPurpose
hMove one character left
jMove one line down
kMove one line up
lMove one character to right
wMove one word right
WMove one word (past punctuation) right
bMove one word left
BMove one word (past punctuation) left
e Move to the end of the current word

Navigation – Moving on Screen


CommandPurpose
H Move to top of screen (1st line)
M Move to middle of screen
L Move to bottom of screen (last line on screen)

Navigation – Moving on Screen using Ctrl key


CommandPurpose
Ctrl+f Page forward one screen forward
Ctrl+d Scroll forward one half screen
Ctrl+b Page backward one screen
Ctrl+u Scroll backward one-half screen

Navigation – Go to desired line by line number


CommandPurpose
GGo to the last line
1GGo to the 1st line
24GGo to the 24th line

Changing Text


CommandPurpose
ccChange line
cwChange characters in a word or entire word right of cursor
CChange from cursor to end of line
sSubstitute string for character(s) from cursor forward
rReplace character with other character at cursor
RReplace characters from the cursor position
r ReturnBreak line at cursor
JJoin to current line line below
xpTranspose character cursor and character to right
~Change case of letter to uppercase or lowercase at cursor
uUndo previous command
UUndo all changes to current line

Copying, Pasting and Moving lines


CommandPurpose
yyYank or copy current line
5yyCopy 5 lines
YYank or copy line
pPut yanked or deleted line below current line
PPut yanked or deleted line above current line
:1,5 co 9 Copy lines 1-5 and put after line 9
:2,5 m 9 Move lines 4-5 and put after line 9

Show/Hide Line Numbers


CommandPurpose
:set nuShow line numbers for all lines
:set nonuHide numbers for all line

Searching – Ignore/Set case


CommandPurpose
:set icIgnore case while searching
:set noicCase sensitive searching

Search and Replace


CommandPurpose
/stringSearch for string anywhere in file
?stringstring Search backward for string
n Find next occurrence of string in search direction
N Find previous occurrence of string in search direction
:s/old/new/ Replace the first occurrence of the word “old” with “new” on the current line
:s/old/new/gReplace all occurrences of the word “old” with “new” on the current line.
:.,+50s/old/new/ Replace all occurrence of the word “old” with “new” starting at the current line for the next 50 lines.
:1,50s/old/new/ Replace all occurrence of the word “old” with “new” on lines 1 thru 50.
:%s/old/new/ Replace all occurrences of the word “old” with “new” in the entire file.:e!