This Vi tut and reference is one of the lessons available in the Linux Academy as part of the course “Working with vi”. This lesson will show you how to use vi and it’s basic commands. This will help you get started with THE Linux text editor available on all distributions.

Vi THE Linux text editor (course notes and vi reference from The Linux Academy

Command Mode: Accepts commands, which are usually in the form of individual letters. Example, a and i.
You can use h, j, k and I to navigate in similar ways as the up and down arrow. Not needed if you have an up and down arrow on your keyboard.
Yank – Term used to copy files. Use YY to yank text. Navigate to the line you wish to yank and type yy make sure you are in command mode.
2yy will yank 2 lines, 4yy will yank 4 lines. Precede the YY command with the number of lines you would like to yank.

a – append text after cursor
A – append text to end of current line
p – Command will past the contents starting on the line after your cursor.
P – Upper case P will paste the yanked line(s) starting on the line before our cursor
G – Takes you to specified line
H – Takes the cursor to the top of the page
L – Takes the cursor to the bottom of the page
o – inserts a new line below the line your cursor is on and inserts into insert mode for typing.
u – undo any change, must be in command mode and only goes back one change.

Change text – cc-allows you to change the entire line (basically removes the line and inserts into insert mode) cw to replace just one word on the line the current word your mouse is over.
Search – search forward type / and ? searches backwards; both followed immediately by the term to search for.

n – moves to next occurrence of search string
N – move to next occurrence of search string in opposite direction

Ex mode: Enter ex mode from command mode with “:”. Use this to save or manipulate files. This mode is also referred to as colon commands

Replace text – :%s/fine/ one – this replaces all occurs of “fine” with “one”
% represents “global changes to all occurrences” replace to a line number, ending line number of comma.
:= – returns number of current line
:w filename – writes contents of file to a new filename.
Insert Mode: Enables you to insert and edit text. The Esc key exits insert mode and returns you back to command mode.
R – enters the insert mode to replace text not insert text. It will replace text until it hits a space. After a space you must start typing over the text for it to apply or it will just push text down.
i – used to insert text and not replace it. Pushes existing text down.

:w – Saves changes
:wq – Saves changes and quits
:q! – Quits without saving changes.
:e /file.txt will load the new file.txt into the vi editor for editing. This will only occur if the current changes to the current file being edited are saved.
:r allows you to bring contents of an old file into a new one.
:! Allows you to run shell commands from within vi. Such as :!mkdir test

Get actionable training and tech advice

We'll email you our latest articles up to once per week.