How make `reverse-i-search` history use fzf in irb or pry console

1.1k views Asked by At

I have looked around, can not find anyway to use fzf to search history in irb or pry console. Is there any way make it?

1

There are 1 answers

0
Fangxing On

After a look around, I get it that command completion is associated with GNU Readline, and then I found a pure ruby implementation of Readline, in fact It's quite easy to make fzf works in pry with RbReadline, what I have do is overwrite the RbReadline's class method rl_reverse_search_history, which is triggered when we hit Ctrl + R in pry console.

Summary of what I have done:

  1. Install rb-readline

     gem install rb-readline
    
  2. Modify your .pryrc add this

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

The rl_insert_text inserts the result you selected from fzf into the terminal after your cursor.