Rbenv Irb History Is Not Saving

rbenv irb history is not saving

I found this way for solving my problem. In file ~/.irbrc write:

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

Found in this question: irb history not working

IRB history not working with Ruby 2.3.0

OS X's command-line editing is based on the libedit library. OS X has a version of the readline library which is a wrapper around libedit, but it does not behave completely like GNU readline. irb history works in Ruby built with OS X's wrapper up to Ruby 2.1, but Ruby 2.2 and later need to be built with GNU readline for irb history to work.

In the following, 2.3.0 can be any Ruby version from 2.2.0 on. I wrote 2.3.0 since that's what Evan used.

Using Homebrew

If you install ruby using homebrew, it will bring with a working version of readline.

  • brew install ruby

Then follow the instructions to add it to your PATH. Then execute gem install irb if it says can't find gem irb.

Using MacPorts

rbenv doesn't know about MacPorts, so you need to explicitly tell it to use MacPorts' readline.

  • sudo port install readline if it isn't installed already.
  • rbenv uninstall 2.3.0
  • RUBY_CONFIGURE_OPTS=--with-readline-dir=/opt/local rbenv install 2.3.0

Using Homebrew with rbenv

rbenv automatically detects homebrew and looks in it for readline, so, if you're using Homebrew and irb history doesn't work, you either haven't installed readline or you built your Ruby before you installed readline.

  • brew install readline if it isn't installed already
  • rbenv uninstall 2.3.0
  • rbenv install 2.3.0

irb history not working

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

IRB not loading library libreadline

You may have upgraded something that replaced the readline library and it's now necessary to rebuild your Ruby. This sort of thing happens infrequently with Homebrew when you run brew clean and it deletes an obsolete version of a dependency, not realizing one of your Ruby builds depends on it. This is because Rbenv has no way of communicating to Homebrew that it still needs that library.

This is usually easy to fix. Try:

rbenv install 2.4.2

You might also want to take the time to update to the latest version which is, as of today, 2.5.3.

History support in Ruby 2.7 REPL


Is there a way I to get the patched-irb work with Ruby 2.7?

Keeping patched versions of software working with new versions is always tricky, especially if you're making assumptions about how the guts work.

It's harder when your patched version has no relationship to the original. You have made your own irb repository that has no relationship to the original irb and so can't use version control to keep up to date.

Instead, make the fork of the original repository and make your patches in the fork. Then by pulling from the original upstream repository you can better keep up to date with changes, make sure everything still works, and find out exactly at what upstream version your patches failed.

Fortunately irb was recently spun off into its own gem. Fork that gem, make your patches in that fork, and stay up to date by periodically pulling changes from the upstream gem.

Then submit your patches upstream as pull requests. If they're merged they will be kept working as part of the larger project. It's no longer your job to maintain them separately.



Related Topics



Leave a reply



Submit