Wilfred / ag.el

An Emacs frontend to The Silver Searcher
http://agel.readthedocs.org/en/latest/
525 stars 61 forks source link

Searching multiple paths at once #155

Open harold opened 5 years ago

harold commented 5 years ago

On my machine, the output of ag --version is:

$ ag --version
ag version 2.1.0

Features:
  +jit +lzma +zlib

My Emacs version is:

$ emacs --version
GNU Emacs 25.2.2
Copyright (C) 2017 Free Software Foundation, Inc.
GNU Emacs comes with ABSOLUTELY NO WARRANTY.
You may redistribute copies of GNU Emacs
under the terms of the GNU General Public License.
For more information about these matters, see the file named COPYING.

I'm using:

$ uname -a
Linux wintermute 4.15.0-43-generic #46-Ubuntu SMP Thu Dec 6 14:45:28 UTC 2018 x86_64 x86_64 x86_64 GNU/Linux
$ lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description:    Ubuntu 18.04.1 LTS
Release:    18.04
Codename:   bionic

From the command line I can do something like:

harold@wintermute:~ []$ mkdir -p test/a test/b
harold@wintermute:~ []$ echo x > test/a/foo.txt
harold@wintermute:~ []$ echo x > test/b/bar.txt
harold@wintermute:~ []$ mkdir test2
harold@wintermute:~ []$ cd test2/
harold@wintermute:~/test2 []$ ag x ~/test/a ~/test/b
/home/harold/test/b/bar.txt
1:x

/home/harold/test/a/foo.txt
1:x

Searching for x in files in test/a and test/b simultaneously.

Is this possible in ag.el? Perhaps by some clever use of ag-project-root-function?

Ultimately I am working on an app, and would like to search the app's code and some of the libraries that the app uses simultaneously.

Thanks in advance for your time and consideration, this package is rad and I use it every day.

vitorqb commented 5 years ago

The PR #157 added a functionality similar to what you are looking for. With elisp you can do something like

(ag/search "x" "/home/vitor" :files '("test/a" "test/b"))
-*- mode: ag; default-directory: "~/" -*-
Ag started at Wed Jul 31 20:30:27

ag --literal --group --line-number --column --color --color-match 30\;43 --color-path 1\;32 --smart-case --stats -- x test/a test/b
File: test/a
1:1:x

File: test/b
1:1:x
2 matches
2 files contained matches
2 files searched
2 bytes searched
0.000325 seconds

Ag finished at Wed Jul 31 20:30:27

I hope this helps!

harold commented 5 years ago

Woah, that's neat! Thanks. I will play with this.

vitorqb commented 5 years ago

Nice :) . There is also the unmerged pr #158, it tries to add a nice integration with dired. If/once it gets merged you can do something like this gif: https://gist.githubusercontent.com/vitorqb/291b7129effd0f85885fb30cfdcf6ebf/raw/1e18f2fb88c28a7258732319e7b2893f3d17c0a5/img.gif

harold commented 5 years ago

Ha! That's nuts 🥜.

That's a few too many steps for me. What I think I'd like, and what I may try to do with the new list :files argument is to have a way of quickly specifying some root search paths (maybe a file in my project's root directory, or maybe just some hardcoded set of search paths) and then to be able to quickly search all those, maybe with a sequence as short as M-x ag-something RET search-string RET.

Definitely doing it in general involves either a lot of configuration or a lot of steps. I sometimes find myself going out to my terminal and just doing cd ..., then ag --clojure search-string. This is quite general and pretty fast, but two obvious downsides (1) the results aren't in an emacs buffer for going between them w/ next-error and (2) no good way to do excludes if there are lots of siblings in the parent folder.

Regardless, I appreciate your efforts here and hope we all have fun searching for a long time to come.