Move By Context, Not Cursor Position


The poor soul who is using VIM for the first time will be found pressing up and down arrows and executing key repeats, moving horribly inefficiently through any body of code.

He will be scrolling or paging (btw: ^f moves forward one page, ^b moves backward one page) and searching with his poor eyeballs through piles of code.

This poor soul is slow and clueless, and probably considers VIM to be a really bad version of windows notepad instead of seeing it as the powerful tool it is.

By the way, the arrow keys don't always work for vim, but don't blame vim.

It's actually an issue with the way your terminal is set up. Vim can't tell that your arrow keys are arrow keys. If you have the problem, you have more research to do.

To use VIM well, it is essential that you learn how to move well.

Do not search and scroll.
Do not use your eyes to find text.
They have computers for that now.

Here are a handful of the most important movement commands. The best way to move is by searching:
/search forward: will prompt for a pattern
?search backward: will prompt for a pattern
nrepeat last search (like dot for searches!)
Nrepeat last search but in the opposite direction.
txMove "to" letter 'x' (any letter will do), stopping just before the 'x'. Handy for change/delete commands.
fx"Find" letter 'x' (any letter will do), stopping on the letter 'x'. Also handy for change/delete commands


If you're not searching, at least consider jumping
ggMove to beginning of file
GMove to end of file
0Jump to the very start of the current line.
wMove forward to the beginning of the next word.
WMove forward to the beginning of the next space-terminated word (ignore punctuation).
bMove backward to the beginning of the current word, or backward one word if already at start.
BMove backward to the beginning of the current space-terminated word, ignoring punctuation.
eMove to end of word, or to next word if already at end.
EMove to end of space-terminated word, ignoring punctuation


The following commands are handy, and are even sensible and memorable if you know regex:
^Jump to start of text on the current line. Far superior to leaning on left-arrow or h key.
$Jump to end of the current line. Far superior to leaning on right-arrow or k key.


Here is some fancy movement
%move to matching brace, paren, etc
}Move to end of paragraph (first empty line).
{Move to start of paragraph.
(Move to start of sentence (separator is both period and space).
)Move to start of next sentence (separator is both period and space).
''Move to location of your last edit in the current file.
]]Move to next function (in c/java/c++/python)
[[Move to previous function/class (in c/java/c++/python)


Finally, if you can't move by searching, jumping, etc, you can still move with the keyboard, so put your mouse down.
hmove cursor to the left
lmove cursor to the right
kmove cursor up one line
jmove cursor down one line
^fmove forward one page
^bmove backward one page


You want to use the option hls (for "highlight search") in your vimrc. You will learn about that soon enough.

In the short term you can type ":set hls" and press enter.

No comments:

Post a Comment