23

Under different Unix/Linux systems I've observed different double click behavior in X terminal applications (e.g. xterm).

Sometimes a double click selects everything left and right until the next non-alphabetic character (e.g. it selects the word under the cursor).

Sometimes everything until the next blank/eol is selected (e.g. full paths under the cursor are selected).

How can I configure the double click behavior - say - in xterm (because it is available on most systems)?

Currently, I find the 2nd mode more convenient for most use cases.

maxschlepzig
  • 57,532

2 Answers2

27

You do it with X resources. I have a file, .Xresources, that contains these xterm-related resources:

XTerm*VT100.cutNewLine: false
XTerm*VT100.cutToBeginningOfLine: false
XTerm*VT100.charClass: 33:48,35:48,37:48,42:48,45-47:48,64:48,95:48,126:48

In my .xinitrc file, I have some line that merge in those resources:

if [ -f $userresources ]; then
    /usr/X11/bin/xrdb -merge $userresources
fi

Those lines make xterm double-clicks and triple-clicks do what I like:

Double-click considers a "word" to include slash (/), dot (.), asterisk (*) and some other non-alphanumeric characters. That's the "charClass" resource. I had to do some tedious fiddling with that charClass to get it to do what I want. That mostly lets you double-click on URLs and fully- or partially-qualified paths to highlight them.

The other two lines make triple-click start from the word under the mouse, and go to the end of the line, but not include any new-line. That way, you can triple click on a command you just executed, paste it in another window, and because it has no new-line, you can edit it before running it in the other window.

The Arch Wiki has an article on X resources, including a section on xterm resources, but those xterm resources aren't complete.

  • 2
    Wikis can be useful, but the place to consult for xterm resources is its manual page. – Thomas Dickey Mar 24 '16 at 00:39
  • Or use XTerm*VT100.charClass: 0-32:0,33-65535:1. Double-click will then consider a "word" to be any sequence of symbols excluding whitespace (and control characters). – Newtonx Feb 28 '18 at 05:04
  • The whitespace rule is no good if you want to get what's inside quotes, in a json oneliner for instance. But I guess it depends on the usage you have. The provided snippet in this answer is perfect for me. – Jean-Bernard Jansen May 09 '18 at 09:47
  • You've got a mistake: cutNewLine -> cutNewline. Maybe that was introduced in newer versions. – x-yuri Aug 18 '18 at 17:51
  • 1
    And onNClicks: regex seems at least more readable. – x-yuri Aug 18 '18 at 18:03
9

xterm has several resource-settings related to double- and triple-clicking for selection (the manual page lists all of the relevant resources):

cutNewline (class CutNewline)
If "false", triple clicking to select a line does not include the Newline at the end of the line. If "true", the Newline is selected. The default is "true".

cutToBeginningOfLine (class CutToBeginningOfLine)
If "false", triple clicking to select a line selects only from the current word forward. If "true", the entire line is selected. The default is "true".

on2Clicks (class On2Clicks)
on3Clicks (class On3Clicks)
Specify selection behavior in response to multiple mouse clicks. A single mouse click is always interpreted as described in the Selection Functions section (see POINTER USAGE). Multiple mouse clicks (using the button which activates the select-start action) are interpreted according to the resource values of on2Clicks, etc. The resource value can be one of these:

word
Select a "word" as determined by the charClass resource. See the CHARACTER CLASSES section.

line
Select a line (counting wrapping).

group
Select a group of adjacent lines (counting wrapping). The selection stops on a blank line, and does not extend outside the current page.

page
Select all visible lines, i.e., the page.

all
Select all lines, i.e., including the saved lines.

regex
Select a "word" as determined by the regular expression which follows in the resource value.

none
No selection action is associated with this resource. Xterm interprets it as the end of the list. For example, you may use it to disable triple (and higher) clicking by setting on3Clicks to "none".

The default values for on2Clicks and on3Clicks are "word" and "line", respectively. There is no default value for on4Clicks or on5Clicks, making those inactive. On startup, xterm determines the maximum number of clicks by the onXClicks resource values which are set.

Thomas Dickey
  • 76,765
  • 1
    What the manual doesn't tell you: For some distributions, the default is not the documented default. Setting an empty charClass resource restores the documented default. Whatever you specify for charClass overwrites only the the entries you specify; unspecified entries are restored to the documented default. At least, that's how it appears to work for me. – Jeff Learman Feb 08 '17 at 18:32
  • I've run into a post that suggest the following values: xterm*on2Clicks: regex [^/@ \n]+, xterm*on3Clicks: regex [^ \n]+, xterm*on4Clicks: regex [^#$]+, xterm*on5Clicks: line. Seems better and easier to understand than those in the accepted answer. – x-yuri Aug 18 '18 at 17:33
  • On second thought, for now I decided to get the best of both approaches (at the least the way I understand it now): 2 clicks for a word, 3 clicks for a WORD (in vim's sense, xterm*on3Clicks: regex [^ \n]+), 4 clicks for copying commands from shell (from current word to the end of the line, xterm*on4Clicks: line, xterm*cutNewline: false, xterm*cutToBeginningOfLine: false), 5 click for whole lines (xterm*on5Clicks: regex [^\n]+). – x-yuri Aug 18 '18 at 18:01