vimvscodevim

how to effeciently search and change a specific text vim


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


Solution

  • 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.