
世界には「grep -r」には満足できずに開発された高速検索ツールが多数存在します。
それらのツールを使用すると、ディレクトリ以下に含まれるテキストファイルを特定のパターンで高速に検索することができますが、例えば有名なThe Silver Searcherには、検索対象のテキストファイルに含まれる日本語の文字コードがUTF-8に限定されるという制限があります。
今回紹介する「The Platinum Searcher」はそのような制限を回避するために作成された、Go言語製の高速検索ツールです。
日本の開発者monochromegane氏によって作成され、UTF-8のほかEUC-JPやShift JISといった日本語文字コードにネイティブに対応しています。
公式サイトには以下のような特徴がリストアップされています。
- ackの3〜5倍高速
- agよりも高速
- .gitignoreで指定されたパターンを無視
- ドット(.)で始まるファイルを無視
- UTF-8、EUC-JP、Shift JISに対応
- macOS、Windows、Linuxのマルチプラットフォーム対応
以下使用方法を説明します。
The Platinum Searcherのインストール
macOSでHomebrewを使っている場合、以下のコマンドでインストールできます。
brew install pt
Homebrewを使用していない場合、または他のプラットフォームを使用している場合は、GitHubのリリースページからバイナリファイルをダウンロードします。
The Platinum Searcherの使用方法
ptの最もシンプルな使用方法は「pt パターン」です。
pt bootstrap
上のコマンドを実行するとカレントフォルダ以下のファイルから「bootstrap」が含まれる文字列を検索することができます。検索にマッチした場合、パターンが含まれるファイルや、パターンが実際にマッチした箇所が色つきでカラフルに出力されます。

「pt オプション パターン パス」のように各種オプションや検索対象のパスを追加する事も可能です。指定可能なオプションは単に「pt」と実行するか、「pt --help」で確認可能です。
$ pt
Usage:
pt [OPTIONS] PATTERN [PATH]
Application Options:
--version Show version
Output Options:
--color Print color codes in results (default: true)
--nocolor Don't print color codes in results (default: false)
--color-line-number= Color codes for line numbers (default: 1;33)
--color-path= Color codes for path names (default: 1;32)
--color-match= Color codes for result matches (default: 30;43)
--group Print file name at header (default: true)
--nogroup Don't print file name at header (default: false)
-0, --null Separate filenames with null (for 'xargs -0') (default: false)
--column Print column (default: false)
--numbers Print Line number. (default: true)
-N, --nonumbers Omit Line number. (default: false)
-A, --after= Print lines after match
-B, --before= Print lines before match
-C, --context= Print lines before and after match
-l, --files-with-matches Only print filenames that contain matches
-c, --count Only print the number of matching lines for each input file.
-o, --output-encode= Specify output encoding (none, jis, sjis, euc)
Search Options:
-e Parse PATTERN as a regular expression (default: false). Accepted syntax
is the same as https://github.com/google/re2/wiki/Syntax except from \C
-i, --ignore-case Match case insensitively
-S, --smart-case Match case insensitively unless PATTERN contains uppercase characters
-w, --word-regexp Only match whole words
--ignore= Ignore files/directories matching pattern
--vcs-ignore= VCS ignore files (default: .gitignore)
--global-gitignore Use git's global gitignore file for ignore patterns
--home-ptignore Use $Home/.ptignore file for ignore patterns
-U, --skip-vcs-ignores Don't use VCS ignore file for ignore patterns
-g= Print filenames matching PATTERN
-G, --file-search-regexp= PATTERN Limit search to filenames matching PATTERN
--depth= Search up to NUM directories deep (default: 25)
-f, --follow Follow symlinks
--hidden Search hidden files and directories
Help Options:
-h, --help Show this help message
検索時によく指定するオプションを設定として保存しておきたい場合、TOML形式の設定ファイル($HOME/.ptconfig.tomlやカレントディレクトリのptconfig.toml)にオプションを記述しておきます。内容はコマンドラインオプションと同様の書式となります。
color = true context = 3 ignore = ["dir1", "dir2"] color-path = "1;34"
色設定や無視するフォルダの設定を記述しておくと便利です。
まとめ
The Platinum Searcherを利用すれば、UTF-8だけではなくEUC-JPやShift JISで記述されたテキストファイルも高速検索することができます。Go言語で作られているためUnix由来のツールがうまく動かない事が多いWindows環境で簡単に使えるのも魅力的だと思います。
