30
26

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

rgの使い方メモ

Last updated at Posted at 2017-11-26

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
30
26
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
30
26

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?