6

I've recently started using Ruby 3 more and it seems the home key (to take me to the beginning of the line) the end key (end of the line) and ctrl-u (clear the line) aren't working.

I'm running Arch Linux with Ruby 3.1.1p18 and a zsh shell. The keys work fine on Ruby 2.7. I've tried with Alacritty and xfce4-terminal and both have the same issue. I'm not using Tmux or anything similar.

There is this similar question from a few years ago but that's for Windows and the solutions didn't help: Backspace and arrow keys aren't working in IRB(Git Bash console) on windows machine

KNejad
  • 2,376
  • 2
  • 14
  • 26
  • This is most likely an issue with your terminal bindings, not IRB. Check your *TERM* variable and any vi, emacs, or readline bindings for your shell. – Todd A. Jacobs Jun 10 '22 at 23:15
  • Thanks @ToddA.Jacobs, when I don't have x launched it seems the home and end keys work but ctrl-u does not. When I launch X they don't work. I've tried clearing my zshrc, changing my TERM from xterm-256color to linux (which is what it is before launching x) as well as switching to bash and trying a different terminal emulator. No matter what I try, when X is running non of the keys work and instead I get a few strange characters printing – KNejad Jun 19 '22 at 11:42
  • 1
    This solution worked - https://github.com/ruby/irb/issues/330#issuecomment-1132017233 – Deepak Mahakale Nov 04 '22 at 09:04

4 Answers4

1

I also specifically see ctrl-u not working in Ruby 3's irb, despite other ctrl commands working (ctrl-a, ctrl-k).

For the meantime, I'm using pry for my REPL. Bonus… I prefer it's simpler auto-complete.

gem install pry
pry
ruby_slinger
  • 111
  • 3
1

I have found a good solution for this issue in this Archlinux guide: Home_and_End_keys_not_working

Just create the file ~/.inputrc with the content:

"\e[1~": beginning-of-line
"\e[4~": end-of-line
"\e[7~": beginning-of-line
"\e[8~": end-of-line
"\eOH": beginning-of-line
"\eOF": end-of-line
"\e[H": beginning-of-line
"\e[F": end-of-line

This solution works pretty good for me.

  • Oh wow, almost exactly a year after I asked the question you solved my issue with just a simple config file :) Thanks! – KNejad Jun 14 '23 at 17:06
0

I can't take credit for this as this is just a copy from: https://github.com/ruby/irb/issues/330, but I hope this is useful for those that stumbled upon this question without a good answer.

For those who would prefer to do the above patches at runtime, for macOS/iTerm2, add this code to "~/.irbrc":

require "reline/ansi"

if defined?(Reline::ANSI::CAPNAME_KEY_BINDINGS) # make sure you're using an affected version
  # Fix insert, delete, pgup, and pgdown.
  Reline::ANSI::CAPNAME_KEY_BINDINGS.merge!({
    "kich1" => :ed_ignore,
    "kdch1" => :key_delete,
    "kpp" => :ed_ignore,
    "knp" => :ed_ignore
  })

  Reline::ANSI.singleton_class.prepend(
    Module.new do
      def set_default_key_bindings(config)
        # Fix home and end.
        set_default_key_bindings_comprehensive_list(config)
        # Fix iTerm2 insert.
        key = [239, 157, 134]
        func = :ed_ignore
        config.add_default_key_binding_by_keymap(:emacs, key, func)
        config.add_default_key_binding_by_keymap(:vi_insert, key, func)
        config.add_default_key_binding_by_keymap(:vi_command, key, func)
        # The rest of the behavior.
        super
      end
    end
  )
end

No switch to application mode needed, and no iTerm2 rebinding.

Roman Gaufman
  • 1,104
  • 1
  • 13
  • 17
0

This was fixed in a newer version of reline, but in the meantime what you can do is add this to your ~/.irbrc:

Reline::KeyActor::Emacs::MAPPING[21] = :unix_line_discard
KARASZI István
  • 30,900
  • 8
  • 101
  • 128