Project

rbgrep

0.0
The project is in a healthy, maintained state
rbgrep is a mostly grep-compatible search tool for Ruby codebases. By default it behaves like grep. With --context, each hit in a Ruby file is shown together with the method definition and class that enclose it, located with the Prism parser.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
 Dependencies

Runtime

>= 0.24
 Project Readme

rbgrep

A mostly grep-compatible search tool for Ruby codebases.

By default, rbgrep takes the same flags as grep and prints the same kind of output: a filename heading, then NN: hit lines. Add --context and each hit in a .rb file is shown together with the method definition and class that enclose it, located with the Prism parser.

Installation

gem install rbgrep

Usage

rbgrep [-rn -w -i -E -F -A/-B/-C --include=GLOB] PATTERN [PATH...]

Plain search works like grep (including \| alternation in basic regexps, or -E for extended ones):

$ rbgrep -n 'def visible?' app/models/news.rb
app/models/news.rb
55:   def visible?(user=User.current)

With --context, the hit arrives with the code that gives it meaning — the enclosing method body and the class frame, with -- marking elided stretches:

$ rbgrep -n --context 'def visible?' app/models/news.rb
app/models/news.rb
20- class News < ApplicationRecord
--
55:   def visible?(user=User.current)
56-     !user.nil? && user.allowed_to?(:view_news, project)
57-   end
--
105- end

A hit on a method definition shows the body (capped); a hit that merely references something shows a few surrounding lines. Files under test/ and spec/ directories, and non-Ruby files, print plain hits.

--head N caps output by hits, returns each hit whole, and reports how many were cut — unlike | head, you always know whether there was more:

$ rbgrep -rn --head 3 'def initialize' lib
...
... 48 more hits (raise --head to see them)

When reading from a pipe, rbgrep behaves exactly like grep — no headings, no context — so ... | rbgrep -v foo works as expected.

Use with coding agents

rbgrep is designed to be handed to an AI coding agent as its search tool. instruction.md is a ready-made instruction sheet: tell your agent to read and follow it.

License

MIT. See LICENSE.txt.