KNejad
KNejad

Reputation: 2508

Ruby 3 IRB home end and ctrl-u not working

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

Upvotes: 6

Views: 621

Answers (4)

KARASZI István
KARASZI István

Reputation: 31467

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

Upvotes: 0

Carlos Jiménez
Carlos Jiménez

Reputation: 96

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.

Upvotes: 2

Roman Gaufman
Roman Gaufman

Reputation: 1124

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.

Upvotes: 0

ruby_slinger
ruby_slinger

Reputation: 131

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

Upvotes: 1

Related Questions