2010-10-13 70 views
0

我有一點點的Vim腳本,做了多行查找和替換:腳本的Vim找到-exec

vim -c 's/^ *<hi a=\"26\">\nHello/<td height=\"26\">\r<\/table>\r<bla \/>' \ 
    -c 'w!' -c 'q' test.html 

工程。然而,當我把在發現-exec做這個遞歸目錄:

find . -iname 'test.html' -exec \ 
    vim -c 's/^ *<hi a=\"26\">\nHello/<td height=\"26\">\r<\/table>\r<bla \/>' \ 
    -c 'w!' -c 'q' \ 
{} \; 

的test.html保持不變,和Vim給了我這個錯誤:

Pattern not found: 
^*<hi a=\"26\">\nHello 
in ./test.html 

這真是奇怪因爲這是正確的正則表達式,我可以在Vim中手動搜索它併成功。

你能用我的查找語法看到任何明顯的錯誤嗎?

+0

當你只是一個'-print',而不是'-exec',不是列表的test.html運行'find'是否正確? – bstpierre 2010-10-13 19:11:42

+0

是的,它確實列出test.html當我只是做 - 打印。當我執行--exec時,它成功地用Vim打開文件。 – nnyby 2010-10-13 19:16:46

回答

3

我想通了!搜索模式需要%字符:vim -c '%s/switch/to/'是正確的語法。

0

你必須逃避執行短語中的大括號(假設你沒有,也許論壇張貼吃了反斜槓)。它應該結束\ {\} \;

+0

謝謝,我躲過了花括號。我原來的問題仍然存在,但:( – nnyby 2010-10-13 19:30:03

2

當我做這樣的事情時,我傾向於在vim中做到這一點。

:vimgrep /^ *<hi a=\"26\">\nHello/ **/* 

其次是遞歸宏的創建和執行:

qbq 
qa (actually you don't press enter after this) 
:silent! !p4 edit % " check out the file 
:e " refresh the r/w status 
:%s/^ *<hi a=\"26\">\nHello/<td height=\"26\">\r<\/table>\r<bla \/> 
:w " no need to force write if it's r/w 
:cnf 
q 
[email protected]@bq 
@b