开发者

Is there a way to configure Vim grepprg option to avoid waiting until the external tool has finished searching?

I am a long time Vimmer. However, I keep switching to shell to make searches. This avoids me to use the quickfix functionality.

The main reason for switching to shell is that when I use grep from inside Vim (with :grep), I cannot follow progress.

Because the code base I search is usually wide, I really appreciate immediate feedback.

It gives me a chance to find out that my search expression is开发者_如何转开发 wrong before the full results have been displayed.

This allow me to cancel the search, refine the expression then relaunch the search.

Any hint how to reproduce this pattern inside Vim would be appreciated.


I don't see the same vim behaviour as you. When I run :grep, I still see the results in vim (not in the quickfix) before the search completes (but I cannot do anything until the search is done).

I even tried using no vim settings or plugins:

gvim -u NONE -U NONE

If that's not your behaviour, check your grepprg. Mine is the default:

:verbose set grepprg
grepprg=grep -n $* /dev/null

When I use run grep -e "score" -R /etc I see this output in vim:

:!grep -n -e "score" -R /etc /dev/null 2>&1| tee /tmp/voLcaNS/232

It's possible that your system is missing tee or your vim doesn't use it (I'm using Vim 7.2 on Ubuntu 10.10). tee takes the text passed to it and writes it to a file and to stdout.


If you're looking for a way to have the quickfix get updated with your search results and have vim not block while you're searching, then you could write a script that:

  • searches with grep as a background process and redirects to a file
  • every second until grep completes, have vim load the file in quickfix (cgetfile) (you can tell vim to do something from another process with --remote-expr)

You can try my AsyncCommand plugin to get your code started. It does the above, except that it only loads the file when the search is complete.


Are you familiar with ack.vim at all? It doesn't use the quickfix window, but uses a separate buffer in a split. However, it's rather faster results come right back to the vim frame.


This may be due to buffering between grep and tee, not vim itself. To test this theory, run grep from the command-line and pipe the output through tee (i.e. grep <pattern> <files> | tee temp.out). If it behaves the same as you observe within vim, then buffering is occurring.

To work around, install expect (sudo apt-get install expect-dev on Ubuntu 10.10) and grepprg to unbuffer grep -n $* /dev/null. (See Turn off buffering in pipe).


Take a look at :vimgrep in the online documentation. It displays the file name being searched and updates as it goes.


There are three ways to do a search in entire projects.

  • System command grep(fast, but not working well with Ouickfix list)

    =>$ grep -n Example *
    
  • Vim internal grep(slow, but have a strong pattern support)

    :vim[grep] /{pattern}/[g][j] {file} ...
    
  • System plugin ack(perfect)

    1 install ack

    brew install ack
    

    2 add below configs to your .vimrc

    :set grepprg=ack\ --nongroup\ --column\ $*
    
    :set grepformat=%f:%l:%c:%m
    

    3 then you can use grep to call ack in vim like

    :grep "object\." app/**/*.rb
    
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜