0

I am very new to vim and I am using vs code vim extension. I have to frequently replace some words for that I use * command example is shown below

const FooBar = ()=>{

//some code

}

export default FooBar

If I want to change FooBar to say JhonDoe I go to FooBar in normal mode press * it takes me to next occourance of FooBar in the file I use ciw and change the word to JhonDoe and now I just have to press n to go to next occourance and . to change the word if I want to change the word. This is very convinient and gives me a lot of control.

However, I want to do the same thing but I want to change Foo to Pas how can I do that in a similar way. I want someting like I can highlight Foo in visual mode change the word and go to next occurance of Foo, not entire word and press . to change it

I want something similar to what vs code provides in ctrl + D shortcut but with better control

Nisha Dave
  • 669
  • 1
  • 9
  • 25

1 Answers1

1

What you are looking for is gn, the gn search forward for the last used search pattern, like with n, and start Visual mode to select the match (details see :help gn). For example:

  1. use /Foo to search Foo
  2. use cgn to change the Foo to Pas
  3. now you can press n to go to the next occurrence and . to repeat the last cgn if you want.

Another question is about visual star: What you want is Visual select the Foo, then press * to start search. Then the rest of the operation is exactly the same as before. This is a frequently duplicated question.

See vscodevim included in the question tag, so just setting the vim.visualstar option to true is enough if you use Vim in VSCode. Otherwise, please see Search for visually selected text or SO: Search for selection in Vim.

Rocco
  • 471
  • 1
  • 3
  • 7
  • Note that VSCodeVim doesn't have `gn`, which, sadly, makes the first half of your answer irrelevant for OP. – romainl Feb 23 '23 at 11:09
  • @romainl Maybe I didn’t understand what you mean correctly. But I just checked my *vscodevim*, the version of **Vim** extension is `v1.24.3`, and it supports `gn`. – Rocco Feb 23 '23 at 11:28
  • Searched issue [#1955](https://github.com/VSCodeVim/Vim/issues/1955) in [VSCodeVim/Vim](https://github.com/VSCodeVim/Vim), the `gn` feature has been implemented in PR [#2589](https://github.com/VSCodeVim/Vim/pull/2589). As with all other commands, **vscodevim** may not have the full **Vim** experience. – Rocco Feb 23 '23 at 11:49