25
Help us understand the problem. What are the problem?

More than 3 years have passed since last update.

posted at

updated at

Organization

rgの使い方メモ

Install

https://github.com/BurntSushi/ripgrep/releases からダウンロード

補完させるようにする
cp -a /path/to/ripgrep-0.7.1-x86_64-unknown-linux-musl/complete/rg.bash-completion /etc/bash_completion.d/

Usage

再帰検索
rg [keyword] [directory(DEFAULT: ./)]
grep代わり
echo hogehogefugafugapiyopiyo123hogefugapiyo | rg hoge
大文字小文字無視
rg -i [keyword]
単語単位で検索
rg -w [keyword]
抽出ではなく強調だけ
echo -e "aaa\nbbb\nccc" | red bbb bbb
検索対象を決める
rg -g '*.py' [keyword] # Pythonファイルから検索
rg -g '!*.py' [keyword] # Pythonファイル以外から検索(!が展開されてしまうためダブルクオーテーションは使えません)
rg -tpy [keyword] # rg -g '*.py' [keyword]
rg -Tpy [keyword] # rg -g '!*.py' [keyword]
-tの設定追加
rg --type-add 'foo:*.{foo,foobar}' # -tfooで.fooと.foobarにマッチするようになる
find代わり
rg -g '*.py' --files # Pythonファイルの検索
rg --files | rg '.*\.py' # Pythonファイルの検索

応用

sed代わり

red(){ #この関数を作らないとゼロマッチの時に何も出力されない
    arg="$(cat -)"
    echo "$arg" | rg "$1" -r "$2" -C 9999999999999999999
}

echo -e "hoge\nfuga\npiyo"  | rg ".*g" -r G
# Ge
# Ga
echo -e "hoge\nfuga\npiyo"  | red ".*g" G
# Ge
# Ga
# piyo
echo hogehogefugafugapiyopiyo123hogefugapiyo | red '(h.*?e).*(p.*?o)' '$1 and $2' # "$1 and $2" とダブルクオートで囲ってしまうと$1が普通に変数として展開されてしまうのでダメ
# hoge and piyo
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Sign upLogin
25
Help us understand the problem. What are the problem?