ruby / irb

interactive Ruby
BSD 2-Clause "Simplified" License
387 stars 119 forks source link

Up arrow does not work with Ruby 2.7.x on macOS #107

Closed rmehner closed 4 years ago

rmehner commented 4 years ago

Description

👋

I'm running into this weird issue and am not really sure if this is the right place to report, but here we go:

When I'm using Ruby 2.7.0 or Ruby 2.7.1 on macOS 10.15.5, irb does not work with using the arrow keys for navigating the history.

E.g. if I do the following:

irb in Terminal Enter puts "foo" and hit Return Hit arrow key up -> Nothing happens Funnily enough, CTRL^R brings up reverse search for history, as expected.

If I switch to Ruby 2.6.x (any version), Ruby 2.8.0-dev or even Ruby 2.3.1 irb works as expected. Both 2.7.0 and 2.7.1 do not work.

I've tried installing the versions a new and even reinstalled readline from Homebrew. The build output even mentions it is using Homebrew's readline.

Readline version: 8.0.4

I've set up a Ubuntu 20.04 VM with rbenv to test if the problem happens there as well and it doesn't, so it seems to be something with my Mac setup.

Maybe you, kind reader, heard about this before and have an idea, because I'm really at loss here :) Thanks in advance!

Terminal Emulator

iTerm2 (Build 3.3.9)

Setting Files

Are you using ~/.irbrc and ~/.inputrc?

~/irbrc (I've tried running it with irb -f which is supposed to ignore this file)

require 'rubygems'
# require 'wirble'
require 'pp'

# Wirble.init
# Wirble.colorize

# print SQL to STDOUT
require 'logger'
if ENV.include?('RAILS_ENV') && !Object.const_defined?('RAILS_DEFAULT_LOGGER')
   Object.const_set('RAILS_DEFAULT_LOGGER', Logger.new(STDOUT))
elsif defined?(Rails)
   ActiveRecord::Base.logger = Logger.new(STDOUT)
end

# Autocomplete
require 'irb/completion'

# Prompt behavior
ARGV.concat [ "--readline", "--prompt-mode", "simple" ]

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

# Easily print methods local to an object's class
class Object
  def local_methods
    (methods - Object.instance_methods).sort
  end
end

~/.inputrc

# Edit commands in Vi mode
#set editing-mode vi
# set keymap vi
#
## SMARTER TAB-COMPLETION (Readline bindings) ##

# Perform file completion in a case insensitive fashion
set completion-ignore-case on

# Treat hyphens and underscores as equivalent
set completion-map-case on

# Display matches for ambiguous patterns at first tab press
set show-all-if-ambiguous on

# Immediately add a trailing slash when autocompleting symlinks to directories
set mark-symlinked-directories on

# Use the text that has already been typed as the prefix for searching through
# commands (basically more intelligent Up/Down behavior)
"\e[A": history-search-backward
"\e[B": history-search-forward

# Do not autocomplete hidden files unless the pattern explicitly begins with a dot
set match-hidden-files off

# Show all autocomplete results at once
set page-completions off

# If there are more than 200 possible completions for a word, ask to show them all
set completion-query-items 200

# Show extra file information when completing, like `ls -F` does
set visible-stats on

# Be more intelligent when autocompleting by also looking at the text after
# the cursor. For example, when the current line is "cd ~/src/mozil", and
# the cursor is on the "z", pressing Tab will not autocomplete it to "cd
# ~/src/mozillail", but to "cd ~/src/mozilla". (This is supported by the
# Readline used by Bash 4.)
set skip-completed-text on

# Allow UTF-8 input and output, instead of showing stuff like $'\0123\0456'
set input-meta on
set output-meta on
set convert-meta off

# Use Alt/Meta + Delete to delete the preceding word
"\e[3;3~": kill-word

irb version: irb 1.2.4 (2020-05-02) LC_CTYPE=en_US.UTF-8

hmans commented 4 years ago

Hi, I had the same problem. I'm guessing the conflict is with these two lines from your .inputrc:

"\e[A": history-search-backward
"\e[B": history-search-forward

Removing these fixed irb for me. Good luck!

rmehner commented 4 years ago

Confirmed, this fixes the issue. Still curious that with 2.6 or 2.8 it worked fine, but this is godsend.

Thank you @hmans :)

aycabta commented 4 years ago

This is fixed by https://github.com/ruby/reline/pull/142.

ogirginc commented 2 years ago

In my case, I was able to solve this issue by disabling USE_MULTILINE from irbrc:

IRB.conf[:USE_MULTILINE] = false