ripgrep (rg) のインストール:基本正規表現検索、主要フラグ、grep との対照

rg を入れ、最初の regex 検索と flags を押さえ、grep を残す場面を分ける。

grep -r works in a repo, but it happily walks node_modules, needs more flags for a sane default, and regex metacharacters fight the shell. ripgrep (rg, Rust, BurntSushi) recurses by default, respects .gitignore, and packs daily search into short flags. 本稿は macOS/Linux への rg 導入、リテラルと基本正規表現、主要フラグ、grep との対照 を hereink 向けにまとめ、fd・bat と併用する前提です。ignore の詳細は ripgrep と .gitignore を参照してください。

検索語はripgrep をインストールして正規表現検索し grep の常用フラグと比べる。 You should finish with rg on PATH, a first search from the repo root, fluency with -n/-l/-i/-w, and when to keep /bin/grep.

installbrew · apt · dnf
searchliteral · regex
comparerg vs grep
install rgregex & flagsvs grep

What rg fixes

Repo search with fewer footguns

rg TODO recurses from here, skips ignored paths, and prints paths with line numbers. Less noise than grep -rIn TODO . on a large tree.

Works with fd and bat

fd lists paths, rg searches content, bat previews hits: rg -l pattern | xargs bat.

Upstream: github.com/BurntSushi/ripgrep. Run rg --version after install.

Install on macOS and Linux

  1. 1
    macOS (Homebrew)

    brew install ripgrep. Verify with which rg and rg --version.

  2. 2
    Debian / Ubuntu

    sudo apt update && sudo apt install ripgrep. The binary is rg.

  3. 3
    Fedora / Arch

    Fedora: sudo dnf install ripgrep. Arch: sudo pacman -S ripgrep.

  4. 4
    Windows (optional)

    winget install BurntSushi.ripgrep.MSVC or GitHub Releases. Examples below focus on macOS/Linux.

  5. 5
    First search

    At a Git root: rg "fn main" or rg TODO. Exit code 1 means no matches (not always an error in scripts).

Literals and basic regex

By default rg PATTERN treats PATTERN as a regex (Rust regex syntax). For dots and parentheses as literals, quote and escape, or use -F / --fixed-strings.

rg -F 'config.json' .
rg -F 'foo.bar' src/
rg '^import ' --type py
rg '\bError\b' logs/
rg 'function\s+(\w+)' -n app.js
rg 'TODO|FIXME' .
rg pattern src/ tests/

Common flags

rg -n pattern
rg -l pattern
rg -i pattern
rg -w Token
rg -C 3 panic
rg -t js pattern
rg -t rust 'unwrap\('
rg pattern -g '*.ts' -g '!*.min.ts'
rg -c pattern
rg -F -i 'License MIT'
rg --max-depth 2 pattern

Use --no-ignore or -u only when you mean to search ignored trees; default is safer in day-to-day dev.

rg vs grep

Goalripgrep (rg)grep
Recursive tree searchrg PATTERNgrep -rIn PATTERN .
Respect .gitignoreon by defaultnot built in
Fixed stringrg -F 'a.b'grep -F 'a.b'
Case insensitiverg -igrep -i
Files onlyrg -lgrep -rl
Context linesrg -C 3grep -C 3
Extension filterrg -t js or -g '*.js'grep -r --include='*.js'
Speedparallel, code-orientedfine for single files
POSIX minimal imagesextra installoften preinstalled

Keep /bin/grep for portable init scripts and one-off log files; use rg inside Git repos.

Good fit
Daily symbol search in repos, pairing with fd/bat, skipping vendor dirs by default.
Not ideal
Tiny containers with no extra binaries, or scripts that must stay strict POSIX—grep still wins.
Overlap with the .gitignore ripgrep article?

That post goes deep on ignore rules; this one focuses on install, regex basics, flags, and grep comparison.

Can rg replace sed for edits?

No—rg searches only. Use your editor, sd, or careful sed. rg -l first to scope files.

Why quote patterns in the shell?

Unquoted *, ?, and $() expand before rg runs. Single quotes around the pattern are the easy default.

検索後に設計を揃える

Module boundaries are easier on a shared board. See スペースを作成.

今すぐ開始