2013-02-18 44 views
2

我寫了一個函數,它在我正在編輯的文件上執行一些搜索和替換。但是對於某些文件(在文件名中有一些特定的關鍵字),我需要添加一些特定的搜索並將其限制爲這些文件。如何使用特定字符串在文件名上添加特定搜索和替換模式

我需要知道如何解決下面的代碼:

function! Test() 
    " basic search and replace for all the files 
    %s/I'll /I will /ge 
    " if the filename starts with "blah-" the following additional search and replace needed otherwise not 
    if match(readfile(expand('%:t')),"^blah-") 
     %s/could'nt /could not /gec 
    endif 
endfunc 

而且在調用該函數:call Test()所有這些模式將被執行。因此,我不需要擔心某些文件類型的具體說明。

任何人都可以幫我解決這個問題嗎?

回答

1

如果不存在匹配-1match()返回。另外,您可能不需要撥打readfile()來檢查文件名。因此,改變

if match(readfile(expand('%:t')),"^blah-") 

...到...

if match(expand('%:t'), '^blah-') != -1 

...和你blah-文件(只有你blah-文件)將有額外的替代執行。

相關問題