Within a Line


Vim comes with a wide variety of commands that move the cursor around a buffer. We differentiate (generally) horizontal motions from the (generally) vertical motions, which are discussed in the next section.

Horizontal motions move the cursor in the same line that is being edited. These are the motions that are used while actively editing text or code, and therefore are some of the most commonly used motions.

We start with a few motions that move the cursor to absolute positions with the current line:

CommandAction
0cursor to the first char of the line
<Home>same as "0"
^cursor to the first CHAR of the line
|cursor to column N
gMgo to character at middle of the text line
g_cursor to the last CHAR N - 1 lines lower
$cursor to the end of Nth next line
<End>same as "$"

The following motions are similar to those above, but move the cursor to absolute positions within the current (visible) screen:

CommandAction
g0when 'wrap' off go to leftmost character of the current line that is on the screen; when 'wrap' on go to the leftmost character of the current screen line
g^when 'wrap' off go to leftmost non-white character of the current line that is on the screen; when 'wrap' on go to the leftmost non-white character of the current screen line
gmgo to character at middle of the screenline
g$when 'wrap' off go to rightmost character of the current line that is on the screen; when 'wrap' on go to the rightmost character of the current screen line

Let's demonstrate some of these commands to highlight the differences and how to use them. Starting from the following buffer:

Initial buffer content
<ul> 
····<li>one</li>
····<li>two</li>
····<li>three</li>
</ul>
NORMALTop1:1
 

First, let's move to the end of the current line, using $.

Move to end of line$
<ul> 
····<li>one</li>
····<li>two</li>
····<li>three</li>
</ul>
NORMALTop1:4
 

For those of you not yet familiar with patterns, $ in a pattern refers to the end of the pattern, which provides a useful mnemonic for remembering how to get to the end of the current line. $ and <End> are equivalent, so use whichever feels most natural.

$ (as well as g_) also accepts a count. When a count is used, Vim first moves the cursor down [count-1] lines, then moves the cursor to the end of that line.

Move down one line3$
<ul> 
····<li>one</li>
····<li>two</li> 
····<li>three</li>
</ul>
NORMAL50%3:16
 

Next, lets move to the first character of this line, using ^:

Move to start of text^
<ul>
····<li>one</li>
····<li>two</li> 
····<li>three</li>
</ul>
NORMAL50%3:5
 

Similar to $, in patterns ^ refers to the start of the pattern, so makes a nice mnemonic to remember moving to the start of the line. ^ and <Home> are equivalent, so use whichever feels most natural.

Next, move the cursor to the middle of the current line:

Move to the middle of the linegM
<ul>
····<li>one</li>
····<li>two</li>
····<li>three</li>
</ul>
NORMAL50%3:9
 

This could have also been accomplished using |. In general, gM provides a quick jump to the middle of the line, while | is useful when more precision is required.

Finally, lets move to the first column of this line, using 0

Move to start of line0
<ul>
····<li>one</li>
····<li>two</li>
····<li>three</li>
</ul>
NORMAL50%3:1
 

Now that we have seen how to move horizontally to absolute positions, we will next look at how to move horizontally relative to characters in the line.