albusshin
albusshin

Reputation: 4010

How to move cursor to specific word in current line in Vim

Let's say we're currently in this line of code:

readonly L|a|zy<ICountryRepository> countryRepo;

and the cursor is in the position of letter "a", as shown in the code between two "|" symbols.

Now I want to move my cursor to the letter y of the word countryRepo, how can I do that using the minimum key strokes?

(Currently I'm using the key sequence of fyfyfyfy in normal mode ... Kind of stupid)

Upvotes: 15

Views: 15165

Answers (7)

aveLestat
aveLestat

Reputation: 430

use / for search, then type your word and press Enter

however, if you want to jump to next word, just press n

Upvotes: 4

Jacob Wang
Jacob Wang

Reputation: 4794

Use EasyMotion.

In your case, <Leader><Leader>e then a corresponding keypress (in this case b) will bring your cursor onto the second y. Personally I use <Leader> as the easymotion trigger so it is only 3 keystrokes for me. The main advantage is you do not need to guess or calculate.

Upvotes: 2

romainl
romainl

Reputation: 196546

I would do

tR;

or

WtR

or maybe

Wfy

Upvotes: 2

Xuan
Xuan

Reputation: 5629

If you have vim-easymotion, https://github.com/Lokaltog/vim-easymotion

You can do <leader><leader>t and then search for letter y. It's not that fast for the letters on the same line though. The real advantage is when you jump in the entire file.

Upvotes: 3

Edgar Klerks
Edgar Klerks

Reputation: 1537

I can think of:

4fy 

But you should only do this if you are some strange robot.

/co<cr>fy

Which is one character shorter than your solution, but more easy..

Wfy

Go one WORD forward and then find y.

f>fy

Something like this I would do. Depends on what popups in my mind.

You should look into the easymotion plugin, which helps with arbitrary movements.

EDIT:

easymotion is rather worthless here, it is more useful for jumping to targets further away.

Upvotes: 5

blackbird
blackbird

Reputation: 967

In this case, I would use

W

to move to countryRepo, followed by

fy

Upvotes: 5

pfnuesel
pfnuesel

Reputation: 15310

If you know that it's the 4th y, you can do

4fy

If you know it's the last y in the line, you can do

$Fy

If you don't know at which position it is, you can still do

fy;;;

Upvotes: 19

Related Questions