Linux – Pasting ‘required text’ into terminal emulator results in ‘^[[200~required text~’

arch linuxcopy/pasteiterm2macos catalinaurxvt

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.

Best Answer

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.)

Related Question