Timur Fayzrakhmanov
Timur Fayzrakhmanov

Reputation: 19617

Why * (asterisk) immediately go to the next occurrence in vim?

Let's see a simple example:

Some text TOM, some text
           ^ (my cursor here)
Some TOM, some text

After pressing * I get:

Some text TOM, some text
Some TOM, some text
     ^ (now cursor here)

It always jump to the next word and it's annoying. I just want it to stay at the same place including TOM to the search pattern (for the next occurrence pressing n). It's useful when I want to refactor variable name starting from the cursor.

For example if * simply include a word to search pattern I could do the following:

tom := "some text"
 ^ (cursor)
func test() {
  println(tom)
}

*(add old name to pattern) ciw(change a word) newname(write new one) n(next occurrence) .(repeat last command). Is there any option to change behaviour?

Upvotes: 6

Views: 3338

Answers (1)

romainl
romainl

Reputation: 196779

You only need `` (2 backticks, Gary Bernhardt calls it "tick-tick-back") to jump back:

*``ciwfoo<Esc>n.n.

Note that you can use cgn instead of ciw and skip the n step:

*``cgnfoo<Esc>..

And remap it if you don't like to press "tick-tick-back":

nnoremap <key>       *``cgn
nnoremap <other-key> #``cgN

Upvotes: 9

Related Questions