Craig Francis


Using VI

VI, now more affectionally known as VIM, is a really powerful text editor which can be used from a terminal window.

It takes some time getting used to, but that time is well spent. It is extremely powerful and each feature can save you lots of time. It is so powerful I don't think anyone knows every feature it can offer.

See below for a quick reference on a few of the commands in VI, these might not mean much until you have learnt the basics, but I find this list as a good reminder.

Starting VI

vi <filename> Edit a file called <filename>
vi Brings up editor with no filename

Saving and Quitting

:q Allows you to quit if no changes were made
:q! Quits without saving changes
:wq Writes changes to the file, then quits
ZZ Writes changes to the file, then quits
:w Save changes without leaving edit session

Moving the cursor

h Move Left
j Move Down
k Move Up
l Move Right

Moving around in COMMAND MODE

^b Back one screen
^f Forward one screen
^g Show current line number
:set number Display line numbers along left side of screen
:set nonumber Turn off line numbers along left side of screen

Go-to line

:<#> Go-to line number <#>
<#>G Go-to line number <#>
1G Go-to first line
$G Go-to last line
:$ Go-to last line
G Go-to last line
^ Go-to start of current line
$ Go-to end of current line

Undo

u undo last change made
U undo all changes made to current line

COMMAND to EDIT mode

i insert before current position (EDIT mode)
I insert at beginning of current line (EDIT mode)
a append after current position (EDIT mode)
A append at end of current line (EDIT mode)
o open new line below current line (EDIT mode)
O open new line above current line (EDIT mode)
<esc> return to COMMAND mode

Edit commands

x delete current character
dd delete current line
d$ delete to end of current line
dw delete to end of current word
r replace current character with next character typed
R overwrite characters until <esc> is typed

Searching & Replacing

/<string> Search forward for <string>
?<string> Search backward for <string>
:<#1>,<#2> s/<orig>/<new>/g Replace all occurrences of the old string in the range between the two line numbers with the new string

Moving & Copying

:<#1>,<#2> t <#3> copy text between <#1> and <#2> to line <#3>
:<#1>,<#2> m <#3> move text between <#1> and <#2> to line <#3>. Although in this case "." means "current line", while "$" means "last line"
:r <filename> brings in the file <filename> at the cursor