Why does irb insert control characters into my reverse-interactive-search buffer?
If I do a reverse interactive search in irb and then enter a control character (arrow keys are the only ones I've seen this with actually). So I start with this session:
开发者_运维问答$ irb
>> print "hello"
hello
then enter ^r
, h
$ irb
>> print "hello"
hello
(reverse-i-search)`he': print "hello"
So far so good. Then I hit the left arrow, and I end up with this:
$ irb
>> print "hello"
hello
>> print "[Dhello"
Subsequent use of the arrows moves the cursor around normally.
This won't solve your problem, but at least I can answer the question you asked (the Why):
The left arrow gets entered into the terminal as three characters, ESC, [, and D. (You can test this by typing read
and pressing the left arrow, which displays ^[[D
, where ^[
is how the escape character gets rendered.)
Somehow irb
picks up the ESC character when it shouldn't; so ESC ends the history search, and [D
gets entered literally. Looking at other programs using readline, Bash and Python behave correctly, but ledit
(yeah, obscure -- sorry ^^) is having this problem as well.
Unless someone knows a solution for this, the only workaround I can think of is pressing ESC instead of an arrow key to exit history search. Or hitting enter if you don't need to edit further, of course.
Do you have vi editing mode enabled?
~ $ irb
1.9.3p392 :001 > Readline.vi_editing_mode?
=> true
I had it turned on in my $HOME/.inputrc
file via set editing-mode vi
. Disabling it "fixed" my arrow-keys.
As mentioned by @JoLiss, other readline programs (bash, etc.) were also working correctly; so AFAICT, it's a Ruby problem. See: http://bugs.ruby-lang.org/issues/7859#change-36333
精彩评论