0
2

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.

再帰的にテキスト検索してVIMで開く

Last updated at Posted at 2017-01-24

手早くテキストファイルを再帰的に検索する

ほとんどの人は以下のようにやっていると思う。

find . -name "*.c" | xargs grep "hoge"

入力文字数を減らすなら

grep -ainr --include="*.c" "hoge"

となる。

.bashrcに以下の記述を追加すれば、さらに入力文字数を減らすことができる。

.bashrc
function rgrep() {
    grep -ainr --include=$1 $2
}

これで、以下のように再帰的なワード検索ができる。

rgrep "*.c" "hoge"

テキスト検索結果一覧からVIMでそのファイルの該当行を開く

.bashrcに以下の記述を追加すれば、VIM上に検索結果一覧を表示することができる。

.bashrc
function ogrep() {
    grep -ainr --include=$1 $2 | vim -R -
}

ここで、以下のような検索を行うと

ogrep "*.c" "hoge"

VIM上に検索結果一覧が表示される。

無題
sample1.c:28        result = hoge();
sample1.c:100           void hoge() {
library/sample2.c:5     printf("hoge");

該当行の行頭にカーソルを合わせてgFを入力すると該当ファイルの該当行にジャンプすることができる。1

  1. vimgrepを使えという意見もあろうと思いますが、コンソールからVIMの該当行にジャンプできるのも気持ちいいかな、と。

0
2
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
0
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?