Thorsten Zöller

Multiple-Click Selections in xterm

It is possible to configure in detail which portion of the screen – a word, a line, etc. – is selected in xterm(1) on rapid successive mouse clicks.

What is a Word in xterm, After All?

In xterm, words can be selected by double-clicking on any character in the respective word. This will highlight the word, and it is then in the primary selection of the X Window System and can be pasted elsewhere.

Characters are grouped into character classes. Words are defined in xterm as strings of characters belonging to the same character class. Therefore, what xterm interprets as a word depends on the mapping of characters to character classes.

In the default mapping, alphanumeric characters and the underscore belong to one character class, space and tab belong to another character class, and all other characters have their own character class. This definition works well when dealing mostly with prosaic texts, since it more or less corresponds to the usual definition of a word (i.e. certain characters separated by whitespace). In other cases, it is less convenient though. For instance, it is not possible to select a path consisting of several directories by double-clicking on it, since directories are separated by a slash, which belongs to a different character class than alphanumeric characters and the underscore.

Interestingly, the mapping can be changed via a certain xterm resource called charClass. Its value is a list of key-value pairs, where the key is either a single character code or a range of character codes, and the value is a character class. A given key-value pair then defines that the character(s) specified by the key belong to the character class specified by the value.

Character classes are identified by numbers (which in turn correspond to the character code of a representative of that character class). The default mapping of characters to character classes is given in the respective section of the manpage. In the default mapping, alphanumeric characters and the underscore belong to character class 48 (the character code of the character 0), and space and tab belong to character class 32 (the character class of space).

Therefore, in order to add e.g. the characters ! (character code 33) and : (character code 58) to character class 48, add the following line to ~/.Xresources:

xterm*charClass: 33:48,58:48

To add all “regular” characters to character class 48, add the following line to ~/.Xresources:

xterm*charClass: 33-126:48

Activate the change:

$ xrdb -merge ~/.Xresources

xterm Knows More Than Just Words

Words are just one kind of character grouping in xterm. The full list is:

word
As defined above.
line
The entire line to which the character under the pointer belongs to.
group
Basically the paragraph the character under the pointer belongs to, i.e. all lines above and below up to the next blank line. Does not extend outside the current page.
page
All visible lines on the current page.
all
All lines, including i.e. saved lines.

Even that is not all there is, though. On top of that, it is even possible to define custom groupings via regular expressions (basically, all other predefined groupings are just shortcuts; they could just as well be defined via a corresponding regular expression). Also, there is the pseudo-grouping none which specifies that no selection should occur in the respective case.

So it is possible to specify in a very fine-grained way what will be in the primary selection when double-clicking on some character. But it can be specified just as well what happens when clicking multiple (i.e. more than two) times in rapid succession. This is useful since one may be interested in selecting different character groupings depending on the respective case. For instance, in the default configuration, triple-clicking usually selects the entire line. Which character grouping is selected when clicking 2, 3, 4 and 5 times in rapid succession is specified by assigning one of the character groupings defined above to the xterm resources on2Clicks, on3Clicks, on4Clicks and on5Clicks, respectively.

As an example, in order to specify that double-clicking should select words defined as a string consisting of alphanumeric characters, the underscore, the exclamation mark and the colon, that triple-clicking should select words defined as a string consisting of all regular characters except whitespace (defined via a corresponding regular expression), and that clicking four times in rapid succession should select the entire line, add the following lines to ~/.Xresources:

xterm*charClass: 33:48,58:48
xterm*on2Clicks: word
xterm*on3Clicks: regex [^[:space:]]+
xterm*on4Clicks: line

Even when using the mouse sparingly, it is very convenient to be able to specify what gets selected on multiple clicks in xterm, and it certainly does save some time.