142

I'm using iterm2 v 3.0.15 on Mac Sierra. I'm having trouble cutting and pasting from other places (e.g. text on a web page) into my terminal. For instance, I highlight some text from a web page, press Command + C, go to the terminal, and then press Command + V, I get

localhost:myproject nataliab$ 00~on the next01~

Notice the "00~" and "01~" in front of and behind my text. I do not see these characters when I cut and paste into a text editor or other places. How do I eliminate these special characters from appearing when I cut and paste into my iterm2 terminal?

dbenhur
  • 20,008
  • 4
  • 48
  • 45

12 Answers12

221

In the terminal window type reset, and press enter. And now, try pasting contents in terminal, it should work properly.

Arjun Sankarlal
  • 2,655
  • 1
  • 9
  • 18
  • 24
    This is simple but powerful answer. By the way, `⌘+R` is shortcut for `reset` command in iTerm2, so just press `⌘+R`. – kangbu Jan 07 '20 at 01:16
  • 17
    This solution does help, but any idea what caused this problem? – Vivek Oct 18 '21 at 10:27
  • 3
    I can confirm that this works, but how come? – Player1 Oct 22 '21 at 08:45
  • this is the only working solution. thanks a lot – Mgreg Apr 21 '22 at 07:23
  • 1
    Also curious as to why this worked, but it did! thanks – Jessica Feb 06 '23 at 21:27
  • 1
    When I most recently got this behavior, it was after I hit ^C during tab completion in Fish, which seems to use some fancy color and positioning stuff. Presumably this left the terminal in an unusual mode, but `reset` / ⌘-R brought it back to its default mode. – amacleod Jul 07 '23 at 15:06
  • 1
    @amacleod Even I got this problem and the terminal showed a message on the banner which says something like `'Unexpected exit has left the paste bracketing turned on. Do you want to switch it off'` I opted yes and the issue is gone. I believe `reset` do the same trick to bring back stability – SrinR Aug 23 '23 at 05:38
61

To permanently disable bracketing paste mode, open the Preference, search "bracketing", uncheck "Terminal may enable paste bracketing"

Jianwu Chen
  • 5,336
  • 3
  • 30
  • 35
58

Solution in my case:

Paste printf '\e[?2004l' to the iTerm2 terminal and press enter afterwards.

Community
  • 1
  • 1
whiletrue
  • 10,500
  • 6
  • 27
  • 47
  • 1
    Is this the kind of thing that can be added to `PS1` somehow in such a way that you guarantee that you're always in bracketed paste mode? – jdotjdot Mar 20 '19 at 20:57
  • 17
    Can you explain what exactly this does, and why it fixes the problem? Helping people understand the issue is crucial to learning, just giving us the answer helps in the short term but doesn't increase the asker's understanding. – Bryson Jun 25 '19 at 16:34
  • 5
    While yes, this fixes the problem, what is especially amusing is that this cannot be pasted into Mac's terminal; you have to type it over. It blows my mind that standard keyboard input doesn't work on Mac in 2019. – Paul Lammertsma Jul 18 '19 at 20:50
  • @PaulLammertsma yes it can be pasted both into iTerm2 and Terminal – whiletrue Jul 19 '19 at 09:24
  • @Bryson see jdotjdot's answer, which I believe is the most reasonable to your question – whiletrue Jul 19 '19 at 09:24
  • After you paste this in iTerm don't forget to remove 00~ from beginning and 01~ from the end manually :) – Bakul G Oct 24 '19 at 18:44
  • 11
    This command disables "bracketed paste mode". See [explanation](https://github.com/ChrisJohnsen/tmux-MacOSX-pasteboard/issues/31#issuecomment-55966569). See [other answer](https://stackoverflow.com/a/50454690/39396). – Carl G Nov 05 '19 at 19:09
14

I think it's a bracketed paste issue

  1. Edit -> Paste special -> Advanced Paste

  2. Uncheck "Bracketed paste mode"

  3. Restart the terminal

user3429211
  • 141
  • 1
  • 2
12

I had the same issue, and was unable to figure out why the problem was happening, but closing iTerm2 with Command + Q and reopening it fixed the problem.

Robert Sigler
  • 129
  • 1
  • 4
  • 3
    That's a big hammer. You can fix it with `printf '\e[?2004h'`. – dbenhur May 21 '18 at 18:32
  • 2
    @dbenhur, your comment is also a big hammer. Where to put your suggested command? – whiletrue Aug 09 '18 at 10:19
  • 2
    @Fábio, ...eh? "Big hammer" colloquially means you're fixing something using too much force (and might be breaking other things at the same time). I don't see how that applies to dbenhur's comment, which is much more focused and nuanced. – Charles Duffy Jul 24 '19 at 21:59
7

I recently started encountering this behavior in iterm2 as well. Killing and restarting a session fixes it, but loses context. I found this comment about bracketed paste mode and tried printf '\e[?2004h' to exit bracketed paste mode and repair the terminal and it worked well for me.

dbenhur
  • 20,008
  • 4
  • 48
  • 45
7

$ printf '\e[?2004l'

It solved my issue. but the funny part is when i pasted the above command, it also pasted as below

00~printf '\e[?2004l'01~

Ram Patro
  • 121
  • 4
  • 10
3

Adding set t_BE= to .vimrc to disable bracketed paste mode should fix this. you may refer: https://gitlab.com/gnachman/iterm2/issues/5698

Kun Zhao
  • 31
  • 1
2

I can't guarantee the conditions of this fix because I had been fiddling for a while with the solutions above, but what fixed the issue for me was to go to:

iTerm2 → Preferences → Keys → double-clic to edit the target of ⌘v → Under Speed, set Chuck Size to 256B and Interchunck delay to 100ms.

n.caillou
  • 1,263
  • 11
  • 15
  • 1
    This worked for me. I set iTerm2 -> Preferences -> Advanced/Pasteboard -> "Number of bytes to paste in each chunk when pasting normally" to 256 (a fresh install of iterm2 v. 3.3.9 had that value set to 667). I upped the value for '...when pasting slowly' to 64 from 16. I did not disable bracketed paste mode (wasn't haven't the 00, 01 append issues). My original issue was pasting from vsCode to iTerm2 running python 3 (on OSX 10.15.4) was including bizarre partial and multiple duplication of the pasted chunk. Anyway, this appears to have solved. Will update if that behavior returns. Thanks! – Connor Dibble Apr 21 '20 at 22:55
1

I had to do shell-->Hard Reset to make it work immediately

logan
  • 7,946
  • 36
  • 114
  • 185
0

In my case, the issue was that I was pasting a string with an em dash (sometimes called long dash, m-rule or mutton, this one: "—"), and it was messing everything up. Try pasting "ciao — amore" to your iTerm.

sekmo
  • 1,517
  • 18
  • 27
-1

printf '\e[?2004h' should solve the issue; also make sure to remove the trailing and starting junk characters.

M--
  • 25,431
  • 8
  • 61
  • 93
Sahil
  • 75
  • 2
  • 9