There are innumerable key combinations in Vim (well, they are in fact finite, but numerous), so I thought I'd describe the ones that I use most often and find the most useful:
- h, j, k and l - Move left, down, up, or right.
- gg and G - Move to the top or the bottom of the file.
- ctrl-u and ctrl-d - Move up or down one half-screen.
- w and b - Move forward or backward, one word at a time.
- fX, FX - Move the cursor over the next or previous occurrence of X in the current line, where X is any character.
- tX, TX - Move the cursor to (not over!) the next or previous occurrence of X in the current line.
- 0 and $ - Move the cursor to the beginning or end of the line.
- ^ - Move the cursor to the first non-whitespace character of the line.
- /[string] and ?[string] - Searches forward or backward for [string].
I love tabs. They're a new feature in Vim 7, and I instantly became dependent on them. Here are the commands for using them:
- :tabe [file] - Open a new tab editing [file]. Don't specify [file] to open a blank tab.
- gt and gT - Move to the next or the previous tab. If already at the last or first tab, wraps around.
- Ngt - Move to the Nth tab.
- :q - Closes current tab.
- :qa - Closes all tabs and exits Vim.
- Deletes in the direction of your movement. E.g.: d$ deletes to the end of the line, dw deletes to the end of the current word, dF[ deletes backward to and including the next '[' character.
- dd - Deletes current line.
- rX - Replaces current character with character X.
- ~ - Toggles capitalization of current character. If in visual mode, toggles capitalization of highlighted block.
- s - Deletes current character and enters insert mode.
- i - Enters insert mode.
- a - Moves one character forward and enters insert mode.
Folding is incredibly useful. It's great for getting large blocks of code out of your way in annoyingly long class files.
- zf[movement] - Folds lines in direction of [movement]. E.g.: zfG folds all lines from the current line to the end of the file. For [movement], you can also use text objects.
- zf - In Visual mode, will fold the currently selected lines.
- zo - Open the fold under the cursor.
- zc - Close the fold the cursor is in.
- :% - Replaced by current file. You'll see how this is useful below.
- :![command] - Execute [command] in the shell. E.g.: !php % - runs current PHP script, !ls - lists files in current directory, !php -l % - checks syntax of current PHP script, !cp % %.bak - creates a backup of current file.
- :nohl - Turns off highlighting, if it's on. Depending on your Vim's configuration, doing searches (with / and ?) highlight all matches. This will un-highlight all search matches.
This may seem overwhelming if you're new to Vim, but it becomes second-nature as you get used to it. I did not have to check the Vim documentation at all to write this blog post. It is in fact possible to become accustomed to using so many (useful!) Vim commands. Of course, these aren't all I use, but just the most common.
Next post, I'll discuss my .vimrc file, which contains tons of configuration that makes using Vim a lot easier for me. Perhaps you'll be able to use some of my settings to help you, too.