弹冠相庆

October 6, 2008

在VIM中搜索文件夹

Filed under: Uncategorized — Corey @ 20:19
Tags:
现在主流的编辑器都有搜索文件夹的功能,VIM同样也能实现。

vimgrep是VIM寻找模式匹配的内部方法,可以自动识别换行符和编码,并且
可以使用VIM强大的正则表达式。缺点是相对慢些,因为所有文件都要读
入内存。

语法
:vim[grep][!] /{pattern}/[g][j] {file} …
            Search for {pattern} in the files {file} … and set
            the error list to the matches.
            Without the ‘g’ flag each line is added only once.
            With ‘g’ every match is added.

            {pattern} is a Vim search pattern.  Instead of
            enclosing it in / any non-ID character (see
            |’isident’|) can be used, so long as it does not
            appear in {pattern}.
            ‘ignorecase’ applies.  To overrule it put |/c| in the
            pattern to ignore case or |/C| to match case.
            ‘smartcase’ is not used.

            When a number is put before the command this is used
            as the maximum number of matches to find.  Use
            ":1vimgrep pattern file" to find only the first.
            Useful if you only want to check if there is a match
            and quit quickly when it’s found.

            Without the ‘j’ flag Vim jumps to the first match.
            With ‘j’ only the quickfix list is updated.
            With the [!] any changes in the current buffer are
            abandoned.

来个简单的例子
在 d:mydocs下查找含有“弹冠相庆”的txt文档,但不跳到第一个匹配
:vimgrep /弹冠相庆/gj d:/mydocs/*/*.txt
如果要包含子文件夹,则用

:vimgrep /弹冠相庆/gj d:/mydocs/**/*.txt
打开quickfix窗口查看匹配结果
:cw

参考资料:

http://vimcdoc.sourceforge.net/doc/quickfix.html

:h vimgrep

1 Comment »

  1. 大清早写文章,厉害!

    Comment by 淑怡 — October 7, 2008 @ 01:16 | Reply


RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Create a free website or blog at WordPress.com.

%d bloggers like this: