Greph

CLI Reference

Greph ships four executables. All four share the same engine; the difference is the surface they expose.

BinaryPurpose
grephNative text search, AST search, and AST rewrite
greph-indexWarmed text index, AST fact index, and cached AST search
rgripgrep-style compatibility wrapper
sgast-grep-style compatibility wrapper

All paths in the examples use ./vendor/bin/<binary>. Substitute ./bin/<binary> if you are working from a clone of the repository.


greph

Native text search, AST search, and AST rewrite.

greph [options] pattern [path...]
greph -p pattern [options] [path...]
greph -p pattern -r replacement [options] [path...]

If a path is omitted, the current directory is searched.

Text mode

Default mode. Patterns are PCRE2 regexes unless -F is set.

# Regex search
./vendor/bin/greph "function\s+\w+" src

# Fixed-string search
./vendor/bin/greph -F "function" src

# Case-insensitive whole-word with two lines of context
./vendor/bin/greph -F -i -w -C 2 "function" src

Output format matches grep -rn:

src/Greph.php:31:final class Greph
src/Greph.php:36:    public static function walk(...

AST mode

Activated by -p. Patterns are written as ordinary PHP with $VAR and $$$VARIADIC metavariables. See Modes / AST Search for the full pattern grammar.

./vendor/bin/greph -p 'new $CLASS()' src
./vendor/bin/greph -p '$obj->$method($$$ARGS)' src
./vendor/bin/greph -p 'function $name($$$PARAMS): void {}' src

Rewrite mode

Activated by combining -p with -r. The replacement template uses the same metavariables as the search pattern; captured nodes are spliced in by name.

# Preview only
./vendor/bin/greph -p 'array($$$ITEMS)' -r '[$$$ITEMS]' --dry-run src

# Apply
./vendor/bin/greph -p 'array($$$ITEMS)' -r '[$$$ITEMS]' src

# Confirm each file before writing
./vendor/bin/greph -p 'array($$$ITEMS)' -r '[$$$ITEMS]' --interactive src

Options

FlagMeaning
-FFixed-string search (no regex)
-iCase-insensitive matching
-wWhole-word matching
-vInvert match
-cCount matches per file
-lList matching file paths only
-LList non-matching file paths only
-hSuppress filename prefixes in text mode
-HAlways print filename prefixes in text mode
-nShow line numbers (default: on)
-A NShow N lines after each match
-B NShow N lines before each match
-C NShow N lines of context before and after each match
-m NStop after N matches per file
-j NUse N parallel workers
-p PATTERNAST search pattern
-r TEMPLATEAST rewrite template (requires -p)
--glob GLOBInclude only files whose paths match GLOB
--type NAMEInclude a file type alias (php, js, md, ...)
--type-not NAMEExclude a file type alias
--lang NAMEAST language. Default: php
--jsonEmit JSON output
--no-ignoreIgnore .gitignore and .grephignore rules
--hiddenInclude hidden files
--dry-runPrint rewrites without writing files
--interactiveConfirm each rewritten file
--helpShow inline usage

greph-index

Warmed text index, AST fact index, and cached AST search.

greph-index build [path] [--index-dir DIR]
greph-index refresh [path] [--index-dir DIR]
greph-index search [options] pattern [path...]

greph-index ast-index build [path] [--index-dir DIR]
greph-index ast-index refresh [path] [--index-dir DIR]
greph-index ast-index search [options] pattern [path...]

greph-index ast-cache build [path] [--index-dir DIR]
greph-index ast-cache refresh [path] [--index-dir DIR]
greph-index ast-cache search [options] pattern [path...]

Text index

# Build (full)
./vendor/bin/greph-index build .

# Refresh (incremental: added, updated, deleted, unchanged)
./vendor/bin/greph-index refresh .

# Query
./vendor/bin/greph-index search -F "function" .
./vendor/bin/greph-index search -i -w "function" .
./vendor/bin/greph-index search -c "function" .

The text index is a trigram + identifier postings store. It lives at .greph-index/ in the indexed root by default.

AST fact index

./vendor/bin/greph-index ast-index build .
./vendor/bin/greph-index ast-index refresh .
./vendor/bin/greph-index ast-index search 'new $CLASS()' src

The AST fact index extracts node-level facts (calls, instantiations, classes, methods) into a queryable store. Lookups are O(log n) on the fact key, then verified against the source. Stored at .greph-ast-index/ by default.

AST cache

./vendor/bin/greph-index ast-cache build .
./vendor/bin/greph-index ast-cache refresh .
./vendor/bin/greph-index ast-cache search 'array($$$ITEMS)' src

The AST cache stores parsed trees on disk so searches skip the parser entirely. Stored at .greph-ast-cache/ by default.

Options

Text search options match the greph flag set (-F, -i, -w, -v, -c, -l, -L, -A, -B, -C, -m, --glob, --type, --type-not, --json, --no-ignore, --hidden).

Additional flags:

FlagMeaning
--index-dir DIRUse a non-default index directory
--lang NAMEAST language for ast-index / ast-cache searches. Default: php
-j N, --jobs NWorkers for AST scans
-l, --files-with-matchesList matching file paths only (AST mode)
--strict-parseFail on parse errors instead of skipping
--fallback MODEMissing-index behavior: fail (default) or scan
--helpShow inline usage

rg

ripgrep-style compatibility wrapper. Supports the most commonly used ripgrep flags. The verified surface is published in the feature matrix.

rg [options] pattern [path...]
rg --files [options] [path...]

Examples

./vendor/bin/rg -F "function" src
./vendor/bin/rg --json -F "function" src
./vendor/bin/rg --files src
./vendor/bin/rg --files --type php .
./vendor/bin/rg --type-not js -F "function" .

Supported options

FlagMeaning
-F, --fixed-stringsFixed-string search
-i, --ignore-caseCase-insensitive search
-w, --word-regexpWhole-word search
-v, --invert-matchInvert matches
-c, --countCount matching lines
-l, --files-with-matchesList matching files
--files-without-matchList non-matching files
-I, --no-filenameSuppress filename prefixes
-H, --with-filenameAlways print filename prefixes
-n, --line-numberShow line numbers
-A N, --after-context NShow N lines after each match
-B N, --before-context NShow N lines before each match
-C N, --context NShow N lines before and after each match
-m N, --max-count NStop after N matches per file
-j N, --threads NUse N parallel workers
-e P, --regexp PSearch pattern
-L, --followFollow symlinks
--glob GLOBInclude only files whose paths match GLOB
--type NAMEInclude a file type
--type-not NAMEExclude a file type
--jsonEmit ripgrep-style JSON events
--no-ignoreIgnore .gitignore and .grephignore rules
--hiddenInclude hidden files
--filesList candidate files instead of searching
--helpShow inline usage

sg

ast-grep-style compatibility wrapper. Supports the most commonly used ast-grep flags. The verified surface is published in the feature matrix.

sg run --pattern PATTERN [options] [path...]
sg scan -p PATTERN [options] [path...]
sg rewrite -p PATTERN -r TEMPLATE [options] [path...]

run is the canonical ast-grep verb. scan and rewrite are wrapper-only aliases that mirror common usage. A bare invocation (sg --pattern ... .) also works as a one-shot search.

Examples

./vendor/bin/sg run --pattern 'array($$$ITEMS)' src/App.php
./vendor/bin/sg run --pattern 'array($$$ITEMS)' --rewrite '[$$$ITEMS]' src/App.php
./vendor/bin/sg run --pattern 'array($$$ITEMS)' --rewrite '[$$$ITEMS]' --update-all src/App.php
./vendor/bin/sg run --json --pattern 'dispatch($EVENT)' src/App.php
./vendor/bin/sg scan -p 'array($$$ITEMS)' src/App.php
./vendor/bin/sg rewrite -p 'array($$$ITEMS)' -r '[$$$ITEMS]' --dry-run src/App.php

Supported options

FlagMeaning
-p, --pattern PATTERNAST pattern
-r, --rewrite TEMPLATERewrite template
-l, --lang NAMEAST language. Default: php
-j, --threads NUse N parallel workers
-i, --interactiveConfirm each rewrite
-U, --update-allApply rewrites without confirmation
--files-with-matchesPrint only file paths with matches
--json[=STYLE]Emit JSON output. STYLE = pretty, stream, or compact
--no-ignore [MODE]Ignore repository ignore rules
--hiddenInclude hidden files
--glob GLOB, --globs GLOBInclude only files whose paths match GLOB
--type NAMEInclude a file type
--type-not NAMEExclude a file type
--dry-runPreview rewrites without writing files
--helpShow inline usage

Exit codes

Greph follows grep / ripgrep conventions:

CodeMeaning
0At least one match was found (or, for rewrites, at least one file changed)
1No matches found
2Usage error or runtime failure (bad arguments, missing index, parse error in --strict-parse mode)

On this page