Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vim open a file found through grep

Tags:

grep

bash

vim

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.

like image 846
Trevor Avatar asked Jun 03 '14 23:06

Trevor


2 Answers

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" ../
like image 76
merlin2011 Avatar answered Oct 14 '22 07:10

merlin2011


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

like image 44
David Lam Avatar answered Oct 14 '22 06:10

David Lam