2

How do I ag search into a specific set of folders using the -G option?

Here's an example where I use -G routes, but it's picking up another result from another folder, because routes is still in the path. Yet if I try -G ^routes, it doesn't seem like the regex is taking?

enter image description here

On that note, Atom has a nice path searching syntax which lets me do things like app,routes,!storage (search in app and routes folders, but ignore storage folder). Since I've switched to vim, I'm finding it hard to get a searching workflow down with ack/ag. Anyone have any tips for me?

Jesse Leite
  • 6,581
  • 3
  • 18
  • 17

1 Answers1

2

I suppose you'd really like to try this plugin https://github.com/junegunn/fzf.vim. It will (among other things) run your ag search and present the result in a fuzzyfinder. Then you'll have the very same feature you mention in atom by running :Ag middleware followed by(in the fzf window) 'app/ 'routes/ !storage/'.

about the use of regex for the file pattern, you'll need to quote your regex

ag -G '^routes' middleware

that being said, the '^...' doesn't work as intended here on my computer either, although '\b' does. I started using ripgrep instead of the-silver-searcher not so much because it's faster but rather because it has a better documentation, maybe you'd like to give it a try. rg -g will take a glob which is easier to understand than the "FILEPATTERN" mentioned in the ag man page

sudavid4
  • 1,081
  • 6
  • 14
  • Thanks for the reply @sudavid4! I already use and love fzf.vim, but prefer :Ack (to fzf.vim's :Ag) because of it's integration with the quickfix window, which can make larger refactors easier. I wonder why the `^` regex doesn't work though? – Jesse Leite Feb 12 '18 at 16:58
  • 1
    fzf has a select-all that populates the quickfix though. If you wish to search->filter results -> populate quickfix with a subset of the original results. It's alt+a by default which doesn't work well on mac so I've changed it for my use, this is the file/line you'd need to change if alt+a doesn't work for you(https://github.com/junegunn/fzf.vim/blob/master/autoload/fzf/vim.vim#L687) – sudavid4 Feb 12 '18 at 18:12
  • Oooo, I was looking into populating quickfix a bit ago but couldn't figure it out. I'll take another look thanks! – Jesse Leite Feb 12 '18 at 18:18
  • Using the fzf fuzzy search after is definitely helpful, but sometimes too fuzzy in certain scenarios it seems. If I could just figure out why `^` regex won't work with `-G`. It would also make running previous searches a bit easier, as all the params would be in vim's command history. – Jesse Leite Feb 20 '18 at 22:31
  • 1
    You should look more into [fzf extended-search](https://github.com/junegunn/fzf#search-syntax). E.g:`^routers` inside the fzf window will filter out everything that doesn't start with routers. `^routers 'boo` say 'must start with routers and have an exact match boo'. `^routers 'boo !maa faa` say 'start with routers, have an exact boo match, not have an exact match maa and fuzzy search faa`. It's actually stronger than hardcoding this in your ag search. If you still really want it in your search, seriously, try the ripgrep. It's faster and better documented than ag. – sudavid4 Feb 21 '18 at 06:07
  • Nice, the fzf extended-search syntax workflow is . I'll take a look at ripgrep as well. Before I mark this as answered (sorry for holding it hostage ), how are you populating your quickfix with fzf's `:Ag` results? After googleing a while, I finally figured out how to get this stuff in quickfix with https://github.com/junegunn/fzf.vim/issues/185#issuecomment-322120216 , but I can't hit enter on a quickfix item to open that result in a buffer. Have you figured this out? – Jesse Leite Feb 23 '18 at 20:06
  • Oh nevermind! I had a `` mapping that was messing with my quickfix window. Thank you so much again for your ideas! – Jesse Leite Feb 23 '18 at 20:46