Re: [dev] st: bracketed paste mode

From: Egmont Koblinger <egmont_AT_gmail.com>
Date: Thu, 19 Sep 2013 17:48:09 +0200

On Thu, Sep 19, 2013 at 4:51 PM, Nick <suckless-dev_AT_njw.me.uk> wrote:
> To check, how does this work exactly? Does X send the escape code to
> any window when pasting with middle click

X doesn't do anything special. It's st that changes its behavior and
does some extra stuff.

There's no change whatsoever until someone outputs '\e[?2004h'. Until
this moment, everything will work as it worked before. It is expected
that if an application prints this escape code then it knows what'll
happen :) So if none of the applications you use use this extension
then there won't be any change.

When some app (let's say, vim) outputs this code, the terminal changes
its behavior slightly. X is not directly involved in this, the way X
handles it selection/clipboard remains unchanged. It's the terminal
application (st, xterm, ...) that does something tricky whenever you
try to paste. It encloses the pasted text by \e[200~ and \e[201~.
E.g. it asks the X server for the contents of the clipboard, X says
the clipboard contains "bar", so then st sends "\e[200~bar\e[201~" to
vim, rather than simply just "bar".

This way vim can tell that "bar" was not typed from the clipboard, but
rather pasted by some means (whether it's mouse middle click, or
shift-insert, or anything else is irrelevant).

Vim (or any other text editor) will probably disable auto indentation
and line wrapping on seeing \e[200~, and revert to the previous
settings on \e[201~.

How to do low-level testing:

Start a "cat" command. Type "foo" from the keyboard, then copy-paste
"bar" from somewhere else. You'll end up seeing "foobar".

Now execute
echo -e '\e[?2004h'
and repeat the previous step. If your terminal doesn't support this
feature, you'll end up seeing the same behavior. But if your terminal
supports bracketed paste, you'll see "foo^[[200~bar^[[201~".

How to do high-level testing:

Edit some source code (function definition, etc.) in vim, with
autoindent enabled. Paste some function definition from another file.
 You'll see staircase effect like this:
int blah (int xy) {
    int a;
        int b;
            int c;
                [and so on...]
                }

Get a terminal that supports bracketed paste (e.g. st with the
attached patch), and configure vim according to the stackoverflow
thread linked above. Take care that that one looks for $TERM=xterm,
adjust it properly.

Then paste the same function into vim, and notice that it will be
indented properly, without the staircase effect.

> and those which don't understand it just ignore it?

Those which don't understand won't receive these \e[200~ and \e[201~
brackets because they don't enable the 2004 mode.

So the "those which don't understand it just ignore it" works the
other way than you think: this sentence holds for terminals. st
currently doesn't understand the 2004 escape hence ignores it.

> And then once st has done the
> appropriate stuff with the pasted text, vim (for example) will
> detect that and behave as though :paste is enabled for the duration
> of the paste?

Exactly.


cheers,
egmont
Received on Thu Sep 19 2013 - 17:48:09 CEST

This archive was generated by hypermail 2.3.0 : Thu Sep 19 2013 - 18:00:10 CEST