Every IT professional has a favourite text editor. However, sometimes you simply need to make a quick modification to a file, without leaving the terminal (especially when working via SSH). In this scenario, vi remains a popular choice.

vi is a screen-oriented text editor originally created for Unix, which means it is also available on Linux and Mac OS X.

vi has two modes “Command Mode” and “Insertion Mode”.

Command Mode

vi launches in command mode, allowing for cursor movement, text deletion and pasting.

Movement:

h = Move left  
j = Move down  
k = Move up  
l = Move right  
H = Move to top  
L = Move to bottom  


Delete and Undo:

x = Delete after cursor  
X = Delete before cursor  
:d = Delete current line  
u = Undo last change  


Copy and Paste:

yy = Copy line in buffer  
p = Put after cursor  
P = Put before cursor  


Save and Quit:

:x = Exit, saving changes  
:q! = Exit, ignore changes  


Insertion Mode

Insertion mode begins once an insertion key is pressed. Press the “ESC” key to return to command mode.

Insert:

a = Append after cursor  
i = Insert before cursor  
r = Replace one character  
R = Replace many character  


The majority of commands execute immediately, excluding the “colon” commands, which execute after the “Return” key is pressed.