在vim中 :s(substitute)命令用於查找並替換字符串。使用方法如下:
:s/<find-this>/<replace-with-this>/<flags>
例如:
1 :%s/foo/bar/g # 在全局范圍內(%)查找foo並將之替換為bar,所有出現都會被替換(g) 2 :s/foo/bar/g # 在當前行內查找foo並將之替換為bar,所有出現都會被替換(g) 3 :'<,'>s/foo/bar/g # 在選區內進行替換,Visual模式下選擇區域后輸入會自動補全'<,'>
下面是一些可以加的flag. for example, :s/cat/dog/gi 會把cat.Cat() 變成 dog.dog().
g —global replace: replace every occurrence of the pattern, not just the first one
c —confirm each substitution: prompt the user before replacing the text
e —do not show errors if no matches are found
i —ignore case: make the search case-insensitive
I —make the search case-sensitive
最后說一個小tip: 程序員喜歡用foo和bar指代變量名: foo bar -> Fucked Up Beyond All Repair 完全無法修補。