the_silver_searcher icon indicating copy to clipboard operation
the_silver_searcher copied to clipboard

ag should include line number after the filename

Open ssbarnea opened this issue 6 years ago • 9 comments

In order to improve accessibility of the tool and allow users to open files at exact position where the match was found, ag should include the linenumber and eventually even the column number just after the filename, using the : separator.

This notation is something we can already call defacto standard for most, in not all tools like compiliers, linters and other tools returning matches.

If we do this people would be able to click the filename in the terminal and open it directly at the right position.

Current behaviour:

ag "yncing the local repository to the remote host"                                                                                                                                                          [10:07:11]
rtox/rtox.py
165-
166-    # Clone the repository we're working on to the remote machine.
167:    print('Syncing the local repository to the remote host...')
168-    subprocess.check_call([
169-        'rsync',

Proposed behaviour:

ag "Syncing"                                                                                                                                                          [10:07:11]
rtox/rtox.py:167:11
165-
166-    # Clone the repository we're working on to the remote machine.
167:    print('Syncing the local repository to the remote host...')
168-    subprocess.check_call([
169-        'rsync',

Note: on multiple matches on same file, filename should be repeated.

ssbarnea avatar Mar 02 '18 10:03 ssbarnea

ag has lots of output options, including for the filename. Have you looked at the help and man page?

Unfortunately, it still seems to have bugs that cause it to not output the filename even when --filename is specified...

alphapapa avatar Jun 16 '18 00:06 alphapapa

I think that adding the line number after the filename would greatly increase the usability of ag as we could click on results and open them in the editor directly.

ag-search-results

ssbarnea avatar Jun 27 '18 18:06 ssbarnea

Have you considered the --ackmate option? Or you might consider using another tool, like rg.

alphapapa avatar Jun 28 '18 02:06 alphapapa

@alphapapa I tried the --ackmate option but the outcome is not the desired one, is much harder to read and it does not solve the problem of clicking on the filename:line or filename:line:colum to open the file in you reditor at the right place.

In addition to that it also disables the coloring which is a very useful feature of ag as it improves readability, a lot.

I think that a tunning of the default output that would include the line after filename would sortout the problem just fine.

I would not mind trying to add the described feature myself but I would first like to hear if this kind of contribution would be accepted.

ssbarnea avatar Oct 19 '18 10:10 ssbarnea

I agree, due to the fact that I often open a file using Iterm2 on macOS, I need to scroll to the line number by myself >_<. In fact grep's output is better for opening a file but it's slower than ag.

wind2412 avatar Nov 13 '18 03:11 wind2412

Just following up on this.. is there really still no way to do this yet? Would love to be able to click on a file and be taken right to the result. 😆

digitalmaster avatar Oct 23 '21 13:10 digitalmaster

Sorry but I switched to ripgrep one or two years ago, nothing last forever.

ssbarnea avatar Oct 25 '21 10:10 ssbarnea

Indeed. Thanks.. will give that a shot 👍

digitalmaster avatar Oct 25 '21 23:10 digitalmaster

It a bit disappointing that this feature has been ignored.

For those coming here for a solution, a tiny little bit of scripting will do - the output structure is regular, so it can be trivially processed. This is a possible approach:

ag "$pattern" | perl -lne 'if (/^(.+?):(\d+)/ && !$files{$1}) { print "$1:$2"; $files{$1} = "printed" }'

The Perl script extracts filename and line, and, if they aren't in a hashmap that tracks them, prints the filename and line, and puts the filename into the hashmap (so that it isn't printed again).

64kramsystem avatar May 10 '22 22:05 64kramsystem