Tuesday, February 23, 2010

vim tips :wq

Note: The time taken in reading this post will be compensated if you practice it properly :)

Most of my friends use Vim as a text editor. However, I find it strange that even after using it for a year, they still use it like notepad! So if they want to delete the second half of line 23, they press 'i', press the down arrow key till it reaches line 23, press the right arrow key till they reach the half point, and then finally press the delete key till the whole line is deleted. One of my friends stopped using vim because he couldn't use his mouse!!

I wont blame them. The vimtutor is an interesting place to start learning vim. But beyond that, the next place that can help you learn further would be some large reference book. So this series of blog posts are intended to slowly get you started with vim.

Let's start with the basics.

1. dd - Delete the whole line.
Similarly, cc deletes the whole line and changes to insert mode. It's d for delete and c for change.

2. D - Delete the rest of the line (starting from the current position). C does the same, and puts you to insert mode.

3. dw - Delete word. cw is similar.

4. % - Move to the matching bracket of the bracket at the current position. d% will delete from the current bracket till it's matching bracket.

5. f - Find. So if you type fr, it will go to the next occurence of r in the current line. If the letter doesn't occur in the current line, nothing happens. F (capital f) will do the same, but find the previous occurence of the letter in the current line. f and F are most useful when you use it along with d. For example, if you want to delete till the next r in the line (including the r), all you have to do is "dfr"!! t and T also work in the same way except that dtr will delete till the r, but not the r. Using c instead of d will do the same, and also put you into insert mode.

6. . (dot) - Repeat the last editing command.

7. u - Undo

So next time you want to change

draw_dog(int tail) to draw_human(int brain), just go to the first d, type fdc%human.... .

More to come soon.

8 comments:

  1. Just a note, you can use mouse in vim.

    set mouse=a

    ReplyDelete
  2. @lsdkf Yeah, I taught him that, he was happy :) He started using vim again.

    ReplyDelete
  3. hey coder!!!!!
    write something else yaar!! write some of your interesting experiences...thoda saa mast bannaa yaar blog ko. Vim toe we will always learn from GOOOOOgle

    ReplyDelete
  4. @vishal this is intended to be a technical blog. Will be making a different personal blog.

    ReplyDelete
  5. oye can u say how 2 use those in built syntax suggesters..
    i mean if u wanna type glColor3f() etc ive seen u do somethin to directly get dat after u type it partially ....

    spread ur knowledge we 2 will b enlightened :)

    ReplyDelete
  6. n ya if possible add some screen shots ;)

    ReplyDelete
  7. @rakesh Check this out http://www.vim.org/scripts/script.php?script_id=1643 It has instructions on how to install.

    This one is also cool http://www.vim.org/scripts/script.php?script_id=2540

    ReplyDelete