Ast Grep logo

Ast Grep

Community
damusix
ast-grep

AST-based code search, lint, and rewrite using ast-grep. Use when finding code patterns structurally (not textually), writing lint rules, building codemods, or migrating API usage across a codebase. Prefer over regex grep when the match target is a syntactic construct (function call, import, class field, assignment).

Overview

Publisherdamusix
Repositoryskills
Skill nameast-grep
Stars
63
Forks
3
Bundled files
5
Links
  • Markdown instructions

    A SKILL.md file the model loads on demand, so it only costs tokens when a request actually matches.

  • Works with any LLM

    AI skills are plain Markdown, not provider-specific code, so this works with GPT, Claude, Gemini, Grok, or a local model.

  • 5 bundled files

    Scripts, templates, and references the model can read while it works. Files are read-only and never executed.

  • Open source

    Published by damusix on GitHub. Read the source before you install it.

Installation

Install the Ast Grep AI skill in TypingMind to use it with any LLM, or drop it into another agent that reads SKILL.md.

1

Install in TypingMind

TypingMind installs a skill straight from its GitHub folder — it reads SKILL.md, bundles the resource files, and stores the result locally.

  1. Open the app and go to Plugins → Skills.
  2. Choose "Install from GitHub".
  3. Paste the skill folder URL below and confirm.
  4. Enable the skill in any chat where you want it available.
Plugins → Skills → Add skill → From GitHub URL, then paste the folder URL and press Continue.
2

Install in another agent

Any agent that reads the Agent Skills format can use this skill — copy the folder into that agent's skills directory.

Claude Code — .claude/skills
git clone --depth 1 https://github.com/damusix/skills.git /tmp/skills
mkdir -p .claude/skills
cp -r /tmp/skills/ast-grep .claude/skills/ast-grep
Restart Claude Code after copying so it picks up the new skill.

Use it in TypingMind

Enable Ast Grep in any TypingMind chat and the model takes it from there. Its name and description sit in the system prompt, and the moment a request matches, the model loads the full instructions itself — you never invoke it by hand, and it costs no tokens until it is actually used.

The model loads Ast Grep on its own as soon as a request matches it.

Works with any AI model

AI skills are plain Markdown instructions rather than provider-specific code, so Ast Grep is not tied to the model it was written for. Install it once in TypingMind and use it with GPT-5, Claude, Gemini, Grok, DeepSeek, Mistral, Llama, or a local model you run yourself — all on your own API keys.

  • Loaded only when it is needed

    The system prompt carries just the name and description. The instructions are fetched on the first matching request, so an idle skill costs nothing.

  • Switch models mid-chat

    Because the skill is instructions rather than code, changing model does not break it — the next model reads the same SKILL.md.

Skill instructions

This is the SKILL.md content the model loads. Read it before installing — a skill is instructions your model will follow.

ast-grep

ast-grep (sg) is a CLI tool that searches, lints, and rewrites code using Abstract Syntax Trees instead of text patterns. It uses tree-sitter parsers, supports 20+ languages (including Markdown), and runs in seconds across large codebases.

Write a code snippet as a pattern, and ast-grep matches it structurally against the AST -- ignoring whitespace, comments, and formatting differences.

Patterns are one atomic building block. When a pattern alone can't express what you need (disambiguation, naming constraints, relational checks, transforms), graduate to a full YAML rule. Patterns handle ~60% of searches; YAML rules handle the rest.

  1. Write valid parseable code as patterns. Tree-sitter must parse the pattern. Invalid syntax silently produces zero matches with no error, so always verify a new pattern returns results before adding constraints.
  2. Metavariable names use UPPERCASE: $NAME, $$$ARGS, $_. Lowercase $name is treated as literal code, not a capture.
  3. $X captures exactly one AST node. Use $$$X for zero-or-more. The mismatch is the most common cause of "pattern doesn't match" -- a single-arg pattern won't match a two-arg call.
  4. Same name = same content: $A == $A matches x == x but rejects x == y. This is structural equality, not variable binding -- use it intentionally.
  5. Every rule needs at least one positive atomic rule (pattern, kind, or regex). A not rule alone is invalid because ast-grep needs something to anchor the search.
  6. regex matches the full node text. Partial matches fail. /foo/ does not match fooBar -- use ^foo if you want prefix matching.
  7. fix replaces the single matched node. It cannot patch multiple locations. Use expandStart/expandEnd to consume surrounding tokens (trailing commas, semicolons).
  8. Unmatched metavariables become empty strings in fix. This is intentional for optional captures, but verify your pattern actually captures what you expect before relying on it in a rewrite.
  9. Use stopBy: end on relational rules (inside, has, follows, precedes) unless you specifically want neighbor-only matching. The default stopBy: neighbor stops at the first non-matching node and misses deeper results -- this is the second most common cause of "rule doesn't match."
  10. Shell escaping for --inline-rules: the shell interprets $ as a variable. Wrap YAML in single quotes or escape each metavariable with \$VAR.
  11. Write example code before writing rules. Small mistakes in rule composition cascade into completely invalid output. Write a concrete code snippet that should match, verify the AST structure with --debug-query=cst, then build the rule against that snippet.
  12. Verify every rule before searching the codebase. Test against the example snippet with sg scan --inline-rules '...' --stdin or sg scan -r rule.yml test.file. This catches composition errors before they waste a full codebase scan.

Rules are compositions of atomic parts. A single error in one part cascades, so verify at each step.

  1. Understand the intent -- what code pattern are you looking for? What should match and what should not?
  2. Write example code -- a concrete snippet that should match the rule, and one that should not. These are the test fixtures.
  3. Explore the AST -- sg run --pattern 'TARGET_CODE' --debug-query=cst -l LANG to see node kinds and structure of the example code.
  4. Write the rule -- start with the simplest possible pattern. Add constraints, relational rules, and transforms incrementally. Test after each addition.
  5. Test the rule against the example -- echo "example code" | sg scan --inline-rules '...' --stdin or sg scan -r rule.yml example.file. Confirm it matches the positive case and rejects the negative case.
  6. Search the codebase -- sg scan for the full project. Review a sample of results to confirm precision.
  7. Formalize -- if reusable, add id, message, severity, fix. Write test YAML with sg new test, generate snapshots with sg test -U.

When a rule doesn't work, go back to step 3. The AST structure frequently contradicts how source code looks visually -- verify with --debug-query=cst rather than assuming.

Before reporting a rule as done, run it against both the positive and negative example. Confirm it matches what it should and rejects what it shouldn't.

sg run -p 'fetch($$$ARGS)' -l javascript
yaml
rule:
  pattern: $HOOK($$$ARGS)
constraints:
  HOOK: { regex: '^use' }
sg run -p 'oldFunction($$$ARGS)' -r 'newFunction($$$ARGS)' -l typescript -U

Use context + selector when the target fragment needs surrounding syntax to parse:

yaml
rule:
  pattern:
    context: 'class A { a = 123 }'
    selector: field_definition
yaml
id: no-await-in-loop
language: TypeScript
rule:
  pattern: await $EXPR
  inside:
    any:
      - kind: for_statement
      - kind: for_in_statement
      - kind: while_statement
    stopBy: end

References

  • Pattern Syntax -- Metavariables, matching rules, strictness modes, pattern object for disambiguation
  • Rule Reference -- Atomic, relational, and composite rules, matching order, ESQuery selectors
  • YAML Configuration -- Full rule file schema: fix, transform, constraints, utils, rewriters
  • CLI Reference -- All commands, flags, output formats, CLI vs Playground differences
  • Recipes -- Search, lint, rewrite, transform, debugging, and advanced technique examples

Bundled files

The model reads these on demand while the skill is loaded. They are exposed as readable files and are never executed.

Frequently asked questions

What does the Ast Grep AI skill do?

AST-based code search, lint, and rewrite using ast-grep. Use when finding code patterns structurally (not textually), writing lint rules, building codemods, or migrating API usage across a codebase. Prefer over regex grep when the match target is a syntactic construct (function call, import, class field, assignment).

Why use Ast Grep on TypingMind?

Because you install it once and use it with any model. Ast Grep is plain Markdown rather than provider-specific code, so the same skill runs on GPT-5, Claude, Gemini, Grok, or a local model — and you can switch model mid-chat without it breaking. TypingMind runs on your own API keys, so you pay providers directly instead of a per-seat subscription, and your skills and chats stay in your own storage.

How do I install Ast Grep in TypingMind?

Open Plugins → Skills → Install from GitHub in TypingMind and paste https://github.com/damusix/skills/tree/main/ast-grep. TypingMind reads its SKILL.md and bundles its files and installs it as a skill you can enable per chat.

Which AI models can use Ast Grep?

Any model you connect in TypingMind. AI skills are plain Markdown instructions rather than provider-specific code, so GPT, Claude, Gemini, Grok, and local models can all load this skill when a request matches it.

How many AI models can I use with Ast Grep?

As many as you like. As long as a model supports skills, you can use Ast Grep with it — GPT, Claude, Gemini, Grok, DeepSeek, Mistral, Llama and more — all on TypingMind with your own API keys.

Is the Ast Grep AI skill free?

It is published on GitHub by damusix. Check the repository for licensing terms. You only pay your own AI provider for the tokens you use.

What are AI skills?

An AI skill is a reusable instruction bundle that teaches an AI model how to do one specific task. It follows the open Agent Skills format: a SKILL.md file with a name and description, plus any scripts, templates or reference files the model may need. The model reads the instructions only when your request matches the skill, so an installed skill costs nothing until it is used.

How are AI skills different from plugins or MCP servers?

A plugin or MCP server gives a model new tools to call — code that runs somewhere and returns a result. An AI skill gives the model knowledge and process instead: how to approach a task, which steps to follow, what good output looks like. Skills are plain Markdown, so they need no server, no API key and no runtime, and they work with any model.

View all

Set up your own AI workspace now

Get notified about new features and future giveaways by subscribing to our newsletter 👇