Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

grep

vim

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.

like image 359
Godefroid Chapelle Avatar asked Feb 01 '11 16:02

Godefroid Chapelle


3 Answers

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.

like image 91
idbrii Avatar answered Dec 13 '22 09:12

idbrii


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.

like image 29
sleepynate Avatar answered Dec 13 '22 07:12

sleepynate


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).

like image 42
Garrett Avatar answered Dec 13 '22 08:12

Garrett