Skip to main content

ag Cheatsheet

Syntax

ag [OPTIONS] PATTERN [PATH]

Pattern is PCRE regex by default. PATH defaults to ..

Core Flags

FlagPurpose
-iCase insensitive
-sForce case sensitive
-SSmart case
-wWhole word match
-vInvert match
-QFixed/literal string (no regex)
-f FILERead patterns from file

Scope Control

FlagPurpose
--hiddenInclude hidden dotfiles
-uSkip ignore files
-uuSkip ignore files + hidden
--ignore-dir=DIRExclude a directory
--ignore=GLOBExclude file pattern
-G REGEXOnly files matching path regex
--depth NLimit traversal depth

File Types (select)

FlagFiles searched
--python.py, .pyw
--js.js, .jsx
--ts.ts, .tsx
--go.go
--rust.rs
--yaml.yaml, .yml
--sql.sql

Run ag --list-file-types for the full list.

Output Flags

FlagPurpose
-lOnly filenames
-cCount per file
-oOnly matching text
-nLine numbers (default)
--columnShow column number
--noheadingFlat file:line:col:text format
-qQuiet (exit code only)
-A NN lines after
-B NN lines before
-C NN lines each side
-m NStop after N matches
--color / --nocolorForce color on/off

Production Pipelines

# Find and replace across project
ag -l "old_name" src/ | xargs sed -i 's/old_name/new_name/g'

# Top 10 IPs from log
ag -o "([0-9]{1,3}\.){3}[0-9]{1,3}" access.log \
| sort | uniq -c | sort -nr | head -10

# Extract TODO authors
ag -o "TODO\(([^)]+)\)" src/ | sort | uniq -c

# Audit for hardcoded secrets
ag -i "(password|api_key|secret)\s*=" --python --js src/

# Open matches interactively with fzf
ag --noheading "TODO" | fzf | awk -F: '{print $1, $2}' | xargs -r sh -c 'vim +$2 $1'