Trevor
Trevor

Reputation: 1035

Vim open a file found through grep

So I'm browsing a file in vim. I realize the function I am looking for is another file. So I do :! ack-grep "function tracking" ../ to look for it, and I see I need to examine file "../../tracking/api/ etc etc" some really long file name. what I'm doing now is trying to remember this file, type fg to get back into vim, and then :e "that really long file name". I'm wondering if there's an easier way to do this?

For instance, can I do something like vim -addtab <some file> from the command line once I've used ack-grep to find what I'm looking for, and then when I do fg to get back to vim, the file will be open in one a tab?

Awesome, lots of suggestions. I'll try them all out and in comments as I do. Then I'll pick what worked best for me once I've tried them all.

So this is the function that I settled on:

function! s:NewTabGrep(...)
  let args=split(a:1)
  if len(args) == 2
      let dir=args[1]
  else
      let dir='.'
  endif
  echom args[0] . "  " . shellescape(dir)
  tabnew | execute "r ! ack-grep ". shellescape(args[0]) ." ". shellescape(dir) 
endfunction
com! -nargs=? GrepTab call s:NewTabGrep('<args>')

This performs the search, and opens the results in a new vim tab. Then I can use CtrlP to open whichever file seems most interesting. Thanks to Merlin2011 for inspiration.

Upvotes: 8

Views: 2398

Answers (5)

David Lam
David Lam

Reputation: 4948

:vimgrep does this by default! it's the best! in fact, it's so cool, that you can even use :vim to invoke it hahaha

e.g. search all python files for "function tracking" (works best if you keep your vim working directory in your source code's root folder)

:vim /function tracking/ **/*.py

^ this will go to the first search result, and then open the quickfix window with a list of search results. I use a binding like nmap <silent> \` :QFix<CR> to quickly toggle it open and off. See... http://vim.wikia.com/wiki/Toggle_to_open_or_close_the_quickfix_window

--

however, there's a better way for navigating to the function: using CTRL-] which does an instant jump to the definition! Combined with CTRL-O and CTRL-I (jump backwards/forwards), you have an unstoppable move-around files-efficiently combo which will make you forget why you ever used IDE's =D

I googled this wiki page: http://vim.wikia.com/wiki/Browsing_programs_with_tags. One other thing you have to do is download Exuberant Ctags and build a tags file in your working directory: ctags -R ...then you're all set! (note that on Macs/BSD, it has its own ctags which wont work, you gotta use the exuberant one)

Upvotes: 2

shengy
shengy

Reputation: 9749

I recommend you to use the ack plugin., Note that if you have vim-dispatch installed, you can search with ack.vim asynchronously(You need to write: let g:ack_use_dispatch=1 in your vimrc file). Also I recommend ag to replace ack since it's really fast.

Upvotes: 0

Idan Arye
Idan Arye

Reputation: 12603

Vim has the :grep command that runs grep and stores the results into the quickfix list(you can also use :lgrep to load them to the location list). :grep also jumps to the first result, but if you don't want that you can use :grep! to just load the results to the quickfix list.

Once the results are loaded, you can use :copen to open the quickfix list(or :lopen to open the location list) and navigate between the results, or use :cfirst, :cnext, :cprevious and :clast(or :lnext, :lfirst, :lprevious and :llast) for navigation.

So, if you want to open the result in a new file, you have three options:

  1. :grep! "function tracking" ../**/*, :copen to open the quickfix list, put cursor on the first result, ctrl-W Enter to open the result in a new window, ctrl-W T to move the window to a new tab.
  2. :grep! "function tracking" ../**/*, :tabnew to open a new tab, :cfirst to jump to first result.
  3. :tabnew to open a new tab, :grep "function tracking" ../**/* to perform the search and open the first result automatically.

You might find it easier to just use the quickfix list for navigation instead of opening new tabs.

:grep uses grep by default, but you can set the grepprg option to make it use ack-grep instead(you might also need to set grepformat to recognize the output). You can also use :vimgrep to do a search with Vim's regular expression without using an external program, but it will be slower.

Upvotes: 1

merlin2011
merlin2011

Reputation: 75565

You can do :r ! ack-grep "function tracking" ../ to pull the output directly into the vim buffer, and then use gf to navigate to the file under the cursor.

If you want to open the file in a new tab instead, you can do Control-W, gf in normal mode.

Update: If you want the output of the command to appear in a new tab, you can do the following, which opens a new tab, and then immediately pulls the output of the command into it.

:tabnew | r!ack-grep "function tracking" ../

Upvotes: 5

Ry-
Ry-

Reputation: 224913

:exec 'tab split '.fnameescape(system('ack-grep "function tracking" ../'))

should do the trick. (Make it into a function!)

Upvotes: 0

Related Questions