How to use FZF in Pry console for search command history

FZF is a amazing tool for command-line fuzzy search, the feature that I used mostly every day is searching command history in zsh by hit ctrl + r. And I love pry too, I spent a lots time in pry every day of work, but I can not find any way to use fzf in pry console, so I found a way to make fzf works in pry by myself. It’s quite simple.

First,we need install a gem rb-readline, which is a pure-ruby implementation of GNU Readline

gem install rb-readline

Then add this to your ~/.pryr

require 'rb-readline'
require 'readline'
if defined?(RbReadline)
def RbReadline.rl_reverse_search_history(sign, key)
rl_insert_text `cat ~/.pry_history | fzf --tac | tr '\n' ' '`
end
end

Then open your pry, and hit ctrl + r, it works right ? :), and you can hit ctrl + p/n, to move to previous/next command.

If you are a rails developer , your may need add this gem to your Gemfile

group :developement
gem 'rb-readline'
end

If you don’t wanna do this, you can do some hack in the head of your pryrc: