Bracketed paste: Those ~0 and ~1 added around pasted text

This post was written by eli on July 11, 2024
Posted Under: Linux

Intro

This is a super-short post, but I have a feeling it will evolve with time.

Using ssh, pasting text with CTRL-V or the mouse’s middle button sometimes resulted in ~0 and ~1 around the pasted text. Super annoying.

As it turns out, this is called “bracketed paste” and its a way for the terminal application (say, Gnome terminal) to tell the receiver (say, bash) that the text is pasted, and not typed manually.

Why is this helpful? For example, if the text goes to an editor which responds with automatic indentation as a result of a newline, that can have a negative effect. Bracketed paste gives the editor to possibility to accept the text as is, assuming that it’s already correctly indented, since it’s pasted and not typed.

The reason for the ~0 and ~1 problem is probably that the bash version on the ssh’ed computer is really old, and my Gnome terminal is relatively new. So bash doesn’t understand the magic characters, and prints them out as they are.

This problem will probably go away by itself sooner or later.

Magic solution

There are all kinds of “bind” commands, but for some reason, I thought this solution was coolest.

Turning off bracketed paste, which adds ~0 and ~1 around the pasted text:

$ printf "\e[?2004l"

To re-enable bracketed paste:

$ printf "\e[?2004h"

These two commands were taken from this page. The effect of these commands seems to go beyond what one would expect. It seems like they don’t influence just the current terminal session, but I need to figure this out.

Add a Comment

required, use real name
required, will not be published
optional, your blog address