irb history not working
Asked Answered
S

7

16

in ~/.irbrc i have these lines:

require 'irb/ext/save-history'
#History configuration
IRB.conf[:SAVE_HISTORY] = 100
IRB.conf[:HISTORY_FILE] = "#{ENV['HOME']}/.irb-save-history"

and yet when i run irb and hit the up arrow nothing happens. also the irb history file specified is not getting created and nothing is logged to it.

Steamheated answered 14/1, 2010 at 17:1 Comment(7)
What platform are you using? I'm pretty sure the default OS X install doesn't have readline support built in due to licensing issues.Bensky
What you have there appears to work for me on doze, except that I have to hit two up arrows for some reason.Gaskin
This solution worked for me: https://mcmap.net/q/276953/-history-not-savingAfeard
Code in the question worked fine for me on OS X Mavericks.Bernettabernette
See also #37848322 which discusses how ruby must be complied with readlineDaugava
If you're on OS X see Jared's comment.Gaskin
Try Ctrl + P / Ctrl + N before giving up. The actual keybinding to recall the previous commands from history is a feature of readline / libedit, which may be misconfigured on your system. I don't have the details, but you might be able to correct this with your own ~/.inputrc / ~/.editrc, by comparing with another system where it works.Probability
A
11

I don't have an answer for you why the above doesn't work, but I did find a file, /etc/irbrc on my system (OS X - Snow Leopard, Ruby 1.8.7) that does provide a working, persistent history for me. So two pieces of advice: i) check your /etc/irbrc (or equivalent) to make sure that there isn't anything in there that might interfere with your settings, and ii) try out the settings below to see if you can get history working that way.

# Some default enhancements/settings for IRB, based on
# http://wiki.rubygarden.org/Ruby/page/show/Irb/TipsAndTricks

unless defined? ETC_IRBRC_LOADED

  # Require RubyGems by default.
  require 'rubygems'

  # Activate auto-completion.
  require 'irb/completion'

  # Use the simple prompt if possible.
  IRB.conf[:PROMPT_MODE] = :SIMPLE if IRB.conf[:PROMPT_MODE] == :DEFAULT

  # Setup permanent history.
  HISTFILE = "~/.irb_history"
  MAXHISTSIZE = 100
  begin
    histfile = File::expand_path(HISTFILE)
    if File::exists?(histfile)
      lines = IO::readlines(histfile).collect { |line| line.chomp }
      puts "Read #{lines.nitems} saved history commands from '#{histfile}'." if $VERBOSE
      Readline::HISTORY.push(*lines)
    else
      puts "History file '#{histfile}' was empty or non-existant." if $VERBOSE
    end
    Kernel::at_exit do
      lines = Readline::HISTORY.to_a.reverse.uniq.reverse
      lines = lines[-MAXHISTSIZE, MAXHISTSIZE] if lines.nitems > MAXHISTSIZE
      puts "Saving #{lines.length} history lines to '#{histfile}'." if $VERBOSE
      File::open(histfile, File::WRONLY|File::CREAT|File::TRUNC) { |io| io.puts lines.join("\n") }
    end
  rescue => e
    puts "Error when configuring permanent history: #{e}" if $VERBOSE
  end

  ETC_IRBRC_LOADED=true
end
Appendicle answered 14/1, 2010 at 17:50 Comment(4)
despite that this seems kind of like faking it, this works for me. I know irb must have this feature built in somewhere. Oh well, i have a history now. thanks!Steamheated
This was a great help to me. I would however note that I changed: lines.nitems into lines.count because lines.nitems don't work for me.Kalina
I also had to add: require 'irb/ext/save-history'Kalina
/etc/irbrc on OS X is now out of date. See this question.Lafleur
A
20

irb history works in Debian Linux out of the box. There's no etc/irbrc, nor do I have a ~/.irbrc. So, hmmmm.

This person put a bit more in his irbrc than you did. Do you suppose the ARGV.concat could be the missing piece?

require 'irb/completion'
require 'irb/ext/save-history'
ARGV.concat [ "--readline", "--prompt-mode", "simple" ]
IRB.conf[:SAVE_HISTORY] = 100
IRB.conf[:HISTORY_FILE] = "#{ENV['HOME']}/.irb-save-history" 
Anarch answered 14/1, 2010 at 17:56 Comment(5)
RVM automatically adds an irbrc that does it. Not present on Ubuntu out of box without RVM.Indicatory
You are right! This is true of my Ubuntu 20.04-based Linux distro as well as Debian Bullseye (stable, which I tried out with Docker), no /etc/irbc, no ~/.irbc. The difference on my desktop was I had a ~/.inputrc that was messing up the up/down arrow key bindings for some reason. Folks who find themselves in that situation might want to try Ctrl + P to see if that works, before totally giving up.Probability
I don't think that ARGV.concat does anything though; I had to use IRB.conf[:PROMPT_MODE] = :SIMPLE instead, as described in the irb docs. The other settings above were the defaults, excepting the history seems to default to 1000 and the :HISTORY_FILE is ~/.irb_history. So the config settings might be extraneous to the OP's problem, as stated, which probably has more to do with a bug in irb that the time that they posted, or a misconfigured readline or libedit library.Probability
@TheDudeAbides I would bet money on things having changed since 2010Anarch
Not that much, though, if you're using a Linux distro like CentOS 7.Probability
A
11

I don't have an answer for you why the above doesn't work, but I did find a file, /etc/irbrc on my system (OS X - Snow Leopard, Ruby 1.8.7) that does provide a working, persistent history for me. So two pieces of advice: i) check your /etc/irbrc (or equivalent) to make sure that there isn't anything in there that might interfere with your settings, and ii) try out the settings below to see if you can get history working that way.

# Some default enhancements/settings for IRB, based on
# http://wiki.rubygarden.org/Ruby/page/show/Irb/TipsAndTricks

unless defined? ETC_IRBRC_LOADED

  # Require RubyGems by default.
  require 'rubygems'

  # Activate auto-completion.
  require 'irb/completion'

  # Use the simple prompt if possible.
  IRB.conf[:PROMPT_MODE] = :SIMPLE if IRB.conf[:PROMPT_MODE] == :DEFAULT

  # Setup permanent history.
  HISTFILE = "~/.irb_history"
  MAXHISTSIZE = 100
  begin
    histfile = File::expand_path(HISTFILE)
    if File::exists?(histfile)
      lines = IO::readlines(histfile).collect { |line| line.chomp }
      puts "Read #{lines.nitems} saved history commands from '#{histfile}'." if $VERBOSE
      Readline::HISTORY.push(*lines)
    else
      puts "History file '#{histfile}' was empty or non-existant." if $VERBOSE
    end
    Kernel::at_exit do
      lines = Readline::HISTORY.to_a.reverse.uniq.reverse
      lines = lines[-MAXHISTSIZE, MAXHISTSIZE] if lines.nitems > MAXHISTSIZE
      puts "Saving #{lines.length} history lines to '#{histfile}'." if $VERBOSE
      File::open(histfile, File::WRONLY|File::CREAT|File::TRUNC) { |io| io.puts lines.join("\n") }
    end
  rescue => e
    puts "Error when configuring permanent history: #{e}" if $VERBOSE
  end

  ETC_IRBRC_LOADED=true
end
Appendicle answered 14/1, 2010 at 17:50 Comment(4)
despite that this seems kind of like faking it, this works for me. I know irb must have this feature built in somewhere. Oh well, i have a history now. thanks!Steamheated
This was a great help to me. I would however note that I changed: lines.nitems into lines.count because lines.nitems don't work for me.Kalina
I also had to add: require 'irb/ext/save-history'Kalina
/etc/irbrc on OS X is now out of date. See this question.Lafleur
C
1

This is a known bug with a patch available. Easiest solution is to overwrite save-history.rb:

/usr/lib/ruby/1.8/irb/ext/save-history.rb

with a fixed version:

http://pastie.org/513500

or to do it in one go:

wget -O /usr/lib/ruby/1.8/irb/ext/save-history.rb http://pastie.org/pastes/513500/download
Chatterer answered 6/2, 2010 at 13:35 Comment(1)
is there a bug report on this anywhere? a linK? Thanks.Gaskin
D
0

Check to make sure you built ruby with libreadline as irb history seems to not work without it.

Diluvium answered 3/1, 2012 at 17:44 Comment(0)
L
0

This may also happen if you have extra irb config file, e.g. ~/.irbrc. If this is the case, copy the content from liwp's answer to the extra config and it should work.

Lamellibranch answered 5/8, 2016 at 10:58 Comment(0)
S
0

I had the same problem on ubuntu 20.04 and fixed it by running:

gem install irb
Snakemouth answered 11/12, 2020 at 14:17 Comment(0)
R
0

You may simply just need to set IRBRC environment variable. Add to your ~/.bash_profile or equivalent,

export IRBRC=~/.irbrc

which can/should contain:

require 'irb/ext/save-history'
IRB.conf[:SAVE_HISTORY] = 1000
IRB.conf[:HISTORY_FILE] = File.expand_path('~/.irb_history')
Retirement answered 7/6, 2023 at 13:35 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.