Vim: Moving Around

This is part 3 of a series of tutorial to Vim. You can read Part 2 here.

Go Mouseless!

Vim has hundreds of ways to navigate1. You can move character by character, word by word, line, function, page among several other ways. You will soon realize that you not only do not need mouse, in fact using mouse is expensive. It takes too much time to reach mouse, too much time to reach the precise location. Really, using mouse while programming is an anti-pattern.

But not all editors are so keyboard friendly, there are shortcuts for common commands but for rest you need to use mouse to go deep into menu to find the button. You can use keyboard to do the same (using <alt>+f) but it is not a good experience. Vim treat keyboard as first class citizen. Every command is accessible via keyboard in an efficient way. Just press : and press tab for autocomplete.

While Vim supports mouse too2, I would highly encourage you to not enable those options and give mouseless operation a whole hearted try.

Be Lazy

Home Row

Vim takes saving efforts to the extreme. It supports and encourages use of h, j, k, l to be used as arrow keys so that user do not have to leave home row.

h Left
j Down
k Up
l Right

Words

There is a better way to traverse a sentence than to go a character per stroke and that's word by word. Press w to jump to start of next word, e to go to end of word. If you want to jump complex word (word joined by a hyphen, like a super-word), use W and corresponding E.

Going back is same using b and B.

Composing

Still, traversing a 20 word long sentence using w takes effort. Wouldn't it be better if we could directly jump to, say, 5th word. And that's where command composing comes into play. Simple press 5w and you will jump 5 words. Of course, this is generic, you can put in any number followed by, as we'll see later, any command to execute that command that many times. So, 100b takes use back 100 words.

  • Lesson 4

    number followed by command executes the command that many times.

    Example:

    • 5j will traverse down 5 lines.

Some more line navigation

  • $: jump to end of line
  • 0: jump to beginning of line (column 0)
  • ^: jump to beginning of line skipping the whitespace
    • this one is especially useful while working with indented code.

      1: var foo = function (arg) {
      2:     var fooClose = function (argClose) {
      3:         if (argClose == 2) {
      4:             argClose += 2;
      5:         }
      6:     }
      7: }
      

      Here pressing ^ anywhere on line 3 will take us to if rather than column 0.

  • Find

    Even more power command if f. Hit f followed by character to directly jump to word starting with that character. Try this in Vim:

    humpty dumpty sat on a wall
    

    Now while the cursor is on column 03, hit fs, your cursor should directly jump to "sat". What if there were multiple words with same start character:

    humpty dumpty sat on a west wall
    

    No problem, you can compose find command: hit 2fw to find 2nd w Also, to find a character backwards, use F. As, we'll see in future parts, this is a common style in Vim: to do something in reverse, use <shift> and the key.

Jumping through the complete buffer4

  • <ctrl>+u: scroll up
  • <ctrl>+d: scroll down
  • :<number>: to jump to that line number
    • very handy while debugging or when compiler complaints about line number in source code, just hit :31 to jump to line 31.
  • gg: go to beginning to buffer
  • G: go to end of buffer
  • gf: go to file at path under cursor

    /etc/hosts
    

    Just type those lines in Vim buffer, write file, and press gf while cursor is anywhere on path and see the magic.

    • this is very handy while writing configuration scripts, or bash scripts.
    • This opens up a new buffer, we'll see how to manage buffers in later parts, for now, you can use :bp (buffer previous) to return to previous buffer.
  • gd: go to definition
    • poor man's jump to definition: use this to jump to first occurrence of a word.

Summary

Command Comment
h left
j down
k up
l right
w next word
W next complex/big Word
b back: previous word
B Back: previous big/complex word
f find character
F find character backwards
$ end of line
0 beginning of line, column 0
^ beginning of line ignoring whitespace
A Append: goto end of line and switch to insert mode
I Insert: goto to start of line ignoring whitespace and switch to insert mode
<ctrl> + u scroll up half page
<ctrl> + d scroll down half page
:n jump to nth line
gg goto to start of buffer
G goto to end of buffer
gf goto file
gd goto definition

These are by no way comprehensive list of jumping commands. But a good point to start. You can find more ways of jumping easily online or, again, as we shall in future, in Vim help menu.

Click here for part 4

Footnotes:

1

I wouldn't be surprised if this is not actually an exaggeration

3

f starts finding from that location, so starting from column 0 gives us freedom of jumping to any word.

4

Vim Buffer is file loaded into memory. We'll return to this in future part when we discuss handling multiple buffers.

Creative Commons License

Date: 2017-05-30

Author: Anurag Peshne

Emacs 25.2.2 (Org mode 9.1.14)

Validate