If you want to ignore some directories or files, use -exclude
argument. The argument accepts a regexp pattern.
Here is an example that ignores everything under the node_modules
folder:
$ gogrep -exclude '/node_modules$' . '<pattern>'
By default, gogrep
stops when it finds 1000 matches.
This is due to the fact that it's easy to write a too generic pattern and get overwhelming results on the big code bases.
To increase this limit, specify the -limit
argument with some big value you're comfortable with.
If you want to set it to the max value, use -limit 0
.
There is still a cap at some value (~100k), but it's not the case for the count mode (
-c
).
Count mode discards all match data, but prints the total matches count to the stderr
. Disabled by default.
When strict is false, gogrep may consider 0xA and 10 to be identical. By default, strict-syntax is disabled.
Sometimes you want to print the result in some specific way.
Suppose that we have this target.go
file:
func f() {
panic("unimplemented") // Should never happen
}
$ gogrep target.go 'panic($_)'
target.go:3: panic("unimplemented") // Should never happen
As you see, it prints the whole line and the match location, not just the match. If we want gogrep
to output only the
matched part, we can use the -format
flag.
$ gogrep -format '{{.Match}}' target.go 'panic($_)'
panic("unimplemented")
The default format value is {{.Filename}}:{{.Line}}: {{.MatchLine}}
.
Several template variables are available:
{{.Filename}} match containing file name
{{.Line}} line number where the match started
{{.MatchLine}} a source code line that contains the match
{{.Match}} an entire match string
{{.x}} $x submatch string (can be any submatch name)
By default, gogrep
prints the relative filenames in the output.
Technically speaking, it sets {{.Filename}}
variable to the relative file name.
To get an absolute path there, use -abs
argument.
$ gogrep target.go 'append($_, $_)'
target.go:3: append($data, $elem)
$ gogrep -abs target.go 'append($_, $_)'
/home/quasilyte/target.go:3: append($data, $elem)
Some patterns may match a code that spans across multiple lines.
By default, gogrep
replaces all newlines with \n
sequence, so you can treat all matches as strings without newlines.
If you want to avoid that behavior, -m
argument can be used.
// target.go
println(
1,
2,
)
$ gogrep target.go 'println(${"*"})'
target.go:2: println(\n 1,\n 2\n)
$ gogrep -m target.go 'println(${"*"})'
target.go:2: println(
1,
2,
)
gogrep
inserts ANSI color escapes by default.
You can disable this behavior with the -no-color
flag.
Specify color scheme for {{.Filename}}
format variable. By default, dark-magenta
color used.
This argument also can be specified by
GOGREP_COLOR_FILENAME
environment variable.
Specify color scheme for {{.Line}}
format variable. By default, dark-green
color used.
This argument also can be specified by
GOGREP_COLOR_LINE
environment variable.
Specify color scheme for {{.Match}}
format variable. By default, dark-red
color used.
This argument also can be specified by
GOGREP_COLOR_MATCH
environment variable.
Set the number of concurrent workers. By default, equal to the number of logical CPUs usable by the current process.
To work faster, gogrep
doesn't print any search results until it finds them all (or reaches the -limit
).
If you're searching through a big (several million SLOC) project, it could take a few seconds to complete. As it might
look like the program hangs, gogrep
prints its progress in this manner:
N matches so far, processed M files
Where N
and M
are variables that will change over time.
gogrep
has 3 progress reporting modes:
none
which is a silent mode, no progress will be printedappend
is the simplest mode that just writes one log message after anotherupdate
is a more user-friendly mode that will use\r
to update the message (default)
To override the default mode, use -progress
argument.
Logs are written to the
stderr
while matches are written to thestdout
.
A CPU profile that will be used to build a heatmap, needed for IsHot()
filters. By default, heatmap profile is not collected.
A threshold argument used to create a heatmap, see perf-heatmap docs on it. By default value is equal to 0.5
.
Write memory profile to the specified file. By default,the memory profile is not collected.
Collects GC usage and current runtime heap profile.
Write cpu profile to the specified file. By default, the cpu profile is not collected.
Output additional verbose information about the execution process. Disabled by default.
This is usually useful only for gogrep tool debugging or troubleshooting.