177

I use xubuntu 14.04, 64 bit. Every now and then, when I try to paste some text in xfce4-terminal, instead of the expected text to be pasted, it is surrounded by 0~ and 1~, such as:

0~mvn clean install1~

The text is supposed to be mvn clean install -- I verified this by pasting the content in various other applications (gnome-terminal, gedit and others). Every application pastes correctly the content, except xfce4-terminal. I couldn't find any references for this on the internet (unfortunately, it is hard to search for text with special characters on google.com...). Why does this happen?

Michael Mrozek
  • 93,103
  • 40
  • 240
  • 233
botismarius
  • 1,929
  • How are you pasting? Middle click, or something else? – Celada Apr 15 '15 at 00:02
  • 3
    I've tried every method: middle click, right click and chose option paste, ctrl+shift+v. When the 0~ and 1~ characters appear, it does not matter which pasting method I use. – botismarius Apr 15 '15 at 10:38

6 Answers6

240

The issue is that your terminal is in bracketed paste mode, but doesn’t seem to support it properly. The issue was fixed in VTE, but xfce4-terminal is still using an old and unmaintained version of it.

You can try temporarily turning bracketed paste mode off by using:

printf "\e[?2004l"
remmy
  • 5,055
  • 1
  • 24
  • 30
  • Thanks for the answer! I will test this as soon as the issue reproduces (it may take a day or two) – botismarius Apr 16 '15 at 13:41
  • 4
    If you can’t always reproduce it it might be that you were running some program that enabled bracketed paste mode and then it crashed, thus wasn’t able to disable it before exiting. – remmy Apr 16 '15 at 13:46
  • 6
    Awesome, thanks! I've been struggling with this issue for more than a year. Is it possible to disable this "bracketed paste mode" completely? BTW, who implements such crazy things? ;) – Adam Romanek Apr 13 '16 at 13:00
  • 4
    it depends on your terminal. And it is a really useful feature, since it lets applications know whether something was typed in or pasted. For one, weechat, an IRC program, uses it to ask if you want to send multi-line pastes, which it couldn't do without bracketed paste mode, since then there would be no way to know if the user typed the lines or not. – remmy May 04 '16 at 10:42
  • 1
    How can I find which program is messing with bracketed paste mode? – aviggiano Jul 12 '17 at 16:49
  • 3
    For distributions based on Ubuntu you can try to install patched version of 'vte' package from https://launchpad.net/~adrozdoff/+archive/ubuntu/vte – Denis Bakharev Oct 21 '17 at 21:22
  • 3
    Great solution, but when I tried to run 0~printf "\e[?2004l"1~, it told me -bash: 0~printf: command not found. ;) – CynicallyNaive Oct 08 '20 at 16:20
  • @CynicallyNaive You need to disable bracketed paste mode to copy and paste the printf command correctly. Before that, it will surely add those annoying 0~ and 1~ in the pasted text. – jdhao Feb 01 '21 at 07:53
  • I definitely encounter this ( undesired ) behavior, at least in XTerm 322 when I have a remote .. i.e. ssh process running, and I have "escaped" back to the parent shell. So the "remote" session has no such problem, but every time I escape to the "local" session it now exhibits the bracketed paste behavior ( whenever I paste from the clipboard ). I tried the printf "\e[?2004l" from remmy, above and that is working nicely ! Thanks. – LarryC Aug 02 '23 at 02:04
  • BTW - ( adding to my last comment ) every time I resume my remote shell, and escape back to local again ... the bracketed mode behavior comes back. – LarryC Aug 02 '23 at 02:12
144

My issue was due to a badly-closed bash session. So running command reset in my terminal solved it immediately.

petobens
  • 1,540
  • For me, this does not work. printf "\e[?2004l" works. Terminal: zoc terminal, system: CentOS. – jdhao Feb 01 '21 at 06:30
  • This problem happened to me inside of a tmux session. I had to detach from tmux first (CTRL+B D) and run reset from the regular session. This fixed the problem even in the tmux session. – John Muraguri Jul 03 '22 at 09:59
33

I had the same issue, it occurs after I use vi.

Mine was resolved by adding set t_BE= to my .vimrc file.

For reference, this is what t_BE does:

t_BE enable bracketed paste mode

ivan
  • 411
12

This behavior occurs when the terminal emulator is stuck in bracketed paste mode.

This typically happens when an application exits uncleanly (crashes or is buggy).

Another reason for the phenomenon to occur frequently is a bug in older versions of VTE (VTE is the terminal emulation widget under gnome-terminal and xfce4-terminal and many more) which is triggered by certain actions in certain utilities such as toggling the panels in Midnight Commander.

To see if this bug is fixed in the VTE-based terminal emulator you're running: Execute echo -ne '\e[>c', this will insert a few characters as if you typed them. If the middle number is 3602 or bigger, the aforementioned old VTE bug is no longer present for you. If the number is smaller than that, VTE is still affected and gets stuck in bracketed paste mode more often than it should.

To see if a certain terminal emulator is fixed in a certain Ubuntu distribution, check its dependencies on packages.ubuntu.com. libvte-2.91-0 is good. libvte-2.90-9 is only good if the actual version of that package is 0.36.2 or newer, and libvte9 is bad. In particular, xfce4-terminal is buggy in Xenial, and fixed in Zesty.

egmont
  • 5,866
1

This may not apply directly to your problem, but I found this symptom to probably, in my case, be caused by my editor-of-choice 'mcedit' (Midnight Commander)

To alleviate the bug problem, I added the following function to my .bashrc file:

### vvv 'function mcedit' is a fix-up for the ~0/~1 paste problem
function mcedit() { command mcedit $@ ; printf '\e[?2004l' ; }

Then 'source .bashrc'

Now every time I execute 'mcedit', it automatically adds the 'printf "\e[?2004l"' when I close out to reset the "Bracketed Paste Mode"

Works for me, YMMV.

The same approach can be used with vi, vim, nano, or any other misbehaving program.

0

There is an additional way that this problem can manifest. Readline's .inputrc file can contain the following:

set enable-bracketed-paste On

This is not fixed by any of the above methods and has eluded me for months. Removing this line or setting

set enable-bracketed-paste Off

will fix this nicely.

AdminBee
  • 22,803
DanG
  • 1