CarbonEmacsでfindとgrepでのファイル検索

複数の拡張子を指定してサブディレクトリのファイルをgrepするコマンドの例。
2つの方法では、xargsを使うほうが速いらしい。

find . -type f -regex ".*[cpp|h]" -exec grep -nH -e "main" {} /dev/null \;
find . -type f -regex ".*[cpp|h]" -print0 | xargs -0 grep -nH -e "main"

↑これは拡張子の判定がダメだった。
↓これならOKだった。結局、正規表現で複数拡張子の指定はできなかった。

find . -type f \( -name "*.cpp" -o -name "*.h" \) -print0 | xargs -0 grep -nH -e "main"

.emacsに以下の定義をしておくと、grep-findで上ののコマンドが表示されるので、必要な箇所を書き換えて検索すれば良い。

(setq grep-find-command "find . -type f \\( -name \"*.cpp\" -o -name \"*.h\" \\) -print0 | xargs -0 grep -nH -e \"main\"" )

一つ下のサブディレクトリだけなら、こういう方法でもできる。

grep -nH -e "main" *.cpp */*.cpp *.h */*.h