VI Editor

vi Editing commands

Note: You should be in the “command mode” to execute these commands. VI editor is case-sensitive so make sure you type the commands in the right letter-case.

KeystrokesAction
iInsert at cursor (goes into insert mode)
aWrite after cursor (goes into insert mode)
AWrite at the end of line (goes into insert mode)
ESCTerminate insert mode
uUndo last change
UUndo all changes to the entire line
oOpen a new line (goes into insert mode)
dd
3dd
Delete line
Delete 3 lines.
DDelete contents of line after the cursor
CDelete contents of a line after the cursor and insert new text. Press ESC key to end insertion.
dw
4dw
Delete word
Delete 4 words
cwChange word
xDelete character at the cursor
rReplace character
ROverwrite characters from cursor onward
sSubstitute one character under cursor continue to insert
SSubstitute entire line and begin to insert at the beginning of the line
~Change case of individual character

Make sure you press the right command otherwise you will end up making undesirable changes to the file. You can also enter the insert mode by pressing a, A, o, as required.

Moving within a file

You need to be in the command mode to move within a file. The default keys for navigation are mentioned below else; You can also use the arrow keys on the keyboard.

KeystrokeUse
kMove cursor up
jMove cursor down
hMove cursor left
lMove cursor right

Saving and Closing the file

You should be in the command mode to exit the editor and save changes to the file.

KeystrokeUse
Shift+zzSave the file and quit
:wSave the file but keep it open
:qQuit without saving
:wqSave the file and quit