Ruby 3 IRB home end and ctrl-u not working
Asked Answered
X

4

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

Xylography answered 10/6, 2022 at 8:28 Comment(3)
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.Dragone
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 printingXylography
This solution worked - github.com/ruby/irb/issues/330#issuecomment-1132017233Weltschmerz
R
2

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.

Remorseful answered 14/6, 2023 at 17:2 Comment(1)
Oh wow, almost exactly a year after I asked the question you solved my issue with just a simple config file :) Thanks!Xylography
P
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
Pinkard answered 8/7, 2022 at 15:15 Comment(0)
R
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.

Repartition answered 4/2, 2023 at 16:27 Comment(0)
H
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
Heterologous answered 13/7, 2023 at 10:21 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.