When reading input from stdin, the output includes line numbers by default:
$ echo hi | rg i
1:hi
It would be nice for rg not to print line numbers in this case.
Although the -N flag can be used to turn off line numbers, when using rg in a pipeline, it would be more convenient not to have to.
Also ag does not print line numbers:
$ echo hi | ag i
hi
On the other hand, I have aliased ag to always show the line numbers, using its --line-numbers option :) So looks like there is no one _right_ way to do this; it's purely personal choice based.
I can go both ways here, but I should clarify one thing that is kind of important:
when using rg in a pipeline
If rg is used in a pipeline, then it should do the right thing. Note the difference between these commands:
$ rg 'Sherlock Holmes' short.txt
5:Chapter 1. Mr. Sherlock Holmes
7:Mr. Sherlock Holmes, who was usually very late in the mornings, save
$ cat short.txt | rg 'Sherlock Holmes'
5:Chapter 1. Mr. Sherlock Holmes
7:Mr. Sherlock Holmes, who was usually very late in the mornings, save
$ cat short.txt | rg 'Sherlock Holmes' | cat
Chapter 1. Mr. Sherlock Holmes
Mr. Sherlock Holmes, who was usually very late in the mornings, save
The first two show line numbers, but only because rg knows it's writing to a tty. In the last one, rg is in a pipeline, so it defaults to standard grep output.
It does kind of feel like the first two should be consistent with each other. One might argue that this is a little too smart, though.
Oh, I see. I was building up a pipeline and was looking at the output of rg before piping it to the next stage. I didn't realize that the numbers would go away when the next stage was connected.
The current behavior is good, but somewhat surprising.
@jzinn Oh interesting, completely forgot about this ticket! Thanks. :-)
@BurntSushi, thanks for this amazing utility. A core part of my workflow is ripgrep and
rg foo -l | xargs perl -i -ple 's/foo/bar/'
I type it all day long.