3

I am using Vim in my development. Also using :vimgrep to search for text in files within Vim. But while vimgrep is searching, I can't do anything inside Vim until it finishes.

A long time ago, I saw a video in Youtube where you can grep inside Vim without blocking the text editor. Sadly, can't find this video now.

How can you run grep inside vim, display the results in buffer (in quickfix or whatever) similar to vimgrep without blocking the editor? Any ideas?

1

5 Answers 5

4

That's a neat way since vim 8 to use asyncrun.vim plugin to run your shell commands and read output in the quickfix window in realtime.

Once installed, grep can be run asynchronously with ':AsyncRun grep '.Output are displayed in the quickfix window, You can explore the match output immediately or keep working in vim while executing.

2

There are a few ways.

The easiest way is by installing a plugin. For example, http://www.vim.org/scripts/script.php?script_id=3431 has built-in commands for grep, make, and a few others.

Another method (if you know python or Perl or TCL or something like that) is to use an external scripting language with thread support, and call into it with Vim's interface to that language. This can get complicated and relies on having Vim compiled with support for that scripting language.

Finally, the built-in method uses Vim's "client-server" features. The idea is to invoke an external command (with system() or :!start cmd), where cmd will run the search and then call back into Vim using --remote-expr and a pre-defined file name. The expression to have Vim execute should be a function that reads the file in with :cfile or similar to get your search results. See http://vim.wikia.com/wiki/Execute_external_programs_asynchronously_under_Windows#Getting_results_back_into_Vim for a fully developed example of this method. I think on Linux you can use the !cmd & method of running a command in the background in place of !start cmd as on Windows.

0

Dispatch is also a great plugin for asynchronous operations:

Leverage the power of Vim's compiler plugins without being bound by synchronicity. Kick off > builds and test suites using one of several asynchronous adapters (including tmux, screen, > iTerm, Windows, and a headless mode), and when the job completes, errors will be loaded and > parsed automatically.

https://github.com/tpope/vim-dispatch

0

You can do a forward search by pressing / and typing the string you are searching for. ? for backwards searches. Pressed n to repeat your search, N to go back to the results of the last search.

0

Vim 8 has native asynchronous jobs:

Vim jobs start with a channel, you can use job-options to redirect output to a buffer. For example:

:let job = job_start('ls -alh', {'out_io': 'buffer', 'out_name': 'mybuffer'})

then

:sbuf mybuffer

to read it.

Ref: :h job-start

Source: an answer from the Vi and Vim StackExchange site

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .