Smokes your problems, coughs fresh air.

Copy-pasting to and from XTerms

By default, XTerms only supports the PRIMARY selection for copy and paste. The PRIMARY selection is the one that is used by most ‘modern’ X application when you select text. This text can then usually be pasted by clicking the middle/second mouse button. Because this selection is set whenever you select, it’s easily overwritten, often accidentally. That’s why most newer X apps offer a parallel copy/paste mechanism where the selection is only explicitly set by choosing “Cut” or “Copy” from the application’s “Edit” menu or from its context menu (or with the Control-X/C/V keyboard shortcuts). In X, this selection is called CLIPBOARD, just like in Windows where it’s the only selection.

X also has these selections called Cut Buffers, but these are considered obsolete. Maybe that’s just too bad, because they appear to be the only selections with persistence; the other selections, PRIMARY and CLIPBOARD, disappear when the application is quit. Apparently, this is a feature, because it enables content negotiation.

Anyway, XTerm can be configured to do anything and I want it to be CLIPBOARD aware, for two reasons: one is that I often copy things from applications that can only set the CLIPBOARD selection; another is that I want to be able to really copy something from the XTerm, without losing it as soon as I select something else (especially if that something else is something I want to replace with the selection).

So, I added the following to my VT100.Translations #override in .Xdefaults.XTerm:

XTerm*VT100.Translations: #override \
    ShiftInsert: insert-selection(CLIPBOARD) \n\
    Insert: insert-selection(PRIMARY) \n\
    Shift: insert-selection(CLIPBOARD) \n\
    Shift: select-start() \n\
    Shift: select-extend() \n\
    Shift: select-end(CLIPBOARD)

What this does:

  • This gives me an XTerm that pastes the content of the CLIPBOARD when I hold down Shift during my two-finger tab (I’m using a touchpad, so that works as a Btn2).
  • Because I don’t like moving my hands, I can do the same by pressing Shift+Insert.
  • I can use my Insert key without Shift to paste the PRIMARY selection.
  • To copy something to the clipboard, I hold down the Shift key while making the selection.

That’s how you can make use of the CLIPBOARD from your XTerm.


If you want to learn more.

1 Comment

  1. Peter Danenberg

    It took me a couple hours to realize that your code is broken because angle-brackets are not being escaped; what you meant, presumably, is something like this (with proper indentation):

    XTerm*VT100.Translations: #override \
    Shift<Key>Insert: insert-selection(CLIPBOARD) \n\
    <Key>Insert: insert-selection(PRIMARY) \n\
    Shift<Btn2Up>: insert-selection(CLIPBOARD) \n\
    Shift<Btn1Down>: select-start() \n\
    Shift<Btn1Motion>: select-extend() \n\
    Shift<Btn1Up>: select-end(CLIPBOARD)

© 2024 BigSmoke

Theme by Anders NorenUp ↑