Moving Around in Vim

I’ve written a couple of beginning UNIX for OS X entries, such as creating a .bash_profile and .bashrc file using vim.  Here is another vim tutorial for moving around in the screen editor, so you can see just how powerful it’s commands can be.

This post will deal with mostly command mode, the default mode when you open vim (or an existing file with it.)  To move the command marker around the file without entering INSERT mode consists of four basic keys.  Moving to the next/previous word, to the beginning of a paragraph, etc. will require a bit more memorization.

You want to first familiarize yourself with the h, j, k, and l keys.  The outer two (on a standard keyboard) move your cursor left or right one-character;  h, to the left, and l to the right.  j will move your cursor up to the next line, while k moves it down one line.  The fact that you are in command mode means no changes are made to your text at this point.  You can use the arrow keys for the same functionality, but getting used to the keys will help you keep your hands where they belong–typing and editing text.

Moving to the beginning of the current line is done by pressing O (zero).  To move to the end of the current line press $+ moves your cursor to the beginning of the next line, and – moves you to the beginning of the previous line.

Next are some basic ideas for commands.  Some will take a number argument, followed by a command.  Some can be written with a number argument followed by another command.  For example, x in command mode means delete a character.  To delete a word, or the rest of a word if you are in the middle of one, you type dw.  If you precede x with a number, n, vim will delete the following n characters from the line.  The command to delete the next four characters would be written, simply, as 4x. To delete the next four words, you would type 4dw.

Vim is a very capable text editor once you learn more about its commands, and get used to bouncing between INSERT and command modes.  Below you can find a table of common commands and their function, at least enough to get you on your way as a skilled vim user.

Movement Command

Function

h

Move Left One Character

j

Move Down One Line

k

Move Up One Line

l

Move Right One Character

0

Move to First Character Of Current Line

$

Move to Last Character Of Current Line

+

Move to First Character Of Next Line

w

Move to Next Word or Punctuation Mark

W

Move to Next Word

e

Move to End of Current Word

E

Move to End of Next Word

b

Move Back to Beginning of Word or Closest Punctuation

B

Move Back to Beginning of Word

)

Move to End of Current Sentence

(

Move to Beginning of Current Sentence

}

Move to Start if Next Paragraph

{

Move to Start of Previous Paragraph

Delete Command

Function

x

Delete Current Character

X

Delete Character Immediately Left Of Cursor

dw

Delete Current Word

10dw

Delete Ten Words

D

Delete To End of Current Line (also d$)

dd

Deletes Current Line

20dd

Deletes Twenty Lines

dG

Deletes From Cursor to End

u

Undo (doesn’t work for single character deletion)