29

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

27

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, 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
    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.)
    – user1686
    Mar 14, 2020 at 14:53
3

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. ¯_(ツ)_/¯

3

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

1
  • 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
    May 17, 2022 at 13:43
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"".

1
  • 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
    Nov 2, 2022 at 16:24
0

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
  • You could make your answer better by explaining why this works Dec 20, 2022 at 21:31
  • 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
    Dec 20, 2022 at 21:31

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.