Vi can either be one of your worst enemies or one of your best friends. Anyone new to unix/linux will typically have a hard time coming to terms with the fact that vi can be both necessary and efficient, especially if you are only used to graphical text editors.
I truly believe that any new unix/linux user or administrator MUST spend the time learning the basics of VI. This will make you more comfortable with the terminal environment, give you the opportunity to use “set -o vi” later, and increase the efficiency of script writing. Regardless of what the popular argument says, simply learning VI will always be more efficient than writing your scripts in word on your workstation and ftp-ing or scp-ing the files over. Many tools will use vi as their editor such as cron and inittab. Also the aforementioned “set -o vi” will be a lifesaver for anyone working in unix that would like to use command history recall.
Things you need to know in VI:
Vi runs in two distinct modes (command and input). If you are not inputing text into the file, you are issuing commands to vi. These commands include things like: navigate cursor here, page up, jump to next word, and then switch to input mode.
To navigate vi you can move either with the arrow keys or the following hjkl commands:
k — navigate up
j — navigate down
h — navigate left
l — navigate right
From this you may want to navigate larger distances easier. Have no fear, there are commands for that.
:1 — beginning of document
H — beginning of current screen (top left hand side of viewing area)
0 — beginning of line
$ — end of line
G — end of document
w — forward one word
b — backward one word
<ctrl>-b — Up full screen
<ctrl>-u — Up half screen
<ctrl>-d — Down half screen
<ctrl>-f — Down full screen
To switch to input mode you use the following:
i — will switch to input mode where the cursor is located
a — will switch to input mode after the cursor
A — will switch to input mode at the end of the line
o — will open a new line below your cursor
O — will open a new line above your cursor
Change/delete:
cw — change word
dw — delete word
cc — change line
S — replace line
dd — delete line
r — replace single character with single character
s — replace single character with string
R — switch to overwrite mode
Find/replace:
/string — find string (forward)
?string — find string (backward)
:s/string/newstring — replace string with new string (once on current line)
:s/string/newstring/g — replace string with new string (on entire line)
:%s/string/newstring/g — replace string with new string (in entire file)
:ns/string/newstring — replace string with newstring (on nth line)
n — next search match
N — previous search match
Cut/Copy/Paste:
yy — copy line
dd — cut line
p — paste after cursor (cannot paste if buffer emptied by going into input mode)
P — paste before cursor
Undo:
u — undo last change
U — undo all changes to line
Save/Exit:
:w — write changes
:q — quit Vi
:wq — write and quit
:q! –quit without saving
:w newfile — save as newfile
:sh — drop down to shell (will run shell on top of Vi so need to exit shell (<ctrl>-d) to get back to Vi
**NOTE**
- undo can only work for the current line
ncmd — you can increase the repetition or length of a command by issuing a number infront of it
ie 5dd — cut/delete next five lines
10j — navigate down 10 lines
:n — go to nth line
. — repeat last command
:set all — show all of the variables you can change to increase the ease of use of VI
a few important ones:
:set (no)numbers — show line numbers
:set (no)autoindent — auto indent on new line
:set (no)showmode — display INPUT MODE when in input mode
** to turn on use “:set numbers” to turn off use “:set nonumbers”



[...] tutorial will be heavily related the the Vi Tutorial. I suggest if you need more help or explanations with this, please visit that post for further [...]