askill
search

searchSafety 90Repository

Pattern reference for ast-grep (sg) and ripgrep (rg) code search. Use when searching codebases, "find pattern", "search for", "ast-grep", "sg", "rg".

31 stars
1.2k downloads
Updated 2/27/2026

Package Files

Loading files...
SKILL.md

Code Search Reference

When to Use sg vs rg

NeedToolWhy
Structural code pattern (functions, classes, imports)sgAST-aware, ignores formatting
Text/string/comment searchrgFaster, no parse needed
Cross-language refactorsg --rewriteSafe structural replacement
Regex across filesrgFull PCRE2 regex support
Find + replace respecting syntaxsg --interactiveWon't break code structure

sg Metavariables

SyntaxMatchesExample
$NAMEExactly one AST node$FUNC(x) matches foo(x)
$$VARExactly one unnamed/anonymous nodeRarely needed directly
$$$LISTZero or more nodes (variadic)$FUNC($$$ARGS) matches foo(), foo(a), foo(a, b, c)

$$$ is the multi-node wildcard. Use it for argument lists, statement bodies, and anywhere the count varies.

sg Patterns by Language

Python

sg -p 'def $FUNC($$$ARGS)' -l py
sg -p 'async def $NAME($$$ARGS)' -l py
sg -p 'class $CLASS' -l py
sg -p 'except:' -l py
sg -p 'Optional[$T]' --rewrite '$T | None' -l py --interactive
sg -p 'assert $COND' -l py
sg -p '@$DECORATOR
def $FUNC($$$ARGS):
    $$$BODY' -l py

TypeScript / TSX

sg -p 'import $X from $Y' -l ts
sg -p 'import { $$$NAMES } from $SOURCE' -l ts
sg -p 'function $NAME($$$ARGS): JSX.Element { $$$BODY }' -l tsx
sg -p 'console.log($$$ARGS)' -l ts
sg -p 'const $NAME = ($$$ARGS) => $$$BODY' -l ts
sg -p '$EXPR as $TYPE' -l ts

React hooks — can't use use$HOOK() pattern. ast-grep parses use$HOOK as a single identifier, not prefix + metavariable. Use rule YAML instead:

# find-hooks.yml
id: find-react-hooks
language: tsx
rule:
  kind: call_expression
  regex: "^use[A-Z]"
sg scan --rule find-hooks.yml

Go

Go often needs pattern objects because bare patterns are ambiguous without file-level context.

# find-go-func.yml
id: find-functions
language: go
rule:
  kind: function_declaration
  has:
    kind: identifier
    regex: "^Handle"
sg -p 'fmt.Println($$$ARGS)' -l go
sg -p 'if err != nil { $$$BODY }' -l go
sg scan --rule find-go-func.yml

Rust

sg -p 'fn $NAME($$$ARGS) -> $RET { $$$BODY }' -l rs
sg -p 'impl $TRAIT for $TYPE { $$$BODY }' -l rs
sg -p 'unwrap()' -l rs
sg -p '#[derive($$$ATTRS)]' -l rs
sg -p 'println!($$$ARGS)' -l rs

sg Refactoring

sg -p 'console.log($$$ARGS)' --rewrite '' -l ts --interactive
sg -p 'require($MOD)' --rewrite 'import $MOD from $MOD' -l js
sg -p 'assert.equal($A, $B)' --rewrite 'expect($A).toBe($B)' -l ts

sg Gotchas

  • Patterns must be valid parseable code in the target language. $FOO( alone won't work.
  • Can't mix prefix text with metavariables in identifiers. use$Hook, get$Name — none work. Use kind: + regex: rule instead.
  • Go needs pattern objects for most structural matches. Bare patterns lack the package-level context Go's parser requires.
  • $$$ is NOT ... — ast-grep uses $$$ for variadic matching, not Semgrep's ... syntax.
  • Rule ordering matters — in compound rules, the first matched rule binds metavariables. Later rules see those bindings.

rg Patterns

rg 'TODO|FIXME|HACK' --type-add 'code:*.{ts,py,rs,go}' -t code
rg '(def |fn |func |function )\w+' -t py -t rust -t go -t js
rg '^(import |from .+ import |require\()' -t py -t js -t ts
rg -l 'pattern'
rg -c 'pattern'
rg -C3 'pattern'
rg -F 'exact.match()'
rg -U 'struct \w+\s*\{[^}]*\}'
rg -v 'pattern'
rg -o '\b\w+Error\b'
rg --files-without-match 'test'
rg 'old' -r 'new'

rg Essential Flags

FlagPurpose
-t typeFilter by file type (-t py, -t js, -t rust)
-g 'glob'Filter by glob (-g '*.test.ts', -g '!node_modules')
-lList matching files only
-cCount matches per file
-C NShow N lines of context
-FFixed string, no regex
-UMultiline mode
-iCase insensitive
-wMatch whole words only
-vInvert match
-oShow only matched text
-r 'text'Replace matched text in output
--jsonMachine-readable JSON output
-SSmart case (case-insensitive unless uppercase present)

Install

Download ZIP
Requires askill CLI v1.0+

AI Quality Score

91/100Analyzed 2/19/2026

Excellent technical reference for ast-grep and ripgrep code search. Comprehensive with language-specific patterns, clear when-to-use guidance, and highly actionable copy-paste commands. Well-structured with tables and gotchas. Slight penalty for nested path but content is clearly general-purpose.

90
95
85
90
95

Metadata

Licenseunknown
Version-
Updated2/27/2026
Publishersaadshahd

Tags

testing