Skip to content

grep

Search file contents for lines matching a regular expression.

Category: Read-only

Parameters

pattern (required)
Java regex pattern (java.util.regex syntax) to search for in file contents.
path (optional)
The directory to search. Defaults to the current working directory.
include (optional)

Java glob pattern restricting which files are searched (e.g. *.kt, **/*.json). Uses the same syntax as glob. If omitted, all files in the search root are searched.

Note

**/*.ext requires at least one directory separator before the filename. Use *.ext to match files directly in the search root.

Output

Matches grouped by file, with each file sorted by modification time (most recent first) and matches within each file listed by line number:

Found 3 matches

/path/to/Foo.kt:
 Line 12: val name: String
 Line 45: fun name(): String

/path/to/Bar.kt:
 Line 7: val name = "default"

Returns No matches found if nothing matches. Unreadable and binary files are silently skipped. Results are capped at 100 matches by default (configurable via toolLimits.grepMaxMatches in knosh.json); a truncation note is appended when the limit is reached. Lines longer than 2000 characters are truncated for display.

Limits

To keep a pathological caller-supplied pattern (e.g. catastrophic backtracking) from hanging the search, grep enforces two additional bounds:

  • Time budget — the whole search has a wall-clock budget, 5000 ms by default (configurable via toolLimits.grepTimeoutMillis). If the budget is exceeded, the search stops and returns whatever matches were collected before the deadline, followed by a note (e.g. (Search stopped after 5000ms time budget; results may be incomplete.)). If the budget is exceeded before any matches are found, this note is returned instead of No matches found, so a timeout is never mistaken for a genuine absence of matches.
  • Line-length cap — each line is matched against only its first 100000 characters by default (configurable via toolLimits.grepMaxLineLength); a match starting beyond the cap is not found. This is independent of the 2000-character display truncation above: a line between 2000 and grepMaxLineLength characters is fully searched, but its displayed match text is still clipped at 2000 characters.