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?

Gilles 'SO- stop being evil'
  • 807,993
  • 194
  • 1,674
  • 2,175
Martin Vegter
  • 69
  • 66
  • 195
  • 326
  • paste into notepad first, to see what you have, then if it is OK go ahead to paste it into your application – Skaperen May 11 '15 at 12:17
  • 2
    in bash run the command `sleep 999999` then do the paste if it looks ok. the sleep command does not read the lines and they are discarded when you press `ctrl+c`. – Skaperen May 11 '15 at 12:22
  • 3
    Relevant: http://security.stackexchange.com/q/39118 – Stéphane Chazelas May 11 '15 at 12:27
  • 3
    [Next version of `bash` will have support for _bracketed paste_](http://thread.gmane.org/gmane.comp.shells.bash.bugs/22480), `zsh` already has [via 3rd party plugin](https://github.com/robbyrussell/oh-my-zsh/pull/1698). – Stéphane Chazelas May 11 '15 at 12:32
  • Seems that the terminal emulator itself could/should handle this, not the shell. – Otheus May 11 '15 at 13:13
  • @Otheus, the terminal emulator does. That's what the bracketed paste modes are all about. See [my answer to the security.SE Q&A I mention above](http://security.stackexchange.com/a/52655) – Stéphane Chazelas May 11 '15 at 13:19
  • I don't understand your post in that thread, @StéphaneChazelas. It certainly looks brilliant -- the shell scripting is amazing. I just don't get what you're trying to achieve nor how. It also looked like it was dependent on the user running xterm or other X-based terminal. I suppose the idea is that the terminal detects a paste and precedes the content with a "bracket" -- some sort of escape sequence -- that you trap with the WNCH handler and take appropriate action. – Otheus May 11 '15 at 13:30
  • Oh look! Putty supports "bracketed paste mode" since 0.63 (2013). http://www.chiark.greenend.org.uk/~sgtatham/putty/changes.html Oh look! I'm using 0.62! :/ – Otheus May 11 '15 at 13:42
  • 1
    @Otheus, `printf '\e[?2004h'` in most xterm-like terminal emulators enables a bracketed-paste mode where the terminal brackets the pasted text so that applications can detect where the paste start and where it ends. xterm also support another mode with `\e[?2005h` where all pasted characters are prefixed with `^V` (understood by many applications and the tty line discipline as `litteral-next`). – Stéphane Chazelas May 11 '15 at 13:44

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,625
  • 3
  • 35
  • 72
  • 1
    Note that it was actually mentioned at [the link](https://security.stackexchange.com/a/52655/76262) given in the accepted answer. – Stéphane Chazelas Jul 28 '20 at 05:52
  • 1
    No, it's still useful. The accepted answer itself doesn't mention it itself (at the time it was written bash4.4 was probably not out yet). I updated the linked page with the bash information later on. – Stéphane Chazelas Jul 28 '20 at 11:20
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
  • 5,945
  • 1
  • 22
  • 53
  • The script in [there](http://security.stackexchange.com/a/52655/76262) 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