代码之家  ›  专栏  ›  技术社区  ›  nnyby

使用find-exec编写VIM脚本

  •  0
  • nnyby  · 技术社区  · 16 年前

    我有一个vim脚本,它执行多行搜索和替换:

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

    那是有效的。但是,当我把它放在find-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
    

    这真的很奇怪,因为这是正确的regex,我可以在vim中成功地手动搜索它。

    你能看到我的find语法有什么明显的错误吗?

    3 回复  |  直到 16 年前
        1
  •  4
  •   nnyby    16 年前

    我明白了!搜索模式需要一个%字符: vim -c '%s/switch/to/' 是正确的语法。

        2
  •  2
  •   dash-tom-bang    16 年前

    当我做这样的事情时,我倾向于在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
    qb@a@bq
    @b
    
        3
  •  0
  •   verisimilidude    16 年前

    您必须避开exec短语中的大括号(假设您没有,可能是论坛帖子在后面加了斜杠)。它应该结束于