irb: In macOS, home and end keys no longer work
Description
In macOS (I am using Monterey 12.1), the home and end keys no longer move the cursor to the start of the line and the end of the line respectively. Instead, these keys output the escape sequences you see in the screenshot below.
This behavior is new in irb that ships with Ruby 3.1. In the previous version, these keys worked as expected.
Result of irb_info
Ruby version: 3.1.0
IRB version: irb 1.4.1 (2021-12-25)
InputMethod: ReidlineInputMethod with Reline 0.3.1
RUBY_PLATFORM: x86_64-darwin21
LANG env: en_US.UTF-8
East Asian Ambiguous Width: 1
Terminal Emulator
I am using iTerm2 Build 3.4.14 which is configured to use xterm-256color.
Setting Files
I am not using a settings file.
About this issue
- Original URL
- State: closed
- Created 2 years ago
- Reactions: 7
- Comments: 22 (2 by maintainers)
Commits related to this issue
- Always set default key bindings Many terminal emulators have default key combinations that do not match the information in terminfo. To maintain compatibility, the key bindings in the comprehensive l... — committed to jitingcn/reline by jitingcn 2 years ago
- Always set default key bindings The default key combinations of many terminal emulators did not match the information in terminfo. To maintain compatibility, the key bindings in the comprehensive lis... — committed to jitingcn/reline by jitingcn 2 years ago
- Always set default key bindings The default key combinations of many terminal emulators did not match the information in terminfo. To maintain compatibility, the key bindings in the comprehensive lis... — committed to jitingcn/reline by jitingcn 2 years ago
- Always set default key bindings The default key combinations of many terminal emulators did not match the information in terminfo. To maintain compatibility, the key bindings in the comprehensive lis... — committed to cover/reline by jitingcn 2 years ago
- Enter and leave keyboard transmit mode at the beginning and end of readline (and readmultiline). This fixes Home and End keys not working on macOS: https://github.com/ruby/irb/issues/330 We need to... — committed to sshock/reline by sshock a year ago
For those who would prefer to do the above patches at runtime, for macOS/iTerm2, add this code to “~/.irbrc”:
No switch to application mode needed, and no iTerm2 rebinding.
I discovered a new workaround that is relatively simple and fixes all keys; just modify
~/.inputrcto contain the following:I did a bunch of testing and
I am convinced that iTerm2 is sending the wrong escape sequence for Home and EndI believe iTerm2 is sending the correct sequences for everything except Insert.Update: The main problem with Home and End is that the latest version of Reline only maps the application mode codes (SS3 == escape +
O) not normal mode codes (CSI == escape +[), but it doesn’t put the terminal into application mode.The main problem with Delete, PgUp, and PgDown is that Reline is not mapping them to anything.
So here are all the issues and workarounds / solutions I’ve come up with:
Home key (khome)
\eOHnot\e[H.\EOHEnd key (kend)
\eOFnot\E[F.\EOFDelete key (kdch1 )
'kdch1'to:key_deleteInsert key (kich1)
\E[2~'kich1'to:ed_ignorePgUp key (kpp)
'kpp'to:ed_ignorePgDn key (knp)
'knp'to:ed_ignoreHere’s a screenshot showing Home, End, and Insert key mappings changed in iTerm2 (not recommended, except for Insert key):
Here’s what I added to ~/.irbrc to make it switch the terminal into application mode:
Here’s a screenshot showing how to fix the Delete key in Reline, and also how to prevent Insert, PgUp, and PgDn from printing out nasties:
I am seeing a similar issue with the Delete key. Output inside irb:
ruby -r reline -e 'while (c = Reline::IOGate.getc); pp c; end'shows the following for the Delete key.I think this has been addressed by https://github.com/ruby/reline/pull/569? cc @tompng
I discovered the following workaround for my setup (using archlinux, kitty and fish):
IRB.conf[:USE_MULTILINE] = falsein~/.irbrc@neonn I think adding your experience is helpful and a good reminder to the maintainers of irb and reline that we still have people needing this to be fixed.
I’ve been waiting patiently for these fixes to be merged in (the last one is still DRAFT, but the first two are tiny and have been ready to go for 9 months):
@rgaufman the delete key is fixed now, but we need to get one of these (preferably both) across the finish line to fix Home and End:
I think this may be a problem in reline, introduced in reline v0.2.7.
I switched to Ruby 3.0 with reline 0.2.0 and the problem went away. Then I tried various versions of reline and the problem first shows up in reline 0.2.7.
Digging further, it appears that the problem surfaced when default key bindings started using
set_default_key_bindings_terminfoinstead ofset_default_key_bindings_comprehensive_list.I managed to fix this by mapping Home and End keys in my iTerm2 profile key mappings to the hex codes 0x01 and 0x05 respectively. These hex codes map to Ctrl+A and Ctrl+E which in an iTerm2 session, function as Home (go to beginning of line) and End (go to end of line) and work as expected in irb. From my profile key mapping settings: