5

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.

8

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)... – user1686 Mar 14 '20 at 14:27
  • 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 Mar 14 '20 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.) – user1686 Mar 14 '20 at 14:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.