Why bracketed paste mode is enabled sporadically in my terminal screen?

I use Ubuntu 14.04 and I have a weird issue with my terminal screen which is bugging me a lot. Could someone help me with it or explain me if I'm doing something wrong or non linux-way? I have some sort of a solution but I want understand why this happens again and again.

I often copy bash commands from my notes or from the Internet and sometimes I get weird 0~ and 1~ symbols which wraps things I copied. It is very annoying and this happens in a totally random fashion.

After a long search I found out that this thing is called bracked paste mode so now I use this command printf "\e[?2004l" to fix my terminal if this mode got suddenly enabled.

Is it possible to disable this feature permanently somehow? I faced with it on all Ubuntu machines I work now. Previously I used Ubuntu 10.10 and 12.04 and I've never had such issue before.


Solution 1:

You can disable bracketed paste mode.

To try it temporarily, in bash:

bind 'set enable-bracketed-paste off'

Then, if you like how that behaves, you can put the setting in your ~/.inputrc, or system-wide at /etc/inputrc (or wherever it is on Ubuntu).

Solution 2:

You can put that command in your bashrc. Then it would apply every time you open your terminal.

Just type vi ~/.bashrc and add printf "\e[?2004l" at the end and save the file with :wq

Solution 3:

To answer your original question of why this happens, here's a possible scenario:

  • My home computer had a new version of zsh that supported bracketed paste (let's call it shell A)
  • I sshed into a computer with my shell set to an older version of bash, that doesn't support bracketed paste (shell B)

Problem is, my terminal program still thinks bracketed paste is enabled when sshing from shell A to shell B, so it keeps adding the characters around your pasted content (the 0~ and 1~ bits). Shell B doesn't support them so it just passes them through unchanged. You have to tell your terminal to turn off bracketed paste by having your shell print a special escape sequence - which is what printf "\e[?2004l" does.

There are a few ways to solve the issue:

  1. If you don't care about bracketed paste at all, turn it off on shell A so it's never enabled in the first place (@jwd's answer)

  2. If you want to keep using bracketed paste on shell A, but disable on shell B, add the escape sequence to your .bashrc (@MOHRE's answer)

  3. Upgrade shell B to support bracketed paste, so it properly interprets those 0~ and 1~ characters.

Side note: if you are using GNU screen, you need to run that printf command outside of the screen. It doesn't seem to work while inside.

Solution 4:

I resolved it by adding the following content to my ~/.bashrc file:

if [[ $- == *i* ]]; then
    bind 'set enable-bracketed-paste off'
fi

In that way i dont get bind warnings when i execute bash scripts.