78

Does anyone know how to view the "history" in the rails console?

Pressing the up arrow lets me iterate through recent commands, but I'd like to see them all together in a list. I'm basically looking for the rails equivalent of the Unix history utility.

Is this possible in rails? If so, how?

Ciro Santilli OurBigBook.com
  • 347,512
  • 102
  • 1,199
  • 985
dB'
  • 7,838
  • 15
  • 58
  • 101

7 Answers7

77

Look at ~/.irb-history, you will find the history there.

Chloe
  • 25,162
  • 40
  • 190
  • 357
lucas clemente
  • 6,255
  • 8
  • 41
  • 61
  • 3
    hm... for the record, it's actually `~/.irb-history` (with a dash, not an underscore) on my machine. Not sure why. I'm on a Mac and using RVM. Anyway, thanks! – dB' Jul 22 '12 at 15:51
  • 1
    Weird, I have both files, but the one with `_` seems to be more up to date than the other. Strange. – lucas clemente Jul 22 '12 at 16:04
  • I have both files too, but in my case `-` is more up to date. Strange indeed! – dB' Jul 22 '12 at 16:30
  • It's a hyphen on my Ubuntu laptop & and an underscore on our Ubuntu server. Odd indeed: they have the same version of ruby but it's hard-installed on the server and rvm-installed on my laptop. Wonder if it's something to do with rvm? – Max Williams Aug 22 '14 at 09:03
  • 5
    I found it in `~/.local/share/pry/pry_history` presumably due to a pry-rails gem in dev requirements in the venvs I was using – Abram Jan 05 '21 at 18:33
  • I also found it in "~/.local/"... If that's the default for ruby venvs they could have come up with a better naming... – Redoman Jan 09 '23 at 21:38
77

The best equivalent to the history command would be

puts Readline::HISTORY.to_a

inside the IRB session. This has the advantage that no filesystem logging to any of the$HOME/.*_history files needs to be configured (as is the case in AWS elastic beanstalk instances).

Perseids
  • 12,584
  • 5
  • 40
  • 64
25

I've tried the accepted answer, but our server didn't have a ~/.irb-history.

As it turned out, the history was kept in ~/.pry_history. Hope this helps.

Ja͢ck
  • 170,779
  • 38
  • 263
  • 309
  • Also here. Maybe when using pry gem, then it'll be stored in `~/.pry_history`, instead of `~/.irb_history`. – Penguin May 25 '17 at 03:00
6

Since we're already in the console,

lines = File.read("#{ENV['HOME']}/.pry_history");

or

lines = File.read("#{ENV['HOME']}/.irb-history");

then,

puts lines
valk
  • 9,363
  • 12
  • 59
  • 79
3

Try CTRL+R to open the search in the history

Jan Sršeň
  • 1,045
  • 3
  • 23
  • 46
1

Regarding

The best equivalent to the history command would be

currently (Pry version 0.14.1 on Ruby 3.0.2 for me) there is actually a history command which is an alias for hist.

bhfailor
  • 175
  • 2
  • 6
0

We can view it by using following command in console

cat ~/.irb-history
General Grievance
  • 4,555
  • 31
  • 31
  • 45
Jigar Bhatt
  • 4,217
  • 2
  • 34
  • 42