Getting started with Vi
Introduction
Vi is the command line text editor that is included in almost every Linux and UNIX system on the planet. Depending on who you ask if it either the best thing since sliced bread or the most awful thing since green ketchup. Occasionally you won't have any chance but to use Vi. This tutorial should show you just enough to do what you need and get out in one piece. :-)
Before you run Vi
Open the "terminal" - this is where Vi is run from
If you are going to edit a file that requires root (administrator) privileges and you are not currently logged in as root you can switch user by typing:
su [enter]
your root password [enter]
Running Vi
To run Vi and create a new file type:
vi new filename
To run Vi and edit an existing file type:
vi /directory/filename
Once you are in Vi:
Vi has two modes: command mode and insert mode. Command mode allows you to do things like save and insert mode allows you to type. Vi automatically starts in command mode, to switch to insert mode type:
i [enter]
To return to command mode hit the [esc] key
Tip: you can tell you are in insert mode because --- INSERT --- is displayed at the bottom of the Vi window.
Moving to and modifying the text
In command mode:
j [enter] - to jump down one line
k [enter] - to klimb up one line
l [enter] - leap right one character space
h [enter] - hop left one character space
dd - deletes entire
In insert mode:
Type to write text
[backspace] or [ctrl] + [backspace] or [delete] to delete text.
When you have finished modifying the file:
Make sure you are in command mode by using [esc]
:wq [enter] - will write quick (save) and close Vi
or
:q! [enter] - will quit without saving changes. (This is a good panic button if you are not sure you have done the right thing!)
Practice
To practice follow the steps below to open Vi and create a new file, add two lines of text, edit one line and save the file.
vi cookies.txt [enter]
i [enter]
U like cookies [enter]
My favorite kind are chocolate chip [enter]
[esc]
k [enter]
l [enter]
i [enter]
[backspace]
I [enter]
[esc]
:wq
Read more about Vi
If you would like to read more about Vi the following websites are very good: