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()所有这些模式都将被执行。因此,我不需要担心某些文件类型的具体说明。

有人可以帮我解决这个问题吗?

4

1 回答 1

1

如果没有匹配-1则从match(). 此外,您可能不需要调用readfile()来检查文件名。因此,改变

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

...至...

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

...并且您的blah-文件(并且只有您的blah-文件)将执行额外的替换。

于 2013-02-18T04:15:28.700 回答