Vim Tips Wiki
Advertisement

Obsolete tip

This tip has been merged into another tip.
See VimTip1549 for the current tip.

Please do not edit this tip, and do not edit the discussion page.
If anything needs to be improved, please fix VimTip1549.


Tip 986 Printable Monobook Previous Next

created 2005 · complexity intermediate · author jheddings · version 6.0


Many times, I launch recursive searches from the current directory that take a while to run. These functions allow a search to run in the background and the results to be pulled in later (once the search completes).

" runs a search in the background
function! BackgroundGrep(search, fpatt)
  let g:bg_grep_file = tempname()
  " for Win32
  silent execute "!start cmd /c \"" . &grepprg . " -R " . a:search . " " . a:fpatt . " >" . g:bg_grep_file . " 2>&1\""
  echo g:bg_grep_file
endfunction

" loads the results of the last background search
function! LoadBackgroundGrepResults()
  if !exists("g:bg_grep_file")
    echohl ErrorMsg | echo "BackgroundGrep() must be run first" | echohl None
    return
  endif
  if !filereadable(g:bg_grep_file)
    echohl ErrorMsg | echo "Cannot open bg_grep_file: " . g:bg_grep_file | echohl None
    return
  endif
  execute "cfile " . g:bg_grep_file
endfunction

In order to make the functionallity work for Unix, you'll have to change the "silent execute..." line of the first function to use the Unix commands (and & to run in the background). It should be pretty straightforward to modify this line.

Also, I usually map the functions to something useful:

map <A-/> :call BackgroundGrep("<cword>", "*")<CR>
map <A-?> :call LoadBackgroundGrepResults()<CR>

Comments

Advertisement