Top 10 Gvim/Vim Commands for coding

16 05 2011

When you first start using Vim, you’ll hate it. It makes a terrible first impression. It isn’t until you think one day, “Hm, I want to skim up to the top of the file to remember what that variable name was, then jump back here,” and then proceed to type, without thinking, magg’a. That’s the day you’ll fall in love with Vim, when you start translating complicated editing actions into quick commands rather than painful trudging through files one arrow key press at a time. You tell Vim what you want to do, and it happens. It just takes a long time to learn how to tell Vim what to do.

If you had to code using just 10 Vim commands (besides edit, write, quit, etc), what would they be? Here are my favorites.

Navigating the Code

1. / text or ? text Fine next/previous occurrence of the text specified. Also highlights all matches. This is the primary way I find myself moving to the middle of a line in Vim. Rather than sitting there and scrolling right character by character or word by word, just start typing the text you want to move to. n moves to next match, N moves to last match. Using the find command may seem like an odd way to move around at first, but I find that just typing 2-3 characters will take me right where I want to go 90% of the time, and ends up taking way less keypresses than other navigation methods.

2. :xxx Jump to line number xxx.

3. ma will mark your current cursor position, ‘a will jump back to it. The a can be any letter. This is amazingly useful when you want to scroll somewhere in the code to read and remember how you did something, then jump right back to where you were editing.

Writing new Code

4. Ctrl+n in insert mode will attempt to autocomplete whatever you’re typing. Great for being able to type partial variable or function names, minimizes typos and speeds your typing up. This can be enhanced further with the intellisense plugin.

5. o in command mode will create a new blank line below your current cursor position, move your cursor to the correct indentation level, and go into insert mode.

6. % in command mode will jump to the opposite brace of the brace your cursor is at.

General cool stuff

7. ! xxx will run the command xxxx in a terminal and show you the result, then let you jump back to vim when you’re done.

8. :%s/1/2/g Replace all instances of 1 with 2 in the file. Can use regular expressions and is extremely powerful.

9. :badd/:bn/:bdel navigating multiple files at the same time is a must for a programmer.

10. y/p yank and paste (also known as copy in paste) is such a fundamental command it hardly warrants mention, but it’s used so often that it takes the number 10 spot.

Advertisements

Actions

Information

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s




%d bloggers like this: