15

I often use ctrl+c to copy text from some gui application and then paste it into my terminal emulator (terminator), using right-mouse-click-menu and paste. Sometimes I forget that the clipboard contains several lines, which when pasted into bash causes each line to be "executed"

Is there some solution to prevent multi-line paste entirely?

Martin Vegter
  • 358
  • 75
  • 236
  • 411

2 Answers2

9

Bash now offers the enable-bracketed-paste option:

enable-bracketed-paste
When set to ‘On’, Readline will configure the terminal in a way that will enable it to insert each paste into the editing buffer as a single string of characters, instead of treating each character as if it had been read from the keyboard. This can prevent pasted characters from being interpreted as editing commands. The default is ‘off’.

So add this to ~/.inputrc:

set enable-bracketed-paste on
Quasímodo
  • 18,865
  • 4
  • 36
  • 73
5

This answer is not the most-specific for the user's question Please see my 2nd answer. I am leaving this here because it addresses the more general issue.

Per the comments to your original post, you need (1) a terminal emulator which supports bracketed paste and (2) corresponding support for whatever is running in the terminal, ie, vim, bash, zsh. Terminal emulators supporting bracketed paste (list to be updated based on comments to this post):

  • xterm - since ??
  • gnome-terminal - since ??
  • putty - since 0.63 (2013-08-06 release date)

Applications supporting bracketed mode:

  • vim
  • zsh

For bash, StéphaneChazelas has put together a shell script to facilitate the detection of that mode and take appropriate action.

A more specific answer -- preventing lines from getting chopped off -- is unanswerable without knowing about the unix program that is running when you hit paste.

A clipboard manager maybe what you need.

Otheus
  • 6,138
  • The script in there doesn't detect bracketed paste, it is a perl script that emulates a 3rd form of bracketed paste which doesn't require support in the application other than the application needs to understand ^V as litteral-next (that script would actually answer this question though would probably need improved, it was just a PoC). – Stéphane Chazelas May 12 '15 at 09:16
  • @StéphaneChazelas feel free to edit/improve my post. :) "The application needs to understand ^V as literal-next. But how does the ^V get into the input stream of the application? – Otheus May 12 '15 at 12:34
  • It does get it if the inner tty device is not in icanon mode (like bash readline's prompt will see it (and will treat is as literal-next)). If in icanon mode, the line discipline of that inner tty device will strip it (as long as the lnext character is ^V), just like when you type Ctrl-V X manually.. – Stéphane Chazelas May 12 '15 at 12:38
  • I just tried that in putty 0.62. It worked! I worship at your feet for a 3rd time. – Otheus May 12 '15 at 12:42
  • Sorry, misunderstood your question. how does the ^V get into the input stream of the application?. That's the whole point of that perl script wrapper. It creates a pseudo-tty device, and in that terminal it emulates, it inserts those ^Vs (\026 in the code) in front of the control characters it reads from the host terminal. – Stéphane Chazelas May 12 '15 at 12:54
  • I'm a little confused about what generates these ^Vs if not using your script. I saw them in bash under putty with -icanon. I did not see them in dash with putty & -icanon. So does inject them before the line reaches readline? If so, can readline be instructed to do something with those 2nd lines? If so, we have a better answer for the OP. – Otheus May 12 '15 at 13:09
  • 1
    If not using that script, nothing inserts ^V unless you use xterm's quoted paste mode (enabled by sending \e[?2005h). That perl script could be adapted so that it removes all but the first line in a multi-line paste (though not reliably for large pastes), but as it is already, since it quotes the newline for readline (as if you had pressed Ctrl-V Ctrl-J), it prevents the lines from being executed (they're just added to the editing buffer and you still need to press Enter to execute them). – Stéphane Chazelas May 12 '15 at 14:07
  • 5
    What second answer? – Xiao Sep 16 '15 at 00:53
  • Thanks for the hint about putty. I had to change .inputrc AND putty configuration: Window - Selection - Permit control charaters in pasted text – klaus thorn Jul 12 '23 at 09:30