Configure readline to paste multiline code properly

This is part of the Semicolon&Sons Code Diary - consisting of lessons learned on the job. You're in the workflows category.

Last Updated: 2024-11-21

When I copy pasted some into the terminal, the Ruby code below did not behave as it did when inside a Ruby file.

str = "hi"
str
  .strip
  .sub(/i/, "allo")

Output when in a Ruby file

=> "hallo"

Output when copy pasted:

# it got executed one line at a time...
foo.strip
=> "hi"
[7] pry(main)> foo = "hi"
=> "hi"
[8] pry(main)> foo
=> "hi"
...

The issue was to do with readline. The readline bash program has an interface to Pry via the Ruby core Readline module. I noticed in man readline that I could set a system wide .inputrc. So I added:

set enable-bracketed-paste On

And noticed that pry now copied all the lines in as one and waited for an enter key before executing

# pasting action
str = "hi"
str
.strip
.sub(/i/, "allo")
# pasting done in one unit. .. awaiting enter keypress to execute all

Problem solved