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

15,805

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.

Share:
15,805

Related videos on Youtube

artemdevel
Author by

artemdevel

Python Developer

Updated on September 18, 2022

Comments

  • artemdevel
    artemdevel over 1 year

    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.

  • Brian Leishman
    Brian Leishman about 7 years
    All this seems to do for me is make it so I cannot type the letter "p" (and only a lower case "p"), or paste it like at all, and nothing else
  • Lupen
    Lupen about 7 years
    @BrianLeishman Due to the peculiar way bind parses arguments it doesn't recognise, that's what will happen if you run this command using Readline before version 7 or Bash before 4.4. This answer won't do anything useful until the next release of Ubuntu (17.04).
  • artemdevel
    artemdevel almost 7 years
    Yes, I did this at first actually :) later I just disabled this mode as @jwd suggested.
  • Cloud
    Cloud almost 7 years
    The printf approach works, but it doesn't work when put in my .bashrc, and the feature somehow turns itself back on periodically. The bind approach breaks my keyboard (p key doesn't work, and putting that line in my ~/.inputrc` works, for 10 seconds, and then the console starts barfing out -enaset-enaset-... forever after 10 seconds of use. I finally just manually upgraded to bash 4.4 from source to fix the stupid problem.
  • IsaaC
    IsaaC almost 7 years
    The correct command is bind 'set enable-bracketed-paste off' (with an space instead of the first `-``. Editing only one character seems not possible.
  • artemdevel
    artemdevel over 6 years
    Thanks @rjh :) But I've never used zsh or had an issue with ssh as you described. Looks like something was just mis-configured on Ubuntu years ago.
  • pzkpfw
    pzkpfw over 5 years
    echo 'printf "\e[?2004l"' >> ~/.bashrc does the same thing in one line, and you can verify with tail -n1 ~/.bashrc. There's no need to use vi.
  • Dasmowenator
    Dasmowenator over 4 years
    Why is the bind part of this command necessary? Does just running set enable-bracketed-paste off do the same thing?
  • Kenji Noguchi
    Kenji Noguchi over 3 years
    zsh doesn't seem to understand bind command. How to disable in the zsh shell A?