2012-07-02 224 views
2

我有我的劇本很多行看起來像:正則表達式搜索替換

./run -f abc.txt 
./run -f abc1.txt 
./run -f abc2.txt 
.. 
.. 
./run -f abc50.txt 

我需要abc.txt全部更換abc*.txt。有沒有在Vim的方式我可以做到這一點,我在哪裏搜索所有abc*.txt並取代它們?

回答

5

你可以這樣做:

:%s/abc\d*.txt/abc.txt/g 
+0

@ZnArK沒必要,你可以試試。 – xdazz

+0

太懶,以爲你不得不 – ZnArK

1

總之你可以試試:(可能需要輕微的按摩)%s/abc\d*\.txt/abc\.txt/g

檢查此頁以瞭解更多資訊。

http://vim.wikia.com/wiki/Search_and_replace

:%s/foo/bar/g 
Find each occurrence of 'foo', and replace it with 'bar'. 
:%s/foo/bar/gc 
Change each 'foo' to 'bar', but ask for confirmation first. 
:%s/\<foo\>/bar/gc 
Change only whole words exactly matching 'foo' to 'bar'; ask for confirmation. 
:%s/foo/bar/gci 
Change each 'foo' (case insensitive) to 'bar'; ask for confirmation. 
This may be wanted after using :set noignorecase to make searches case sensitive (the default). 
2

我會添加此不同的方式去做多樣性的名字,但我覺得標準,最簡單的方法是@xdazz方法。

:%g/abc/norm!fclvf.hx 

當我們使用%g我們正在申請中的命令給所有匹配拳頭參數(在這種情況下abc這意味着所有對它們有abc的行)的行。該norm!命令意味着執行命令,如在正常模式下fc意味着找到信cl手段去左邊,v啓動可視模式,f.找到A點,h往右走,最後x刪除可視化字。

這種方法的好處是,您可以將複雜的宏應用於選定的行。