What do you use to search through your source code when no tag exists?

Hi,

So I generated tags file using ctags. There is an “@” variable that isn’t tagged. What’s the best way to grep through your source files when there isn’t a tag generated? Do you use a command like:

:grep "pattern" *.rb */*.rb */*/*.rb */*/*/*.rb

In my vimrc:

" Use Silver Searcher instead of grep
set grepprg=ag

Then grep variable_name.

Done.

1 Like

I use ag as well, but through the mileszs/ack.vim plugin.

" Ack in Project. AckG matches file names in the project, regular Ack looks inside those files
map <Leader><C-t> :AckG<space>
map <Leader><C-f> :Ack!<space>
let g:ackprg = 'ag'

So my workflow is like this:

  1. Position cursor on the word/variable/function/whatever you want to search across the whole project.
  2. Use one of the key combos specified above (99% of the time for me it’s <Leader><C-f>)
  3. While vim is in command mode waiting for input I type <C-r><C-w> which will put the word under the cursor as the text in the command area of vim.
  4. Press enter

Then the quickfix list is populated with the results and (with the help of tpope/vim-unimpaired) I can ]q through them forwards or [q through them backwards.

Note: I forget exactly why I didn’t use rking/ag.vim directly. I tried it and I think I liked some of the shortcuts or the way ack.vim worked better. And since you can swap out the back-end with AG it was a win-win. I also have a pretty tweaked out ~/.ackrc so that probably played into it.

Another way I often use this is for project-wide search and replace, there’s just one more step you need to do after doing all of the above.

  1. :Qdo %s/specify_your_search_term_again/new_replacement_text/g
  2. Press enter

This will use the project-wide search that populated the quickfix to perform the specified action passed to :Qdo for each file in the quickfix list. Pretty handy! More info here: Project-wide search-and-replace in Vim with :Qdo – The Pug Automatic (See in particular the Qdo section, and you’ll need to install Henrik’s fork that he mentions to get Qdo functionality).

2 Likes

Also, if you haven’t seen this excellent post from tpope yet on auto-generating ctags when doing a git checkout, you definitely need to read it! tbaggery - Effortless Ctags with Git

1 Like