且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Vim - 搜索并替换结果

更新时间:2023-12-05 15:09:52

如果您已经完成了搜索,只需在替换命令中省略模式即可替换相同的模式。例如:

  /关键字

搜索keyword,然后:

 :%s // new_keyword / g 

会将所有出现的keyword替换为new_keyword。

I'm getting more and more comfortable with Vim after a few months. BUT, there is only one simple feature I can't get any answer from the web. That is "Search and replace the results". The problem is that I know:

:/keyword to search, and hit enter "keyword" will be highlighted (of course with set hlsearch) n, or N to navigate

:% s/keyword/new_keyword/g to replace all occurences of keyword with new_keyword.

BUT, I would think that there must be a way to search, and replace the matched keyword (highlighted) with any new_keyword WITHOUT doing ":% s/keyword/new_keyword/g", which is a lot of typing considering search & replace is such a day-to-day feature.

Any answers/comments will be greatly appreciated!

If you've already done a search you can do a substitution for the same pattern by simply leaving out the pattern in the substitute command. eg:

/keyword

searchs for "keyword", and then:

:%s//new_keyword/g

will replace all occurrences of "keyword" with "new_keyword".