48

I have noticed that when pasting into a terminal emulator, both on macOS (using Terminal.app or iTerm2.app) or in Linux (using urxvt) sometimes I get extra characters.

The characters are always the same - before the pasted text I get ^[[200~ (with the ^[ highlighted) and after the pasted text an extra ~. If I cancel and paste again then it is pasted correctly.

For example here I tried to paste git clone https://git.qemu.org/git/qemu.git copied from their webpage into iTerm2 with with v. I cancelled with ctrlc and (without recopying) pasted again with v and it was OK.

screenshot

This is certainly not always but several times a day and I've not managed to narrow down what causes it and can't reliably recreate it.

  • the text can come from anywhere (website, text document, copied from the terminal itself).
  • position of cursor when I copy doesn't seem to make a difference (I don't think I'm copying the cursor)
  • position of mouse doesn't seem to make a difference.

What are these ^[[200~ ~ characters and how do I get rid of them? Surely they must stand for something specific as they are always the same and appear on both macOS and Linux.

5 Answers 5

44

These characters are used for bracketed paste mode. Some terminal-based programs enable this mode so that they could distinguish pasted text from directly typed text.

For example, text editors temporarily disable auto-indent for pasted text, and CLI shells might allow you to review/confirm the pasted commands before running them (even if they end with a newline). In zsh, all pasted commands get a reverse highlight and aren't immediately run until you press Enter.

The "brackets" follow the usual format of "special key" escape sequences (ESC [ <num> ~), the same as with PgUp/PgDn keys or the F4–F12 function keys. So from your shell's or editor's point of view they're also treated as key bindings. (For example, running bindkey should show the binding "^[[200~" bracketed-paste.) For more information, search for 'bracketed-paste' in the zshzle(1) manual.

As with all other escape sequences, if you have (accidentally) pressed Ctrl+V immediately before pasting (or before pressing a special key), it will cause zsh to treat the following sequence as literal input. For example, Ctrl+V ↑Up will not scroll through your history – it will literally insert the ESC [ A sequence instead. To avoid this, you can unbind the Ctrl+V key through zsh's bindkey (where it is shown as "^V" vi-quoted-insert by default).

Also, like most other terminal emulation modes, the bracketed paste mode is enabled by sending an escape sequence to the terminal emulator app – so if the program using it dies unexpectedly, it might not have any chance to disable the mode. If this happens, running printf "\e[?2004l" will deactivate the mode for now.

(Your terminal emulator is completely unaware of programs starting and exiting (even more so if the app was running remotely over SSH), so there's no way for it to disable the mode automatically when a program dies. The same problem occurs if e.g. the program had "mouse reporting" mode enabled.)

3
  • I assume the Ctrl+V thing is as much of an issue on Mac keyboards (where Cmd+V pastes) as it is on Linux (where Ctrl+Shift+V pastes)... Commented Mar 14, 2020 at 14:27
  • 1
    That was it - mystery solved! I'm more used to Shift+Ctrl+v to insert. If I'm on macOS I must do it out of habit (and zsh seems to treat Shift+Ctrl+v as Ctrl+V on macOS) . It appears to do nothing (but my bindkey says "^V" quoted-insert) so I remember, do cmd+V and get the bracketed paste data. I'll unbind "^V" as I can't imagine ever wanting it.
    – lx07
    Commented Mar 14, 2020 at 14:43
  • You could probably rebind "^V" to some zsh function that grabs text from clipboard – that would solve two problems at once. (I've done so in bash to create a "paste quoted" key.) Commented Mar 14, 2020 at 14:53
11

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.

Consult the man page for readline for more information on this parameter and the ~/.inputrc file since you can change the file's location with the INPUTRC environment variable

3
  • 2
    Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
    – Community Bot
    Commented May 17, 2022 at 13:43
  • I have no ~/.inputrc under Ubuntu-MATE 22.04 – any other place to look for it. (Even after said set enable-bracketed-paste Off)
    – Frank N
    Commented Jan 31, 2023 at 15:54
  • Dis this work for commands other than the shell (or other readline-using stuff) as well? (e.g. pasting into cat) Commented Feb 7, 2023 at 8:52
4

This seems to work for me (though it doesn't make sense)

In my ubuntu Mate Terminal I go Edit > Keyboard Shortcuts goto

Paste - the keys are already mapped to Shift+Ctrl+V

I select and re-enter by pressing exact sames buttons - and click close - now the squiggles don't appear anymore when pasting. ¯_(ツ)_/¯

1
  • Well, I had ctrl-shift-V there (aka the nice plain-text-paste-shortcut I hardly habitually use), now I have ctrl-V there. Perhaps that makes a difference (for the moment it seems to work). (Ubuntu-MATE 22.04.1)
    – Frank N
    Commented Jan 31, 2023 at 15:49
1

To stop this on Ubuntu 22.04, I added this line to ~/.inputrc:

"\C-v": ""

For me, my clipboard manager (Diodon) would always send a Ctrl+V to the terminal, after which I would use the Ctrl+Shift+V which would result in the extra characters.

2
  • 1
    You could make your answer better by explaining why this works Commented Dec 20, 2022 at 21:31
  • 1
    Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
    – Community Bot
    Commented Dec 20, 2022 at 21:31
0

Another solution under Windows is to open a new terminal. I was not able to exit the bracketed paste mode with the accepted solution of "printf "\e[?2004l"".

2
  • 1
    As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.
    – Community Bot
    Commented Nov 2, 2022 at 16:24
  • I had the same issue in Microsoft Terminal when SSHing into a linux shell where I connected to Oracle with sqlplus. Everything was fine in my local shell and in the Linux shell, but sqlplus could not paste. Switching to a different tab in Microsoft Terminal solved it.
    – Blaisem
    Commented Aug 28, 2023 at 21:17

You must log in to answer this question.

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