Friday, December 31, 2010

Understanding VIM : Beginner's Tutorial-1

Vim editor takes some time to settle within the working arena  of professionals. It's a cake walk when people use it for atleast a week.
There are three modes in vim - insert mode, command mode, and last-line mode.
Insert mode is meant for inserting text, press i for start mode. Command mode is used for executing formating texts. Last-line mode is used for executing extended commands for text formating.
Start Vim by typing vim /vi .

When you run vim filename to edit a file, Vim starts out in command mode. This means that all the alphanumeric keys are bound to commands, rather than inserting those characters. Typing j won't insert the character "j"--it will move the cursor down one line. Typing dd will delete an entire line, rather than inserting "dd." To get into insert mode, press i.
Now you can insert text. You finished writing the text, so you want to save it. Type esc key, now you are in command mode. You may need to edit your file using these keys:
    * h moves the cursor one character to the left.
    * j moves the cursor down one line.
    * b move backward one word.
    * G move to the end of the file.
    * gg move to the beginning of the file.
    * `. move to the last edit.
    * k moves the cursor up one line.
    * l moves the cursor one character to the right.
    * 0 moves the cursor to the beginning of the line.
    * $ moves the cursor to the end of the line.
    * w move forward one word.
    * x for deleting the character at which cursor is present.
    * d starts the delete operation.
    * dw will delete a word.
    * d0 will delete to the beginning of a line.
    * d$ will delete to the end of a line.
    * dgg will delete to the beginning of the file.
    * dG will delete to the end of the file.
    * u will undo the last operation.
    * Ctrl-r will redo the last undo.
Note that all these deletion operation will start from the position of cursor.
You can extend the operation of these keys, for example k will move the cursor 1 line up, similarly 3k will move the cursor 3 lines up. Same  follows for l, w, b, h, j, b.
Ok, now you want to search something within the text file or save it. Type :, to get into last-line mode. Now save & exit by entering wq!. w for write, q for quit & ! to confirm your operation. If you want to exit without saving any changes, press q!. We will be covering keys for searching & copying/pasting in the next post. Stay tuned.

No comments:

Post a Comment