1

Hi i'm finding the simplest way to search in vim, i don't have anything on my workflow yet, only the / command. Currently i'm using this line

 map <F3> :execute "vimgrep /" . expand("<cword>") . "/j **" <Bar> cw<CR>

On my .vimrc so i put the cursor on the word, hit F3 and it searches for that word on the current dir and subdirs, i wish i could change the <cword> for the contents of the current visual block and use grep instead, because vimgrep is too slow the problem is grep doesnt output to the quick fix window. Any ideas on doing searches with the content of the current visual block? Or redirecting the ouput of grep to the quick fix list?

Guillermo Siliceo Trueba
  • 4,251
  • 5
  • 34
  • 47

2 Answers2

1

Vim has a :grep command that behaves like :vimgrep. I think this should give you what you want:

map <F3> :execute "grep " . expand("<cword>") . " **" <Bar> cw<CR>

BTW, seeing your use case (searching for the word under the cursor), you may want to try using tag files ( http://vim.wikia.com/wiki/Browsing_programs_with_tags ).

Arnaud Le Blanc
  • 98,321
  • 23
  • 206
  • 194
  • you very rarely want to use `:map`. You probably want to use `nnoremap`. – Peter Rincker Sep 19 '11 at 17:46
  • thats for the grep part of the question, thanks i just need to find out a workaround for the limitation of not being able to use a range with those commands. – Guillermo Siliceo Trueba Sep 19 '11 at 18:02
  • Grep works line by line. There are a number of alternatives, though. This [SO question](http://stackoverflow.com/questions/152708/how-can-i-search-for-a-multiline-pattern-in-a-file-use-pcregrep) might help. – romainl Sep 19 '11 at 18:40
0

You can find a friendlier version of the mapping above in this gist. Just save it to a file (say ~/vimscripts/fastgrep.vim)

Add to your .vimrc:

source ~/vimscripts/fastgrep.vim

now you can:

highglight the word in vim and hit \g (i.e. <leader> g)

You'll be prompted (in vim prompt area):

Search for text: <highlighted_word_but_you_can_change_it>
Files to search: *.rb

Hit enter, and you'll see results in the QuickResults window so you can just click to jump to those lines or use :cp :cn to navigate through the matches.
If you'd like to change *.rb to whatever you most frequently search in, just edit the vimscript and source it again.

paneer_tikka
  • 6,232
  • 1
  • 20
  • 17