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?

9
  • 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, 2015 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, 2015 at 12:22
  • 3
    Relevant: security.stackexchange.com/q/39118 May 11, 2015 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). May 11, 2015 at 13:44

2 Answers 2

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
2
  • 1
    Note that it was actually mentioned at the link given in the accepted answer. Jul 28, 2020 at 5: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. Jul 28, 2020 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.

9
  • 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). May 12, 2015 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, 2015 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.. May 12, 2015 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). May 12, 2015 at 14:07
  • 5
    What second answer?
    – Xiao
    Sep 16, 2015 at 0:53

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .