shell

Vim. Moving around

Vim is not a mouse driven program. With other editors, you use the mouse to place the cursor at the desired position. This seems to be fine. However, you do not realize how faster you can move using a command approach as in Vim.

In Vim, you move around the text using different commands, that is, keystrokes while you are in command mode. At the beginning this may result confusing or even seem a waste of time. However, once you get used to it, you will never leave it and you find it to be faster than any other method.

Basic movements

  • h (left)
  • j (down)
  • k (up)
  • l (right)

In most modern implementations of Vim, you can use the arrow keys to move as you normally do with other applications. However this option is not always available, mostly when you are using Vim remotely and the communication program gets confused or something. The standard keys will always work so you had better get used to them. My advice is that from now on, you use these keys exclusively.

Most of the commands can be repeated automatically just specifying how many times you want to repeat it:

ESC6j moves the cursor six lines downwards.

Moving one character at a time is not really efficient. A better approach is to move word by word. Here are some of the commands to do this:

  • w (word) move at the beginning of the next word
  • W (Word) the same but it “understands” special characters
  • e (end) put the cursor at the end of the next word
  • b (back) put the cursor at the beginning of the previous word

Let us see an example. Bold letters indicate the cursor position.

This is a text sample
w
This is a text sample
W
This is a text sample
e
This is  a text sample
b
This is a text sample
b
This is a text sample

Let us see what happens with words including special characters:

This word has different/special characters.
w
This word has different/special characters.
w
This word has different/special characters.
b
This word has different/special characters.
W
This word has different/special characters.
B
This word has different/special characters.
E
This word has different/special characters.

As you can see, the capital version of the normal commands do the right thing with these special words.

Vim knows about more text objects (e.g. lines) and because of this, you can tell him to go to the beginning or the end of the line:

  • 0 move to the beginning of the line
  • ^ move to the first non-blank character of the line
  • $ move to the end of the line

Four other useful commands are the following, intended for placing the cursor at the first occurrence of a certain character in a line:

  • f places the cursor at the first occurrence of the character
  • F the same, backwards
  • t places the cursor before the first occurrence of the character
  • T the same, backwards

Some examples for these commands:

This is another test
fo
This is another test
Fi
This is another test
to
This is another test
Th
This is another test

The “g” family of commands are used to “goto” a specific place in the file.

  • gg goes to the beginning of the file
  • G goes to the end.
  • numberG goes to the specific line (e.g. 5G goes to line 5)

Another equivalent method for the last one is: ESC:numberENTER

In addition, if you want to move forward and backwards one page at a time, you can use Ctrl-f and Ctrl-b respectively; or if you just want to move half-page at a time: Ctrl-d Ctrl-u. Of course the Page Up and Page Down keys of most of the keyboards work fine too, but it is preferable to use the proper commands.

These commands are sufficient to make a good use of Vim’s movement facilities. Use them as much as you can.

Vim Sheet (II)

  • h, j, k, l: move the cursor
  • w, W, e, b: move at the begin/end of the next/previous word
  • 0, ^, $: move to the beginning/end of line.
  • f, F, t, T: find a character forward/backwards
  • g, G: go to a line number
  • Ctrl-f/b/d/u: move page forward/backwards.

Enjoy!

1 Comment

  • On 10.23.10 commandliners » Vim. Macros said:

    […] repeat complex commands sequences. In order to get the most out of this feature, you need to master movement and insertion […]

speak up

Add your comment below, or trackback from your own site.

Subscribe to these comments.

Be nice. Keep it clean. Stay on topic. No spam.

You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

*Required Fields