VIM how to replace “AA” to “BB” in a file

喜欢而已 提交于 2019-12-25 02:39:11

问题


I need to replace a file which has thousand of lines code.

In the log, it has a specific word which I want to replace this work with some other words.

Say, how to replace all "AA" to "BB" in this file?

thanks


回答1:


You want to use

:%s/\<AA\>/BB/g

% specifies that you want to replace on all lines.

s tells vim that you want to do a substitution

Between the first / and second / is what you want to replace (ie \<AA\>).

The \< and the \> are word boundaries, making sure that AA is matched but fAA or AAg is not.

Between the second / and third / is your replacement (ie BB)

The final g says that you want to replace all occurances on a line (not only the first).




回答2:


Since you don't know one of the most basic and frequent commands in vi / Vim, the :substitute command, and apparently also aren't aware of the great built-in help and many many tutorials on the web (most of which cover the :s command), maybe the best approach (if you're using GVIM) is

:promptrepl

which will bring up a search-and-replace dialog that is very similar to that in other text editors. It's even accessible through the menu Edit > Find and Replace...


Learn how to look up commands and navigate the built-in :help; it is comprehensive and offers many tips. You won't learn Vim as fast as other editors, but if you commit to continuous learning, it'll prove a very powerful and efficient editor.



来源:https://stackoverflow.com/questions/22526475/vim-how-to-replace-aa-to-bb-in-a-file

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!