Binding keys to actions in xterm, and my bindings as an example
One of the probably lesser known features of xterm is that it has a quite complete but obscure and tangled system of adding custom key bindings. Because xterm is a traditional X program, this is configured through X resources instead of a dotfile somewhere, and because xterm is an old program, the syntax for this is what you could politely call interesting. The full gory details are in the KEY BINDINGS section of the xterm manpage, and you can also see the Arch wiki xterm page's section on this.
(Urxvt aka rxvt-unicode can also do key bindings through X resources, but as a more modern program it has a much simpler X resource syntax for them. See the 'keysym.sym' writeup in the urxvt manpage's section of X resources. For other current Unix terminal emulators, you're on your own, but things like gnome-terminal and konsole are probably configured through a GUI.)
I've given an example of these resources several years ago in my entry on getting xterm and modern X applications to do cut and paste together, but today I want to show and discuss my full set of xterm bindings, which now that I look at them turn out to have some history embedded in them and thus probably some surplus things. Here is the X resource:
XTerm*VT100.Translations: #override <Key>Prior: scroll-back(1,halfpage) \n\ <Key>Next: scroll-forw(1, halfpage) \n\ Shift<Key>BackSpace: string(0x7f) \n\ Shift<Key>Delete: string(0x7f) \n\ Meta<Btn2Down>: ignore() \n\ Ctrl Shift <KeyPress> C: copy-selection(CLIPBOARD) \n\ Ctrl Shift <KeyPress> V: insert-selection(CLIPBOARD)
A lot of this is relatively obvious; you have some modifiers, the
action involved in <..>, and then the key or mouse button name. One
tricky bit is that the key name is usually the X keysym, which
is usually but not always related to what the key produces and what
is printed on it. If in doubt, my tool for finding out what keysym
something generates is the venerable xev
. I'm honestly not sure what the
difference is between <Key>
and <KeyPress>
, if there is any.
(If your mouse has additional weird buttons, xev
is also a
good way to find out what X button numbers they generate.)
There's nothing really intricate or clever in my bindings, which is basically how I feel it should be with X terminal emulators if at all possible. Every bit of highly customized behavior you create in your favorite terminal emulator is another obstacle you'll run into if you ever need to use another one temporarily (and if my experience is anything to go by, sooner or later you'll need to for some reason).
As far as the actual bindings go, the Ctrl-Shift-C and Ctrl-Shift-V
bindings are from my entry on cut and paste.
The bindings for Prior
and Next
make the PgUp
and PgDn
scroll only a half a page at a time, which I prefer because it makes
it easier to skim through a scrolled xterm without worrying that
I'll miss noticing something at the top or the bottom of some page;
I can mostly scan the middle area.
The shift-Delete and shift-Backspace bindings are basically there to make the shifted versions of these characters behave the same as the un-shifted versions, probably because I kept accidentally holding the shift key down just a little bit too long when I was going to delete a character. These particular bindings are quite old and predate my no longer swapping Delete and BackSpace. I'm not sure if they're necessary any more.
Ignoring the middle mouse button when meta is held is also an old binding. The very good reason to do nothing here is that the default key binding for this is to clear xterm's scrollback buffer. I definitely don't want to have that on any key combination that I can hit easily, although these days my window manager normally wouldn't let the mouse click through anyways (I have a window manager level binding for meta middle mouse button).
PS: This exercise also serves to demonstrate the kind of things that can sit quietly in the depths of X resources files and other obscure sources of customizations if you use the same environment for long enough. Which I very definitely have.
|
|