Reputation: 1254
Let's suppose I have the cursor located as depicted on next image
Pressing b in normal mode I can go to the start of the word.
Great!
If I move cursor to 1 like
and press b we have
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
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
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
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
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 wordb
stands for backwardsc
is to accept matches under the cursorUpvotes: 1