xterm hacks

A few hacks for using the traditional X11 terminal emulator in a modern desktop environment.
by David Simmons, June 2005


This page describes two hacks for the xterm program:

  1. a new "full screen" feature, and
  2. a solution for the "copy and paste" incompatibilities with other applications. Also, so the better alternative solution that doesn't require patching and recompiling.

What is xterm, and why hack it?

The xterm program is the traditional terminal emulator for the X Window System. In layman's terms, xterm is the text window used to interact with command-line and non-graphical UNIX applications, such as the UNIX shell, vi, pine, and such. An xterm window usually looks something like this:

The xterm program is somewhat antiquated, because modern desktop environments usually provide their own terminal emulator program that integrates seamlessly into the environment. For example, GNOME provides the "GNOME Terminal" and KDE provides "Konsole". However, many people -- especially old timers like myself -- prefer the simplicity of xterm.

On this page, I describe a couple of hacks which I find to be valuable additions to the xterm experience. The first hack adds a "full screen" feature to xterm, and the second fixes the annoying "copy and paste" incompatibilities between xterm and modern applications such as Firefox.



My patches to xterm may be distributed and used under the same terms as xterm itself. I've extracted this copyright/license block from xterm's main.c source file. I'm not a lawyer, so make of it what you will.

Full screen mode

While using the excellent, free PuTTY ssh client in Windows, I became hooked on its "full screen" mode which can be invoked with the Alt-Enter keystroke (if support for this is enabled in the preferences.) When using the program in full-screen mode, the contents of the window occupy the full screen and there are no decorations such as menus, toolbars, the desktop environment's "Start" menu, etc. I find this mode useful when I'm viewing a large amount of information, or when I don't want to be distracted by the desktop environment.

Implementing a full-screen mode would have been more difficult back in the early days of X11 and xterm. However, modern desktop environments make it easy by allowing applications to simply set the "_NET_WM_STATE_FULLSCREEN" property on their windows. I've produced a patch to xterm for toggling full-screen mode when either the Alt-Enter keystroke is received, or the "fullscreen" option is selected from xterm's "Main Options" menu.

The fullscreen patch adds a new option to the Main Options menu. Fullscreen mode can also be toggled with Alt-Enter.

This feature works well in GNOME, and I would guess it also works in KDE. The last time I checked, the Window Maker window manager still did not have support for the _NET_WM_STATE_FULLSCREEN property.

To install the fullscreen patch, you must download the xterm source code and the fullscreen patch, then compile the source according to the instructions in the "compiling xterm" section below.

Friendly copy-and-paste

Many xterm users are frustrated because it is difficult to copy text from an xterm and paste it into modern applications such as Firefox. The root of the problem is that there are two different "selection buffers" that are commonly used in the X Window System:

The "CLIPBOARD" selection buffer is used for the cut-and-paste functions that most users are familiar with: Selecting the "cut", "copy" or "paste" menu items from the application's "Edit" menu, or using the corresponding CTRL-X, CTRL-C, or CTRL-V shortcut keys. This selection buffer is the standard means of performing cut-and-paste operations in most modern applications. However, this selection buffer is unfortunately not used at all by xterm in the default configuration.
The "PRIMARY" selection buffer receives data when the user highlights text with the mouse. The text in this buffer is pasted when the user presses the middle mouse button in an application's text entry field. This cut-and-paste buffer is a legacy function which new users are generally not told about, in the interests of avoiding mass confusion. Most modern applications support this buffer. This is the only buffer used by xterm.

The annoyance is that text highlighted in xterm cannot be pasted into a modern application with CTRL-V (or Edit->Paste). The middle mouse button usually works for pasting, but the PRIMARY buffer is often overwritten by inadvertent highlighting before the user is ready to paste. For example, if a user highlights a URL in an xterm, switches to Firefox, and presses ALT-D to navigate to the URL bar, then Firefox will automatically highlight the old contents of the URL bar, thus overwriting the highly volatile PRIMARY buffer before the user can paste the new URL!

Further details about the proper use of X selection buffers are available on this excellent page at freedesktop.org.

To solve the problem, I've created a "copynpaste" patch which causes xterm to copy text into both the PRIMARY and CLIPBOARD when a selection is highlighted, and text is pasted from CLIPBOARD if there is no data available in PRIMARY. I believe this results in the most sane behavior. With this patch, you can highlight a URL in xterm and paste it into the Firefox URL bar with CTRL-V!

To install the copynpaste patch, you must download the xterm source code and the copynpaste patch, then compile the source according to the instructions in the "compiling xterm" section below.

Alpha X11 propeller heads may notice that the patch actually just modifies the built-in default "translations" resource to add CLIPBOARD to the list of selection buffers it uses. This means that it is theoretically possible to add support for CLIPBOARD without recompiling, by adding an appropriate entry to your .Xdefaults or XTerm app-defaults file! This is left as an exercise for the reader.

Friendly copy-and-paste without patching

Added July 3, 2007.
cweich has provided the solution (hinted at in the previous paragraph) to using friendly copy-and-paste without patching and recompiling:

Hi David, 

It has taken me a while but your source helped me to find a softer
version for the Xterm Copy and Paste trick. (I used to compile your
program, but our sysadmin was not very happy about this due to obvious
updating issues.) So the patch lines helped me to find out that for
the Copy & Paste functionality one simply needs to add the
following line to the ~/.Xresources file: 

XTerm*VT100.translations: #override <Btn1Up>: select-end(PRIMARY, CLIPBOARD, CUT_BUFFER0)

You can test it with an xterm: 

xterm -xrm 'XTerm*VT100.translations: #override <Btn1Up>: select-end(PRIMARY, CLIPBOARD, CUT_BUFFER0)'

Maybe you want to add this hint to your page. I suppose there is a
certain number of people who use the hacked xterm source.

Compiling xterm

Installing patches to xterm (such as the fullscreen and copynpaste patches) involves downloading the xterm source code, patching the source code, compiling the source, and installing the resulting binary.

  1. Download the xterm source code and any patches you'd like to install. The xterm source code and the patches are available in the downloads section above.
  2. Untar the xterm source code:
    tar xvfpj xterm-XF86-4.3.0.tar.bz2
  3. Change directory to the root of the xterm source tree, and apply any patches you'd like. For example, to apply both the fullscreen patch and the copynpaste patch, run these commands:
    cd xterm-XF86-4.3.0
    patch -p1 < ../xterm-fullscreen.patch
    patch -p1 < ../xterm-copynpaste.patch
  4. Compile the xterm source with these commands:
  5. Install the resulting xterm binary:
    cp -p xterm /usr/local/bin/
    Make sure that /usr/local/bin is in your default PATH environment variable. If you are brave, you could also install xterm with "make install", although I believe this will permanently overwrite your existing vendor-supplied xterm binary (which happens to be in /usr/X11R6/bin on my Fedora Core 3 system).

Future improvements

  1. It would be nice if I provided ready-to-install hacked xterm RPMs for various Linux distributions, now wouldn't it? Maybe some day.
  2. I should modify the charClass resource on my local machine one of these days to make the set of "character classes" reflect what PuTTY uses. This would let me double-click "myfile.txt" and have the entire string highlighted instead of just "myfile".
  3. There is currently no internal support in xterm for reading mail.


Further information about these concepts can be found on the following pages: