prevent multi-line paste in bash

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?

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

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

Method 2

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.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x