mcpField
R
ripgrep

ripgrep

github.com/ratacat開発58410 回インストール37 週間前に更新
リポジトリ

ripgrep について

ripgrep(rg)でファイルやディレクトリを正規表現検索するためのスキルです。コードベースや大きなテキスト群向けに、絞り込みや出力方法を説明します。

主な機能

  1. 01再帰的な正規表現検索
  2. 02ファイル種別と glob による絞り込み
  3. 03コンテキストと検索結果の出力制御
  4. 04複数行・圧縮ファイル検索
  5. 05置換結果のプレビュー

活用シーン

  1. 01コードベース内のシンボルを検索
  2. 02全文を読み込まず大きな文書を検索
  3. 03パターンの有無でファイルを特定

インストール

コマンドをコピーしてターミナルで実行します。

$npx skills add https://github.com/ratacat/claude-skills --skill ripgrep

インストール後、Claude Code で/ripgrepを入力して読み込まれたか確認します。

詳細

Ripgrep (rg) - 高速テキスト検索ツール

概要

Ripgrep は、ディレクトリを再帰的に検索して正規表現パターンを探す、行指向の検索ツールです。grep より 10〜100 倍高速で、デフォルトで .gitignore を尊重します。grep、find、大きなファイルを手作業で読む代わりに使用してください。

基本原則: ファイル内のテキストを探すときは、ripgrep を使用してください。検索できる場合は、ファイル全体をコンテキストに読み込まないでください。

使用する場面

次のような場合に ripgrep を使用します:

  • コードベースやディレクトリ全体でテキストパターンを検索する
  • 関数、変数、文字列のすべての出現箇所を見つける
  • 書籍、ドキュメント、大きなテキストファイルを検索する
  • ファイルが大きすぎてコンテキストにすべて読み込めない
  • 多数のファイルから特定の内容を一度に探す
  • 特定のパターンを含む(または含まない)ファイルを見つける
  • 分析用に一致する行を抽出する

次のような場合は使用しません:

  • ファイル全体の内容が必要な場合(Read ツールを使用)
  • ファイル名だけを単純な glob パターンで照合する場合(Glob ツールを使用)
  • 構造化データの抽出が必要な場合(jq、awk を検討)

クイックリファレンス

タスクコマンド
基本的な検索rg "pattern" [path]
大文字と小文字を区別しないrg -i "pattern"
スマートケース(自動)rg -S "pattern"
単語全体のみrg -w "word"
固定文字列(正規表現なし)rg -F "literal.string"
前後のコンテキスト行を表示rg -C 3 "pattern" (前後各 3 行)
行番号を表示rg -n "pattern" (TTY ではデフォルト)
ファイル名のみrg -l "pattern"
一致しないファイルrg --files-without-match "pattern"
一致数を数えるrg -c "pattern"
一致した部分のみrg -o "pattern"
一致条件を反転rg -v "pattern"
複数行検索rg -U "pattern.*\nmore"

ファイルのフィルタリング

ファイルタイプで指定

Ripgrep にはファイルタイプの定義が組み込まれています。-t で対象に含め、-T で除外します:

# Search only Python files
rg -t py "def main"

# Search only JavaScript and TypeScript
rg -t js -t ts "import"

# Exclude test files
rg -T test "function"

# List all known types
rg --type-list

一般的なタイプ: py、js、ts、rust、go、java、c、cpp、rb、php、html、css、json、yaml、md、txt、sh

Glob パターンで指定
# Only .tsx files
rg -g "*.tsx" "useState"

# Exclude node_modules (in addition to gitignore)
rg -g "!node_modules/**" "pattern"

# Only files in src directory
rg -g "src/**" "pattern"

# Multiple globs
rg -g "*.js" -g "*.ts" "pattern"

# Case insensitive globs
rg --iglob "*.JSON" "pattern"
ファイルサイズで指定
# Skip files larger than 1MB
rg --max-filesize 1M "pattern"

ディレクトリの制御

# Limit depth
rg --max-depth 2 "pattern"

# Search hidden files (dotfiles)
rg --hidden "pattern"

# Follow symlinks
rg -L "pattern"

# Ignore all ignore files (.gitignore, etc.)
rg --no-ignore "pattern"

# Progressive unrestricted (-u can stack up to 3 times)
rg -u "pattern"      # --no-ignore
rg -uu "pattern"     # --no-ignore --hidden
rg -uuu "pattern"    # --no-ignore --hidden --binary

コンテキストのオプション

# Lines after match
rg -A 5 "pattern"

# Lines before match
rg -B 5 "pattern"

# Lines before and after
rg -C 5 "pattern"

# Print entire file on match (passthrough mode)
rg --passthru "pattern"

出力形式

# Just filenames with matches
rg -l "pattern"

# Files without matches
rg --files-without-match "pattern"

# Count matches per file
rg -c "pattern"

# Count total matches (not lines)
rg --count-matches "pattern"

# Only the matched text (not full line)
rg -o "pattern"

# JSON output (for parsing)
rg --json "pattern"

# Vim-compatible output (file:line:col:match)
rg --vimgrep "pattern"

# With statistics
rg --stats "pattern"

正規表現パターン

Ripgrep はデフォルトで Rust の正規表現構文を使用します:

# Alternation
rg "foo|bar"

# Character classes
rg "[0-9]+"
rg "[a-zA-Z_][a-zA-Z0-9_]*"

# Word boundaries
rg "\bword\b"

# Quantifiers
rg "colou?r"           # 0 or 1
rg "go+gle"            # 1 or more
rg "ha*"               # 0 or more
rg "x{2,4}"            # 2 to 4 times

# Groups
rg "(foo|bar)baz"

# Lookahead/lookbehind (requires -P for PCRE2)
rg -P "(?<=prefix)content"
rg -P "content(?=suffix)"
複数行の照合
# Enable multiline mode
rg -U "start.*\nend"

# Dot matches newline too
rg -U --multiline-dotall "start.*end"

# Match across lines
rg -U "function\s+\w+\([^)]*\)\s*\{"

置換(プレビューのみ)

Ripgrep では置換結果のプレビューを表示できます(ファイルは変更されません):

# Simple replacement
rg "old" -r "new"

# Using capture groups
rg "(\w+)@(\w+)" -r "$2::$1"

# Remove matches (empty replacement)
rg "pattern" -r ""

特殊なファイルの検索

圧縮ファイル
# Search in gzip, bzip2, xz, lz4, lzma, zstd files
rg -z "pattern" file.gz
rg -z "pattern" archive.tar.gz
バイナリファイル
# Include binary files
rg --binary "pattern"

# Treat binary as text (may produce garbage)
rg -a "pattern"
大きなファイル

コンテキストに読み込むには大きすぎるファイルの場合:

# Search and show only matching lines
rg "specific pattern" large_file.txt

# Limit matches to first N per file
rg -m 10 "pattern" huge_file.log

# Show byte offset for large file navigation
rg -b "pattern" large_file.txt

# Use with head/tail for pagination
rg "pattern" large_file.txt | head -100

パフォーマンスのヒント

  1. パスを具体的に指定する - サブディレクトリがわかっている場合は、ルートから検索しないでください
  2. ファイルタイプを使用する - -t py は -g "*.py" より高速です
  3. 固定文字列を使用する - 正規表現が不要な場合は -F を使用します
  4. 深さを制限する - ディレクトリ構造がわかっている場合は --max-depth を使用します
  5. gitignore を有効にする - 必要な場合を除き、--no-ignore は使用しないでください
  6. 単語境界を使用する - -w は最適化されています

よく使うパターン

関数定義を見つける
# Python
rg "def \w+\(" -t py

# JavaScript/TypeScript
rg "(function|const|let|var)\s+\w+\s*=" -t js -t ts
rg "^\s*(async\s+)?function" -t js

# Go
rg "^func\s+\w+" -t go
インポート/require を見つける
# Python
rg "^(import|from)\s+" -t py

# JavaScript
rg "^(import|require\()" -t js

# Go
rg "^import\s+" -t go
TODO/FIXME コメントを見つける
rg "(TODO|FIXME|HACK|XXX):"
エラー処理を見つける
# Python
rg "except\s+\w+:" -t py

# JavaScript
rg "\.catch\(|catch\s*\(" -t js
クラス定義を見つける
# Python
rg "^class\s+\w+" -t py

# JavaScript/TypeScript
rg "^(export\s+)?(default\s+)?class\s+\w+" -t js -t ts
書籍/ドキュメント内を検索する
# Find chapter headings
rg "^(Chapter|CHAPTER)\s+\d+" book.txt

# Find quoted text
rg '"[^"]{20,}"' document.txt

# Find paragraphs containing word
rg -C 2 "keyword" book.txt

他のツールとの組み合わせ

# Find files, then search
rg --files | xargs rg "pattern"

# Search and count by file
rg -c "pattern" | sort -t: -k2 -rn

# Search and open in editor
rg -l "pattern" | xargs code

# Extract unique matches
rg -o "\b[A-Z]{2,}\b" | sort -u

# Search multiple patterns from file
rg -f patterns.txt

終了コード

コード意味
0一致が見つかった
1一致が見つからなかった
2エラーが発生した

スクリプトで便利です:

if rg -q "pattern" file.txt; then
    echo "Found"
fi

よくある間違い

間違い対処方法
パターンに特殊文字が含まれている固定文字列には -F を使うか、エスケープします:rg "foo\.bar"
隠しファイルが見つからない--hidden または -uu を追加します
node_modules が見つからない--no-ignore を追加します(ただし、通常は検索対象から除外するのが適切です)
正規表現が複雑すぎる先読み/後読みが使える PCRE2 用に -P を試します
出力が長すぎる-m N で制限するか、ファイル名のみなら -l を使用します
バイナリファイルがスキップされるテキストモードにするには --binary または -a を追加します
行全体を表示する必要がある-o(一致部分のみ)フラグを削除します

他のツールを優先する場面

タスクより適したツール
構造化された JSON クエリjq
列ベースのテキスト処理awk
ストリーム編集/置換sed(実際にファイルを変更します)
名前だけでファイルを検索fd または find
簡単なファイル一覧表示ls または glob
ファイルの全内容が必要な場合Read ツール

コメント