First timers generally don't feel comfortable with Unix's vi editor soon, but don't be afraid of vi editor, its like normal notepad editor that you use in Microsoft Windows with ease and charm, only difference is that the way of interacting with vi editor is a bit different. But its easy to learn and to use after learning.
Most important thing in vi editor is that there are 2 modes, insert mode and command mode. The meaning of your keystroke(s) is determined by the mode you are in.
Read on to learn it. Best will be to do hands-on practice side-by-side. This tutorial should be able to tell you most of the vi commands and mechanism, if not all.
To open a file (say test_file.txt) in vi editor, on Unix command prompt, give following command, it will display the file's content in vi editor:
Like for example, to save a file in notepad, we press CTRL + S, in vi editor, we do colon (:) then w, then press Enter key, i.e. :w ENTER.
To quit, if no changes have been made, use :q, then press Enter.
To save and quit, use :wq, then press Enter.
To quit WITHOUT saving the changes done, use :!q (exclamation q), then press Enter.
To type something in vi editor, we have to first specify the Mode. There are two modes: command mode and insert mode.
In insert mode, whatever we type on keyboard, it gets printed on the screen. But in command mode, the keystrokes are interpreted as commands.
When we start vi editor, it starts by Default in command mode. To start typing, exit current mode by pressing ESC (escape key), then press i (inserting) or a (appending) , for insert mode.
When you did, say, ESC + i, anything you type will print on screen, you can now :w (just save), :wq (save anq quit) or :!q (quit without saving).
In notepad, to go to beginning of a line we use HOME, in vi editor, we use 0 (numeric zero). For reaching end of line, we use END key in notepad, in vi, we use $ (dollar key).
In notepad, to search something, we do CTRL + F, then type something. In vi, since search is a command, first go to command mode by pressing ESC, then type forward slash (/), you will see that forward slash on LOWER LEFT CORNER of screen. Type the work to search, it will appear just after that cornered forward slash, and press enter, it will highlight first occurrence of that word. For next occurrence, press letter n (lower case). For previous occurrence, press letter N (upper case).
To delete a letter or number, go to command mode by ESC, then press x (lower case). To delete an entire line, use dd (double letter d in lower case).
To delete 2 lines, use 2dd in command mode.
So general formula is that for deleting n lines use, ndd command.
You want to copy text and paste it in vi editor ? Use yank to copy and put to paste lines.
yy command copies a line into a buffer (5yy copies 5 lines), then use the put command (p for line below or P for line above) to insert the contents of the buffer.
To replace characters, use r or R command. These commands will put you temporarily into insert mode.
r: Will replace the current character with the next character you type. Once you enter the character you are returned to command mode.
R: Puts you in over-type mode until you hit ESC which will then return you to command mode.
For cursor movement among words or characters, first switch to command mode if in insert mode, by using ESC key.
Cursor movement is done by normal left/right arrow keys, same as in Notepad. That will move to next or previous Character only. For moving among words in vi editor, use following:
w (move to beginning of next word)
b (move to beginning of previous word, if in middle of word, b command will move cursor to beginning of current word)
e (move to end of current word)
Up and down arrow keys in command mode will move cursor a line above or down of the current line.
For moving through the screen, use following :
ctrl-f: scrolls down one screen. ctrl-b: scrolls down one screen.
Use o to insert a blank line just below where the cursor is, and O to insert blank line above the cursor.
Use J to join the current line with next line. Press J (uppercase) while on current line, the line just below will be joined to the end. 3J will join 3 lines.
To undo last action, use u. Using u again will undo the last undo action.
To repeat last command, say a line delete action using dd, just type a dot . , it will remove one more line.
Customizing vi environment using :set command
Using :set, we actually set certain variables which affect the overall behaviour of currently opened vi window.:set number will display line numbers
:set nonumber will remove the displayed line numbers
:set wrapmargin=15 to set a right margin of 15
:set wrapmargin=0 no set no margin on the right
:set hardtabs=5 set number of characters in tab to 5
:set all to view all the vi variables, including above.
Text replacement in vi editor
:%s/ab/xy/g replaces each occurrence of 'ab' with 'xy'
:%s/ab/xy/ replaces first occurrence of 'ab' with 'xy' on each line
For further reading, just google vi editor commands.