The Platinum Seacher

The Platinum Searcher: search source code, faster than ack

The Platinum Searcher (pt) is a source code search utility similar to ack and ag (“The Silver Searcher”) written in Go programming language. It is claimed to be 3 to 5 times faster than ack.

Source code searchers

Ack

It all started with ack, which is a tool like grep to quickly search files, but instead of being a generic search utility, it is optimized for programmers: for example, by default it ignores version control directories, such as .git, binary files, build directories, etc., allows to specify file type easily, and has a nice match highlighting. Ack is written in Perl.

Ag (The Silver Searcher)

Then came The Silver Searcher, ag, which is pretty much Ack rewritten in C to be as fast as possible: it takes advantage of multiple CPU cores, uses memory mapping for files, and implements advanced text search algorithms for literal string searchers. It uses PCRE engine with JIT for regular expressions searches. Its author, Geoff Greer, wrote a few blog posts describing his performance optimization work for Ag:

Platinum Searcher (pt)

The Platinum Searcher is the latest addition to this family, another grep alternative for programmers, this time written in Go. Memory-safe language makes pt safer (I remember having segfaults in Ag, although it was during its very early days). It uses Go’s standard regexp package, thus it avoids exponential time matching. Platinum Searcher can search not only in files encoded with UTF-8, but also EUC-JP and Shift_JIS, making it very useful for Japanese programmers.

Installing and using pt

The Platinum Searcher binaries are available for Windows, Mac OS X, Linux (including ARM) from its GitHub releases page. It’s just a single binary, so you can download it, put it into your $PATH and start searching. You can also install it on OS X with Homebrew by typing brew install pt.

pt is easy to use. To search for a pattern in the current directory and all of its subdirectories, type:

pt PATTERN

Searching for "regexp" in pt source code

If you specify a path after the pattern, it will search in this path. You can also specify options before pattern. Here’s the usage information:

Usage:
  pt [OPTIONS] PATTERN [PATH]

Application Options:
      --color               Print color codes in results (Enabled by default)
      --nocolor             Don't print color codes in results (Disabled by default)
      --nogroup             Don't print file name at header (Disabled by default)
  -l, --files-with-matches  Only print filenames that contain matches
      --vcs-ignore=         VCS ignore files (.gitignore, .hgignore, .ptignore)
      --noptignore          Don't use default ($Home/.ptignore) file for ignore patterns
      --noglobal-gitignore  Don't use git's global gitignore file for ignore patterns
  -U, --skip-vsc-ignores    Don't use VCS ignore file for ignore patterns. Still obey .ptignore
      --ignore=             Ignore files/directories matching pattern
  -i, --ignore-case         Match case insensitively
  -S, --smart-case          Match case insensitively unless PATTERN contains uppercase characters
  -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
  -A, --after=              Print lines after match
  -B, --before=             Print lines before match
  -C, --context=            Print lines before and after match
  -o, --output-encode=      Specify output encoding (none, jis, sjis, euc)
  -e                        Parse PATTERN as a regular expression (Disabled by default)
  -w, --word-regexp         Only match whole words
      --stats               Print stats about files scanned, time taken, etc
      --version             Show version

Source code and license

GitHub: https://github.com/monochromegane/the_platinum_searcher
License: MIT

Leave a Reply

Your email address will not be published. Required fields are marked *