10

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?

  • 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
  • 1
    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
  • 2
    Relevant: security.stackexchange.com/q/39118 – Stéphane Chazelas May 11 '15 at 12:27
  • 3
  • 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
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.

  • 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 9: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
  • 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
  • 4
    What second answer? – Xiao Sep 16 '15 at 0:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.