Moving to Character


One of Vim's super-powers is the breadth of commands available to move the cursor around a buffer. We will first look at some of the commands for moving the cursor (generally) horizontally. In many cases these are the most commonly-used commands, since they control cursor location while typing sentences or lines of code.

CommandAction
f{char}cursor to Nth occurrence of {char} to the right
F{char}cursor to the Nth occurrence of {char} to the left
t{char}cursor till before Nth occurrence of {char} to the right
T{char}cursor till after Nth occurrence of {char} to the left
;repeat latest f, t, F or T N times
,repeat latest f, t, F or T in opposite direction N times
&repeat last :s

Let's demonstrate these commands, using the following buffer:

Initial buffer content
Knowledge·can·be·conveyed,·but·not·wisdom.·It·can·be·found,
it·can·be·lived,·it·is·possible·to·be·carried·by·it,·miracles
can·be·performed·with·it,·but·it·cannot·be·expressed·in·words
and·taught.
 
-·Siddhartha
NORMALTop1:1
 

First, lets jump to the first e using fe:

Move to first efe
Knowledge·can·be·conveyed,·but·not·wisdom.·It·can·be·found,
it·can·be·lived,·it·is·possible·to·be·carried·by·it,·miracles
can·be·performed·with·it,·but·it·cannot·be·expressed·in·words
and·taught.
 
-·Siddhartha
NORMALTop1:6
 

Note that the cursor moved directly on top of the character we were searching for, e. f{char} also accepts a count, so let's jump to the 2nd e using 2fe:

Move to second e2fe
Knowledge·can·be·conveyed,·but·not·wisdom.·It·can·be·found,
it·can·be·lived,·it·is·possible·to·be·carried·by·it,·miracles
can·be·performed·with·it,·but·it·cannot·be·expressed·in·words
and·taught.
 
-·Siddhartha
NORMALTop1:16
 

The t{char} command is similar to f{char}, except instead of moving the cursor on top of the target, it moves the cursor up next to the target:

Move up to first oto
Knowledge·can·be·conveyed,·but·not·wisdom.·It·can·be·found,
it·can·be·lived,·it·is·possible·to·be·carried·by·it,·miracles
can·be·performed·with·it,·but·it·cannot·be·expressed·in·words
and·taught.
 
-·Siddhartha
NORMALTop1:18
 

Like f{char}, t{char} also accepts a count:

Move up to second o2to
Knowledge·can·be·conveyed,·but·not·wisdom.·It·can·be·found,
it·can·be·lived,·it·is·possible·to·be·carried·by·it,·miracles
can·be·performed·with·it,·but·it·cannot·be·expressed·in·words
and·taught.
 
-·Siddhartha
NORMALTop1:32
 

Now, to demonstrate repeating a search, ; repeats the previous search:

Repeat previous search;
Knowledge·can·be·conveyed,·but·not·wisdom.·It·can·be·found,
it·can·be·lived,·it·is·possible·to·be·carried·by·it,·miracles
can·be·performed·with·it,·but·it·cannot·be·expressed·in·words
and·taught.
 
-·Siddhartha
NORMALTop1:39
 

, repeats the previous search but in the opposite direction:

Repeat previous search in reverse,
Knowledge·can·be·conveyed,·but·not·wisdom.·It·can·be·found,
it·can·be·lived,·it·is·possible·to·be·carried·by·it,·miracles
can·be·performed·with·it,·but·it·cannot·be·expressed·in·words
and·taught.
 
-·Siddhartha
NORMALTop1:34
 

Note that in this search we were moving up to the o. The previous search started from the n in not, while the reverse search ended at the t.