ggreer / the_silver_searcher

A code-searching tool similar to ack, but faster.
http://geoff.greer.fm/ag/
Apache License 2.0
26.07k stars 1.42k forks source link

[Feature Request] --file-with-matches including line number of first match #715

Open eksperimental opened 9 years ago

eksperimental commented 9 years ago

One feature that I think it will be worth adding is listing the file with matches suchs as in "--files-with-matches" but appending the line number of the first match, separated by a colon, useful in combination with xargs and a text-editor that supports passing the line number when opening files.

ag "\s+def[^\n]+\\\\ *fn[^\(]"
lib/iex/test/iex/server_test.exs
49:  # Helpers
50:
51:  defp boot(opts, callback \\ fn -> end) do

lib/elixir/lib/stream.ex
409:  @spec into(Enumerable.t, Collectable.t) :: Enumerable.t
410:  def into(enum, collectable, transform \\ fn x -> x end) do
771:  @spec uniq(Enumerable.t, (element -> term)) :: Enumerable.t
772:  def uniq(enum, fun \\ fn x -> x end) do

I would like to have an option that lists just the files as in

ag "\s+def[^\n]+\\\\ *fn[^\(]" -l
lib/iex/test/iex/server_test.exs
lib/elixir/lib/stream.ex

but including line number of the first match

ag "\s+def[^\n]+\\\\ *fn[^\(]" --files-with-matches-line-number
lib/iex/test/iex/server_test.exs:51
lib/elixir/lib/stream.ex:410

so it could be piped into xargs and then into your favorite text-editor that supports opening files at a line number, such as "sublime text" and you could do ag "\s+def[^\n]+\\\\ *fn[^\(]" --files-with-matches-line-number | xargs subl and the files will be opened and the cursor with be at the right line that you need to edit.

eksperimental commented 9 years ago

I needed this feature so badly that I implemented a shell version myself. install the script here: https://git.io/ag-l1

and run it like:

ag-l1 "\s+def[^\n]+\\\\ *fn[^\(]"
lib/iex/test/iex/server_test.exs:51
lib/elixir/lib/stream.ex:410

combine it with your text-editor, to open the files at the right line: ag-l1 "\s+def[^\n]+\\\\ *fn[^\(]" | xargs subl