Pablo Burgos
Pablo Burgos

Reputation: 1254

Is there a Vim motion command that put cursor at beginning of current word?

Let's suppose I have the cursor located as depicted on next image

cursor over text

Pressing b in normal mode I can go to the start of the word.

enter image description here

Great!

If I move cursor to 1 like

enter image description here

and press b we have

enter image description here

Question:

Is there a motion command to move cursor to start of the word so that if the word is one character long remains at the initial position? In my example, the cursor should stay at 1.

I'm looking this motion command to implement a Macro that in some of the steps move cursor to the start of a word, with words sometimes having just one character.

Upvotes: 7

Views: 5634

Answers (4)

awvalenti
awvalenti

Reputation: 2013

try this: "_yiw

user938271

Commented Apr 15, 2020 at 14:14

I found this solution awesome. It selects Black hole register "_ and then yanks current word to nothing. Since yank moves the cursor to the beginning of a word, it does the job, without affecting any register.

Upvotes: 0

Moustache
Moustache

Reputation: 464

Simplest solution I have found for this is just wb which works wherever your cursor is on the word, and for single character and multi character words.

Source is reddit user 'happysri' from this thread: https://www.reddit.com/r/vim/comments/1xzfjy/go_to_start_of_current_word_if_not_already_there/

Upvotes: 16

Pablo Burgos
Pablo Burgos

Reputation: 1254

I've found this answer Move to end of the current word in Vim that is somehow similar to my problem...

Applying the idea shown there, I should always move one character forward l and then move at the beginning of the word with b. That works and it is consistent for words of different sizes.

Upvotes: 3

Zorzi
Zorzi

Reputation: 792

I don't believe there is such a motion (please, anyone, correct me if I'm wrong)

But you can achieve that with a search:

:call search('\<', 'bc')
  • \< matches the beginning of a word

  • The b stands for backwards
  • The c is to accept matches under the cursor

Upvotes: 1

Related Questions