5

In Rails 4 rails console I can type history and it will behave just like the history command from the bash shell. E.g.:

[25] my_rails_project »  history
  1: Nomination
  2: {:ad => "asdfsdasadf"}
  3: Nomination.count
  4: Nomination.count.to_sql
  5: Nomination.all.class
  6: Nomination.all.to_sql
  ...

Is there a way to search that history, e.g. history | grep Nomination? How about tail?

Note: When I initially wrote this question I thought the history command came from Rails itself, but it comes from the pry gem which I have in my system (my Gemfile specifies the jazz_hands gem which pulls in pry). Pry does in fact have a grep feature, e.g. history --grep Nomination will give me lines 1 and 3-6 above. It also has a tail feature. These are documented here: https://github.com/pry/pry/wiki/History

Purplejacket
  • 1,808
  • 2
  • 25
  • 43

4 Answers4

8
  1. Go to rails console

  2. press <ctrl> + r and type few letters of the command.

  3. use <ctrl> + r to choose other commands that match the search

  4. use <ctrl> + <shift> + r to choose the command in the reverse direction

Exactly like the <ctrl> + r in bash shell

usha
  • 28,973
  • 5
  • 72
  • 93
7

You can use ~/.irb-history for this purpose. So, the following can be used (you must be knowing how to do this, but this is only for reference):

tail -fn0 ~/.irb-history  # for tailing
cat ~/.irb-history | grep something # for searching

Note that, you might have both the files: ~/.irb-history and ~/.irb_history, and any one of them can be more up to date than the other. I have not been able to resolve this mystery yet. So, use the one you find more suitable for yourself.


UPDATE: You can access history in a variable using the following logic (it took me a while to read the code Pry uses, try edit history inside pry):

 def pry_history
   arr = []
   history = Pry::History.new
   history.send(:read_from_file) do |line|
     arr.push line.chomp
   end
   arr
 end

Now, you can simply call pry_history to get an array of pry's history. You can further save it inside your .pryrc configuration file and use it whenever you want in pry.

Stoic
  • 10,536
  • 6
  • 41
  • 60
2

Probably this is what you are looking for:

def search_history(filter)
    puts `cat ~/.irb-history | grep "#{filter}"`
end
Arnold Roa
  • 7,335
  • 5
  • 50
  • 69
1

It's worth noting that if you are using pry then the history file will be located at ~/.pry_history. You may also come across this history file .byebug_history.

I've added the following commands to my ~/.pryrc config file.

Pry.config.commands.alias_command 'find.history', 'hist -G'

Pry::Commands.command 'search.history', 'filter commands in the history using fzf and output the selected command' do
  puts `cat ~/.pry_history | sort | uniq | fzf --tac --tiebreak=index --height=20`
end

Pry::Commands.command 'search.history.run', 'filter commands in the history using fzf and execute the selected command' do
  _pry_.input = StringIO.new(`cat ~/.pry_history | sort | uniq | fzf --tac --tiebreak=index --height=20 | perl -pe 'chomp if eof'`)
end
Andrew
  • 3,733
  • 1
  • 35
  • 36