Skip to content

Command Line Reference

You can always see up-to-date command line options using sg --help. ast-grep has several subcommands as listed below.

sg run

Run one time search or rewrite in command line. This is the default command when you run sg so sg -p 'foo()' is equivalent to sg run -p 'foo()'. View detailed reference.

Usage

shell
sg run [OPTIONS] --pattern <PATTERN> [PATHS]...

Arguments

[PATHS]... The paths to search. You can provide multiple paths separated by spaces [default: .]

Options

ShortLongDescription
-p--pattern <PATTERN>AST pattern to match.
-r--rewrite <REWRITE>String to replace the matched AST node.
-l--lang <LANG>The language of the pattern query. ast-grep will infer the language based on file extension if this option is omitted.
--debug-queryPrint query pattern's tree-sitter AST. Requires lang be set explicitly.
-i--interactiveStart interactive edit session. Code rewrite only happens inside a session.
-U--update-allApply all rewrite without confirmation if true.
--json[=<style>]Output matches in structured JSON [possible values: pretty, stream, compact]
--heading <WHEN>Controls whether to print the file name as heading [default: auto] [possible values: auto, always, never]
--color <WHEN>Controls output color [default: auto]
--no-ignore <NO_IGNORE>Do not respect hidden file system or ignore files (.gitignore, .ignore, etc.) [possible values: hidden, dot, exclude, global, parent, vcs]
--stdinEnable search code from StdIn. See link
-A--after <NUM>Show NUM lines after each match [default: 0]
-B--before <NUM>Show NUM lines before each match [default: 0]
-C--context <NUM>Show NUM lines around each match [default: 0]
-h--helpPrint help

sg scan

Scan and rewrite code by configuration. View detailed reference.

Usage

shell
sg scan [OPTIONS] [PATHS]...

Arguments

[PATHS]... The paths to search. You can provide multiple paths separated by spaces [default: .]

Options

ShortLongDescription
-c--config <CONFIG_FILE>Path to ast-grep root config, default is sgconfig.yml
-r--rule <RULE_FILE>Scan the codebase with the single rule located at the path RULE_FILE.
--inline-rules <RULE_TEXT>Scan the codebase with a rule defined by the provided RULE_TEXT
--filter <REGEX>Scan the codebase with rules with ids matching REGEX
-i--interactiveStart interactive edit session.
--color <WHEN>Controls output color [default: auto] [possible values: auto, always, ansi, never]
--report-style <REPORT_STYLE>[default: rich] [possible values: rich, medium, short]
--json[=<style>]Output matches in structured JSON [possible values: pretty, stream, compact]
--format <FORMAT>Output warning/error messages in GitHub Action format [possible values: github]
-U--update-allApply all rewrite without confirmation
--no-ignore <NO_IGNORE>Do not respect ignore files. (.gitignore, .ignore, etc.) [possible values: hidden, dot, exclude, global, parent, vcs]
--stdinEnable search code from StdIn. See link
-h--helpPrint help

sg test

Test ast-grep rules. See testing guide for more details. View detailed reference.

Usage

shell
sg test [OPTIONS]

Options

ShortLongDescription
-c--config <CONFIG>Path to the root ast-grep config YAML.
-t--test-dir <TEST_DIR>the directories to search test YAML files.
--snapshot-dir <SNAPSHOT_DIR>Specify the directory name storing snapshots. Default to __snapshots__.
--skip-snapshot-testsOnly check if the test code is valid, without checking rule output. Turn it on when you want to ignore the output of rules
-U--update-allUpdate the content of all snapshots that have changed in test.
-f--filterFilter rule test cases to execute using a glob pattern.
-i--interactivestart an interactive review to update snapshots selectively.
-h--helpPrint help.

sg new

Create new ast-grep project or items like rules/tests. View detailed reference.

Usage

shell
sg new [COMMAND] [OPTIONS] [NAME]

Commands

Sub CommandDescription
projectCreate an new project by scaffolding.
ruleCreate a new rule.
testCreate a new test case.
utilCreate a new global utility rule.
helpPrint this message or the help of the given subcommand(s).

Arguments

[NAME] The id of the item to create.

Options

ShortLongDescription
-l--lang <LANG>The language of the item to create.
-y--yesAccept all default options without interactive input during creation.
-b--base-dir <BASE_DIR>Create new project/items in the folder specified by this argument [default: .]
-h--helpPrint help (see more with '--help')

sg lsp

Start a language server to report diagnostics in your project. This is useful for editor integration. See editor integration for more details.

Usage

shell
sg lsp

sg completions

Generate shell completion script.

Usage

shell
sg completions [SHELL]

Arguments

[SHELL]

Output the completion file for given shell. If not provided, shell flavor will be inferred from environment.

[possible values: bash, elvish, fish, powershell, zsh]

sg help

Print help message or the help of the given subcommand(s).

Made with ❤️ with Rust