Wandering Thoughts archives


Some bits of how Bash and GNU Readline's "bracketed paste" mode behaves

Recent versions of Bash and GNU Readline now default to special handling of pasting into them, in what is called "bracketed paste" mode. This mode requires you to explicitly hit Return in order to have the pasted text accepted, and allows you to edit the paste before then. This simple description might leave you wondering how bracketed paste mode interacts with various other things, and as it happens I have done some experiments here (out of curiosity, since bracketed paste mode is not for us).

First, although it's not completely clear from the description, bracketed paste treats multiple lines pasted at once as a single unit for the purposes of editing and accepting them (or rejecting them with a Control-C). After they've been accepted, Bash (and presumably Readline) will treat them as separate lines for history and so on. This is probably the behavior you want if you're pasting multiple commands.

Second, bracketed paste sends all of your pasted text to Bash (or the Readline based program you're using), regardless of how it would otherwise be interpreted if you typed it, and Bash will process all lines as command lines. That sounds abstract, so let's give you a concrete example. Suppose that you have the following text to cut and paste from some instructions:

cat >/etc/some-config.conf
directive one
directive two

If you paste this normally (and then hit Control-D afterward), the some-config.conf winds up with the two directives in it, because Bash first runs the cat and then the remaining two lines are read by cat. If you paste this into a bracketed paste Bash and then hit Return to accept it all, Bash first runs cat, with it reading from the terminal as standard input and waiting for you to type something to it, and then when you hit Ctrl-D it will attempt to run 'directive one' and 'directive two' as commands. There's no visual indication that this is happening and that Bash has grabbed the second and third lines for itself.

On the one hand, in the Unix model it's hard to see how Bash could do this differently without deep cooperation from the kernel terminal driver (which isn't available). On the other hand, this makes bracketed paste mode dangerously different from un-bracketed paste in a way that is not particularly obvious and I think is not necessarily intuitive.

PS: Because I tested it, Bash really takes all of your pasted text as command lines, even if one of the pasted commands is 'read', which Bash handles internally and so could pass your text to. I assume that internally, Readline is buffering all of this up as command input. Programs that use Readline for multiple things might mix your pasted input in a different way.

unix/BracketedPasteBehaviorNotes written at 22:28:25; Add Comment

Page tools: See As Normal.
Login: Password:
Atom Syndication: Recent Pages, Recent Comments.

This dinky wiki is brought to you by the Insane Hackers Guild, Python sub-branch.