JU home page

A Quick Guide to Customizing Your xterm(1)

Historically, xterm(1) is the only terminal emulator available on all common X11 Window Systems. It provides for a command-line interface (CLI) that, by default, behaves much like a VT102 terminal. Despite its apparent simplicity xterm(1) is highly customizable, allowing you to change fonts, sizes, and colors with only a little effort. If you are a CLI user, you spend a lot of time using such "terminals", so it is well worth the effort to find out how to optimize your use of xterm(1), the original and most ubiquitous X11-based terminal emulator.

Fri Jul 11 13:47:26 EDT 2008

Note: The esc(1) script does not work with ksh93. So the esc script was changed from the ksh(1) shell to the bash(1) shell. If you use ksh88 or pdksh, you can change it back to the ksh(1) shell if you prefer.

For the impatient

This collection of scripts and documentation is available in a single uuencoded gzipped tar file suitable for download. If you are impatient, download the file and expand it by executing it as a shell script. This will create the subdirectory "scripts_xterm". Add that directory to your search path and then in an xterm(1) enter:

# change colors and size and font size
esc -bg gray -fg yellow -rows 40 -columns 132 -fn 5
kolor blue white
kolor
full 10

If anything that happened interests you, read the rest of this document!

Setting xterm(1) Options

Just because xterm(1) has a lot of options doesn't mean it is obvious how to get to all of them. There are four basic methods, each with it's advantages and disadvantages. Options can be selected with

FAQ (Frequently Asked Questions)

References


Menus

The easiest xterm customization feature to learn is the font menu. Open up an xterm and you can see a font menu by holding down the "Ctrl" key on your keyboard and clicking in the body of the xterm(1) window with your right mouse button. Release the mouse over a font selection. Making a selection only effects the current xterm(1) window.

Note that additional features are accessible from menus evoked by the left and middle mouse buttons. Hold down the control key and click the other mouse buttons in the terminal screen window to see the additional menus. Note that if you have a two-button mouse, you can often simulate the middle mouse button by holding down the right and left mouse buttons simultaneously (in CygWin, you have to add the -emulate3buttons switch to your xinit(1) command).

Changing the menu fonts

If you find font names you prefer to the defaults ( using commands such as xlsfonts(1) or a window-based program called xfontsel(1),perhaps.) you can change the font menu for xterm windows by changing the appropriate XResources. There are several ways to do this. Here is a script that will change the menu for new xterms ...

#!/bin/sh
xrdb -merge <<\EOF
!------------------------------------------
! Label for menu item.
xterm*fontMenu*font1*Label: Unreadable
! Very small font.
xterm*VT100*font1:           nil2
!------------------------------------------
xterm*fontMenu*font2*Label: Tiny
xterm*VT100*font2:           5x7
!------------------------------------------
xterm*fontMenu*font3*Label: Small
xterm*VT100*font3:           6x10
!------------------------------------------
xterm*fontMenu*font4*Label: Medium
xterm*VT100*font4:           7x13
!------------------------------------------
! Menu item five for R5 and up only.
xterm*fontMenu*font5*Label: Large
xterm*VT100*font5:           9x15
!------------------------------------------
! Menu item six for R5 and up only.
xterm*fontMenu*font6*Label: Huge
xterm*VT100*font6:           10x20
!------------------------------------------
! Notes:
! In this example, the font names are 
! nil2, 5x7, 6x10, 7x13, 9x15, and 10x20
! 
! The combined commands
!    xlsfonts -fn '*-c-*'
!    xlsfonts -fn '*-m-*'
! list almost all of the fixed-space fonts
! that work with all xterms. (Not all 
! xterm(1) versions can handle
! proportionally spaced fonts).
!------------------------------------------
EOF

A common alternate way to make your font selections the default is to add the lines shown above between the "EOF" delimiters to a file called $HOME/.Xdefaults.

Note that the "nil2" font is typically the tiniest font available (2x2). One use for this "unreadable" font is to make a window nearly as small as an icon, but still be able to monitor when the program generates output.

The Selection Font

The selection font seems, in my experience, to be rarely used. This may be because the meaning of the "Selection" font often found at the font menu bottom is not obvious. If you mark(highlight) the name of a font in the text in a terminal text window with the left mouse and then select the "Selection" font on any xterm(1) font menu, the terminal will try to use the highlighted text as a font name. If you wish to try this, enter the command

xlsfonts '*-*-*-c-*'

to list many fixed-space fonts to the screen. Mark a font name with the left mouse. Next, use "Ctrl" and the right mouse button to bring up the font menu and highlight "Selection". The font should change immediately (do not mark any extra spaces at either end of the font name).

But here is the most convenient way to use standard tools to both change the selection font and find font names for your X resources. Start the utility

xfontsel &

And a small window appears. Clicking on the columns lets you pick various font styles and sizes. If you find an interesting one click on the "select" button and that font will now be available as the "select" font on the xterm font menu.


Command Line Options

Simple options such as the size of the window and it's colors can easily be selected from the command line. For example:

xterm \
   -bg white -fg black  \ 
   -geometry 80x40-0+0 \      
   -sl 1500 -sb           
   -rightbar \            
<-- set background and foreground (text) color
<-- 80 columns by 40 lines
<-- scrollbar with 1500 line buffer
<-- put scrollbar on right

This command opens an xterm with a white background, and a black foreground (text) color, with a righthand scrollbar. More complex settings are possible, and running

man xterm

will give you a complete list of command line options to choose from. The man(1) page is so long, you may wish to print or edit the man page.

man xterm |col -b >xterm.txt # convert man(1) page to flat ASCII text
# if you have man2html(1) you can make the man page into html 
whereis -m xterm|cut -d: -f2|xargs gunzip -f -c|man2html > xterm.html

Geometry specification

If you are not familiar with X11 geometry specifications, an introduction is in order. For many applications the size is specified in pixels. In the case of xterm(1) windows, the size is in font units (but the offsets from the screen borders are still in pixels). That is, the format is

COLUMNSxROWS+XOFFSET+YOFFSET

where offsets are measured from the upper left corner when positive, but from the lower right corner when negative. So this would put four xterm windows on the screen in the corners:

xterm -bg white -fg black  -geometry 80x24-0-0 &
xterm -bg black -fg white  -geometry 80x24-0+0 &
xterm -bg black -fg yellow -geometry 80x24+0-0 &
xterm -bg green -fg black  -geometry 80x24+0+0 &

XResources

When it comes to customizing xterm(1) in a serious way XResources are the way to go, as the custom settings can get quite large.

Normally, XResources are read from the file ~/.Xresources when you login (which are then available to any program using the display) , or from the file ~/.Xdefaults when you start a program (from the programs that share that home directory) -- but either way the file contents look the same. So if you wish to reload new values in your .Xresources file run the command:

xrdb -merge ~/.Xresources

Let's get started with a simple example. Save the following into a file called .Xresources in your home directory, or append it to the file if it exists. All of the allowed options are listed in the man page(1), by the way.

!-------------------------------------
XTerm*background:       black
XTerm*foreground:       white
XTerm*cursorColor:      red
XTerm.vt100.geometry:   80x40-200-100
XTerm*scrollBar:        true
XTerm*scrollTtyOutput:  false
XTerm*ttyModes:      erase ^h kill  ^x
!-------------------------------------
! Make scroll bar thicker
XTerm*scrollbar*thumb:       scroll.xbm
XTerm*scrollbar*thickness:   26
XTerm*scrollbar*foreground:  navy
XTerm*scrollbar*background:  red
XTerm.VT100.scrollbar.foreground:    red
XTerm*scrollBar:     true
XTerm*saveLines:     1000
!-------------------------------------
XTerm*CharClass:     1-31:32,42-58:48,63:48,126:48
XTerm*internalBorder:        2
XTerm*c132:  on
XTerm*EightBitInput: on
XTerm*reverseWrap:   off
XTerm*jumpScroll:    on
XTerm*multiScroll:   on
!-------------------------------------
XTerm.VT100.font:       -misc-fixed-medium-r-normal-*-13-120-*-*-*-*-*-*
XTerm.VT100.boldFont:   -misc-fixed-bold-r-normal-*-13-120-*-*-*-*-*-*
!-------------------------------------

Now run the X11 resource database command:

xrdb -merge $HOME/.Xresources

Once you've done this you can open an xterm(1) and see that the changes have taken effect on all new xterms no matter where you launch them from - even though you've made no command line changes.

In the sample above you may have noticed all the lines start with the name "XTerm". The special class name "XTerm" applies to all xterm(1) windows you open. But you can have as many unique setups as you wish. Here we make a short script that uses the prefix "SAMPLE"

#!/bin/sh
xrdb -merge <<\EOF
!-------------------------------------------------------------------------------
!                               background, text, and cursor colors
SAMPLE*background:                  black
SAMPLE*foreground:                  white
SAMPLE*cursorColor:                 red
!-------------------------------------------------------------------------------
!                               size and position of window
SAMPLE.vt100.geometry:              80x40-200-100
!-------------------------------------------------------------------------------
!                               Make scroll bar thicker
SAMPLE*scrollbar*thumb:             1x1
SAMPLE*scrollbar*thickness:         26
SAMPLE*scrollbar*foreground:        navy
SAMPLE*scrollbar*background:        red
SAMPLE.VT100.scrollbar.foreground:  black
SAMPLE*scrollBar:                   true
SAMPLE*saveLines:                   1000
!-------------------------------------------------------------------------------
SAMPLE*scrollTtyOutput:             false
SAMPLE*ttyModes:                    erase ^h kill ^x
SAMPLE*CharClass:                   1-31:32,42-58:48,63:48,126:48
SAMPLE*internalBorder:              10
SAMPLE*c132:                        on
SAMPLE*EightBitInput:               on
SAMPLE*reverseWrap:                 off
SAMPLE*jumpScroll:                  on
SAMPLE*multiScroll:                 on
!-------------------------------------------------------------------------------
SAMPLE.VT100.font:     -misc-fixed-medium-r-normal-*-13-120-*-*-*-*-*-*
SAMPLE.VT100.boldFont: -misc-fixed-bold-r-normal-*-13-120-*-*-*-*-*-*
!-------------------------------------------------------------------------------
EOF
#################
exit
#################

After you run the preceding script, you can then make an xterm using those settings by entering

xterm -name SAMPLE &

The man(1) page describes all the X11 resources you can set. An example file that sets the defaults on your system can probably be found in the file

/etc/X11*/app-defaults/XTerm

Where XResources are found

As hinted at, once you find out what the X11 Resources for xterm(1) are and what they do from the man(1) page, you will find there is a perplexing number of ways to set the resources. I recommend a new user put resource values in his $HOME/.Xdefaults file. But this will only effect programs that use that home directory. If you use multiple machines that do not share a home directory, you may find it better to load your ~/.Xdefaults into memory each time you start your X11 display by using the command

xrdb -merge < $HOME/.Xdefaults

Short of buying a book on X11 windows, the best readily available description of how X starts and looks for resources and configuration files in found on most systems by the command:

man X

Hopefully, you will not need to know this subject in great detail. But resources can be set per application, per host, per display, per screen, per language, and per user. If you do need a finer grain of control, this is an outline of major places where XResources are searched for, from highest priority to lowest priority:

  1. coded properties; these usually cannot be changed without changing the source code.
  2. Command line options bound to resources (through XtAppInitialize() calls). For example:
    xterm -name VMS -geometry 80x24-0+0
    
    Good for calling a block of options via the "-name" option or for setting size and background color.
  3. Resources specified with the -xrm option. The following elaborate example creates a custom keyboard mapping that helps a user use screen-mode products on VMS platforms such as EDT and EVE that does not effect other subsequent xterm(1) windows. That's right -- you can define what practically every key on the keyboard sends to programs running in your xterm(1) window; including defining keys to load in different definitions.
    #!/bin/sh
    #ident  "@(#) 1.0  set better default X11 resources for xterm(1) - John S. Urban"
    xrdb -merge <<\EOF
    !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
    !=============================================================================
    ! VMS-XTERM--VMS-XTERM--VMS-XTERM--VMS-XTERM--VMS-XTERM--VMS-XTERM--VMS-XTERM
    !=============================================================================
    ! Warning: should make a layout for each vendor and branch using cpp commands
    !          processed by xrdb to identify which machine you are on, because
    !          different vendors have quite different numeric keypads.
    !
    ! use the xev program sample (even across network if you do not have it)
    ! to identify the keypad names on the keypad quickly.
    !=============================================================================
    ! make keypad send out vt100 sequences (it already does on some machines)
    ! here is what a real vt100 keypad sends out in application mode:
    ! (where string is preceded by an esc, e.g. 1 sends out esc O q).
    !
    !*----------*----------*----------*----------*   This panel describes the escape
    !|   GOLD   |   HELP   |  FNDNXT  |  DEL L   |   strings sent out from a VT100
    !|          |          |  FIND    |  UND L   |   keyboard's numeric keypad when
    !| OP PF1   | OQ PF2   | OR PF3   | OS PF4   |   the keypad is in application
    !*----------*----------*----------*----------*   mode. The application strings
    !|   PAGE   |   SECT   |  APPEND  |   DEL W  |   are highlited by the [ [
    !| COMMAND  |   FILL   |   REPL   |  UND W   |   attribute. These two-character
    !| Ow   7   | Ox  8    | Oy  9    | Om  =    |   sequences are always preceded
    !*----------*----------*----------*----------*   by the escape character ESC.
    !| ADVANCE  |  BACKUP  |   CUT    |  DEL C   |
    !|  BOTTOM  |   TOP    |  PASTE   |  UND C   |
    !| Ot  4    | Ou  5    | Ov  6    | Ol  ,    |
    !*----------*----------*----------*----------*
    !|  WORD    |   EOL    |  CHAR    |  ENTER   |
    !| CHNGCASE | DEL EOL  | SPECINS  |  SUBS    |
    !| Oq  1    | Or  2    | Os  3    |          |
    !*----------*----------*----------*          |
    !|        LINE         | SELECT   |          |
    !|     OPEN LINE       |  RESET   |          |
    !| Op       0          | On  .    |[OM[      |
    !*---------------------*----------*----------*
    !=============================================================================
    ! ON THE WORKSTATION:
    ! xterm -name VMS -e telnet escc01
    !=============================================================================
    !   Redefine keys for use with EDT on VMS as a vt100
    !   Version 1.0, John S. Urban, 06/20/91
    !
    ! For DEC5000:
    !=============================================================================
    ! make the delete key the erase key for this window.
    VMS*ttyModes: erase ^h
    VMS*Translations: #override\n\
    <KeyPress>Delete:       string(0x7f) \n\
    Shift <KeyPress>Delete:       string(0x1b) string(0x08) \n\
    <KeyPress>KP_F1:        string(0x1b) string("OP") \n\
    <KeyPress>KP_F2:        string(0x1b) string("OQ") \n\
    <KeyPress>KP_F3:        string(0x1b) string("OR") \n\
    <KeyPress>KP_F4:        string(0x1b) string("OS") \n\
    <KeyPress>KP_0:         string(0x1b) string("Op") \n\
    <KeyPress>KP_1:         string(0x1b) string("Oq") \n\
    <KeyPress>KP_2:         string(0x1b) string("Or") \n\
    <KeyPress>KP_3:         string(0x1b) string("Os") \n\
    <KeyPress>KP_4:         string(0x1b) string("Ot") \n\
    <KeyPress>KP_5:         string(0x1b) string("Ou") \n\
    <KeyPress>KP_6:         string(0x1b) string("Ov") \n\
    <KeyPress>KP_7:         string(0x1b) string("Ow") \n\
    <KeyPress>KP_8:         string(0x1b) string("Ox") \n\
    <KeyPress>KP_9:         string(0x1b) string("Oy") \n\
    <KeyPress>KP_Decimal:   string(0x1b) string("On") \n\
    <KeyPress>KP_Enter:     string(0x1b) string("OM") \n\
    <KeyPress>KP_Separator: string(0x1b) string("Ol") \n\
    <KeyPress>KP_Subtract:  string(0x1b) string("Om") \n\
    <KeyPress>KP_Divide:    string(0x1b) string("OP") \n\
    <KeyPress>KP_Multiply:  string(0x1b) string("[29~") \n\
    <KeyPress>KP_Add:       string(0x1b) string("Ol") \n\
    Shift <KeyPress>Up:           scroll-back(1,lines) \n\
    Shift <KeyPress>Down:         scroll-forw(1,lines) \n\
    Shift <KeyPress>Right:        string(0x1b) string("f") \n\
    Shift <KeyPress>Left:         string(0x1b) string("b") \n\
    Shift <KeyPress>Tab:          string(0x1b) string("*") \n\
    <KeyPress>Help:         string(0x1b) string("OQ") \n\
    Shift<KeyPress>F1:      string(0x1b) string("Ow") \n\
    Shift<KeyPress>F2:      string(0x1b) string("Ox") \n\
    Shift<KeyPress>F3:      string(0x17) \n\
    Shift<KeyPress>F4:      string(0x1b) string("OSERASE_LINE") string(0xff0d) \n\
    Shift<KeyPress>F5:      string(0x1b) string("OQ") \n\
    Shift<KeyPress>F6:      string(0x1b) string("OSQUIT") string(0xff0d) \n\
    Shift<KeyPress>F7:      string(0x1b) string("OSFIND NEXT") string(0xff0d) \n\
    Shift<KeyPress>F9:      string(0x1b) string("OSCENTER_LINE") string(0xff0d) \n\
    Shift<KeyPress>F10:     string(0x1b) string("OSBOTTOM") string(0xff0d) \n\
    Shift<KeyPress>F12:     string(0x1b) string("OSREPLACE") string(0xff0d) \n\
    Ctrl<KeyPress>F1:       string(0x1b) string("Ow") \n\
    Ctrl<KeyPress>F3:       string(0x1b) string("M") \n\
    Ctrl<KeyPress>F4:       string(0x7F) \n\
    Ctrl<KeyPress>F7:       string(0x1b) string("OSSET_LEFT_MARGIN") string(0xff0d) \n\
    Ctrl<KeyPress>F8:       string(0x1b) string("OSSET_RIGHT_MARGIN") string(0xff0d) \n\
    Ctrl<KeyPress>F9:       string(0x1b) string("OSSET_PARAGRAPH_INDENT") string(0xff0d) \n\
    Ctrl<KeyPress>F11:      string(0x1b) string("OP SYSTEM BULLETIN") string(0xff0d) \n\
    Alt<KeyPress>F4:        string(0x1b) string("OSset width 132") string(0xff0d) \n\
    <KeyPress>F1:      string(0x1b) string("Ow") \n\
    <KeyPress>F2:      string(0x1b) string("Oy") \n\
    <KeyPress>F3:      string(0x1b) string("OR") \n\
    <KeyPress>F4:      string(0x1d) \n\
    <KeyPress>F5:      string(0x1b) string("OSRESTORE WORD") string(0xff0d) \n\
    <KeyPress>F6:      string(0x1b) string("OSEXIT") string(0xff0d) \n\
    <KeyPress>F7:      string(0x1b) string("OP") \n\
    <KeyPress>F9:      string(0x1b) string("OSFILL PARAGRAPH") string(0xff0d) \n\
    <KeyPress>F10:     string(0x1b) string("OSTOP") string(0xff0d) \n\
    <KeyPress>F11:     string(0x1b) string("[23~") \n\
    <KeyPress>F12:     string(0x1b) string("[24~") \n\
    <KeyPress>Home:    string(0x1b) string("OS") \n\
    <KeyPress>Prior:   string(0x1b) string("On") \n\
    <KeyPress>Next:    string(0x1b) string("Op") \n\
    <KeyPress>End:     string(0x5) \n\
    <KeyPress>Insert:       string(0x1b) string("OM") \n\
    <KeyPress>0x1000FF0D:   scroll-back(1,page) \n\
    <KeyPress>0x1000FF0E:   scroll-forw(1,page) \n\
    <KeyPress>0x1000FF09:   string(\010) \n\
    <KeyPress>0x1000FF0A:   string(\005) \n\
    <KeyPress>BackSpace:    string(0xff) \n\
    <KeyPress>Select:       select-start() \n\
    <KeyPress>0x1000FF02:   select-end(PRIMARY,CLIPBOARD,CUT_BUFFER0) \n\
    Meta <KeyPress>0x1000FF02:   select-end(CLIPBOARD) \n\
    <KeyPress>0x1000FF04:   insert-selection(PRIMARY,CLIPBOARD,CUT_BUFFER0) \n\
    Meta <KeyPress>0x1000FF04:   insert-selection(CLIPBOARD) \n\
    <Btn1Down>:             select-start() \n\
    <Btn1Motion>:           select-extend() \n\
    <Btn1Up>:               select-end(PRIMARY,CLIPBOARD,CUT_BUFFER0) \n\
    Button1<Btn2Down>:             select-end(CLIPBOARD) \n\
    Button1<Btn2Up>:               ignore()" \n\
    <KeyPress>KP_Enter:     string(0xff0d)
    VMS*c132: on
    VMS*VT100*background: black
    VMS*VT100*foreground: yellow
    VMS*cursorColor: red
    VMS*jumpScroll: on
    VMS*loginShell: off
    VMS*multiScroll: on
    VMS*windowName: VMS (SERVICE CENTER: WIN 284-6100 / 412-374-6100)
    VMS*iconName: VAX
    VMS*scrollBar: on
    VMS*saveLines: 1500
    VMS*VT100*geometry: 80x24
    VMS*internalBorder: 2
    VMS*reverseWrap: off
    VMS*iconPixmap: keyboard16
    ! VMS*VT100*font: -adobe-courier-bold-r-normal--14-100-100-100-m-90-iso8859-1
    ! <KeyPress>Insert:       string(\001) \n\
    !=============================================================================
    EOF
    XTERM  xterm -name VMS \
           -sb -sl 1200 \
           -geometry 80x24-0+0 \
           -fg yellow -bg black -cr red \
           -fn -dec-terminal-medium-r-normal--14-140-75-75-c-80-iso8859-1 \
           -fb -dec-terminal-bold-r-normal--14-140-75-75-c-80-iso8859-1 
           -e telnet <em>remove_hostname</em> &
    
    
    This is a nice technique for scripts. Everything is self-contained in a single file, and values effect only a specific xterm(1).
  4. Your Xdefaults file(s). Normally, this is a file called .Xdefaults in your home directory. You can make host-specific files called
    .Xdefaults-hostname
    
    The environmental variable $XENVIRONMENT can be used to set the path for this file.
  5. If you use multiple screens, you can specify per-screen resources using the -screen option on xrdb(1). For example:
    xrdb -screen  -merge  -display :0.1  <<EOF
    XTerm*background:       black
    XTerm*foreground:       white
    EOF
    
  6. You can set the RESOURCE_MANAGER property of the root window, handled with xrdb(1). (If it is empty, then the contents of the file $HOME/.Xdefaults is used instead).
    Note the xrdb(1) typically runs the input files thru cpp(1), a conditional preprocessor. It also pre-defines all the variables displayed by
    xrdb -symbols
    
    You can thus conditionally load and merge other files depending on which display you are using. You might define different fonts for different screen resolutions, and so on. A simple use might be based on the server host name. Two of my machines are called "eureka" and "electra", so I might use something like:
    xrdb -merge <<EOF
    #define eureka 1
    #define electra 2
    #if SERVERHOST==eureka
    XTerm*background: black
    #elif SERVERHOST==electra
    XTerm*background: pink
    #else
    XTerm*background: gray
    #endif
    EOF
    

    Instead of using multiple .Xdefaults-hostname files.

    The cpp(1) program can vary from machine to machine.

    If you want to look thru a sample resource file for use with xrdb(1) that has a lot of xterm(1) settings in it, here is one of mine ... Xresources.

    This is the easiest mechanism for most user XResource customizations. Resources for multiple applications are looked for here after most systems defaults are read. Other file-based options often override system defaults instead of inheriting them.
  7. User resources searched by calling XtResolvePathname():
    • search the paths in $XUSERFILESEARCHPATH

      Note that only the first resource file found in XUSERFILESEARCHPATH is used, if the search path consists of several directories.

    • If $XUSERFILESEARCHPATH is not set then the default application resource directory $XAPPLRESDIR/ is searched. The format of X[USER]FILESEARCHPATH:
      %N      The value of the filename parameter, or the
              application's class name.
      %T      The value of the file "type".  In this case, the
              literal string "app-defaults"
      %C      customization resource (R5 only)
      %S      Suffix.  None for app-defaults.
      %L      Language, locale, and codeset (e.g. "ja_JP.EUC")
      %l      Language part of %L  (e.g. "ja")
      %t      The territory part of the display's language string
      %c      The codeset part of the display's language string
      
      By default, the following files are searched:
      ($XAPPLRESDIR/%L/%N:$XAPPLRESDIR/%l/%N:$XAPPLRESDIR/%N:$HOME/%N)
      
    • otherwise $HOME/
      ($HOME/%L/%N:$HOME/%l/%N:$HOME/%N)
      
    If you do not change the defaults, what this usually means is that resources are looked for in the directory
    /usr/lib/X11*/app-defaults/
    
    and that a separate file for each X11 application can be found in this directory.

Escape Strings

By sending strings of control characters to the data stream (the standard output of programs running in the xterm(1) window) you can position the cursor, clear the screen, ring a bell sound, and do all the other things you can do on a vt102 (or tektronix 4014) terminal. But extensions to the vt102 (or vt220 in some versions) terminal model let you change fonts, colors, and terminal size and interact with the mouse, among other things. A table of escape sequences describes all the command sequences available. Note that termcap(5)/terminfo(5) files with an xterm(1) entry are now basically standard on Unix variants (these files describe your terminal's capabilities to the system), so you can access many of these capabilities using standard libraries such as the curses(3)/ncurses(3) routines provide. This is how applications like vi(1)/vim(1), emacs(1) in terminal mode (text editors), lynx(1) and w3m (WWW browsers), naim(like AIM), talk(1), screen(1), mc(1) (a screen-oriented shell), and elm(1) and mh(1) (mail utilities) control the appearance of the screen.

A very underutilized combination for building screen-mode interfaces is the use of lynx(1) with "Simulated CGI support" (you may have to compile lynx with this enabled on your system). This allows you to use HTML forms that calls scripts on your local machine without the need for an httpd server. That is, you can make web pages that interface to your scripts that run on your local machine without benefit of a web server, using basic HTML forms. This is a capability that would be of great value if easily available in all browsers! Note that you should use at least version 2.8.5rel.5 of lynx(1) if you use the "Simulated CGI support".

Escape sequences are primarily of interest to utility writers, so we won't go into this topic further in this introduction. If you are interested in writing your own terminal-based utilities, you need to look at the ncurses(3) library and it's alternatives (slang(3), PDCurses(3), ncursesw(3), ...), and maybe some "pixmap" libraries (like AAlib(3) and libcaca(3)) before deciding to make direct use of the escape codes.

For those who want to jump straight to using some of the more basic escape sequences, the next section introduces some simple scripts that use the escape sequences to control basic window attributes.


Scripts for the xterm(1) program

Scripts for the xterm(1) program

The escape strings obeyed by an xterm(1) let you dynamically change xterm(1) attributes; while the command line options and Xresources just let you set the initial style. So the escape strings are a very powerful feature, but there isn't a standard utility for accessing the features this way. The following scripts provide that functionality. Note almost all these scripts call the first script listed here, esc(1); so download it first.

esc(1)

Font

Window geometry

Window manager

Color

other



Mouse Usage in vim(1)

If you use the vim(1) editor the simple answer on how to activate mouse support is to start the editor and enter:

:set mouse=a

Now you can move the cursor and mark text with the mouse (and then move, copy, and delete it). There are many options for customizing the mouse actions, but for starters if you mark some text and enter the letter "d" the marked region is deleted. Move the mouse somewhere else and enter "p" to put a copy of the deleted text back into the document. You can add the above line (sans the colon prefix) to your $HOME/.vimrc file if you have one to make this the default mode. The internal :help command text describes mouse usage with vim(1). An example .vimrc(5) file and associated script are available. Download the files vin(1) and vimrc(5). Place them in your search path and then execute vin(1) as you would vim(1). For those not familiar with placing files into their search path, use the following steps:

cd
mkdir testit
cp vin vimrc testit/.
chmod a+xr testit/*

set path=( $path $home/testit )      # csh/tcsh
   or
export PATH;PATH=$PATH:$HOME/testit  # sh/ksh/bash

man xterm |col -b >xterm.txt
vin xterm.txt

Using esc(1) with vim(1)

Here is an example that makes the xterm(1) have a black background and white text and a large font and window size and then calls vim(1) with most color modes on. After vim(1) finishes, the window is restored to the original state.

# query xterm values
ROWS="`esc -rows`" COLS="`esc -cols`" DOWN="`esc -down`" RIGHT="`esc -right`"
FONT="`esc -fn`" FOREGROUND="`esc -fg`" BACKGROUND="`esc -bg`" CURSOR="`esc -cr`"
ORIGINAL="esc -rows '$ROWS' -cols '$COLS' -down '${DOWN}' -right '${RIGHT}' \
 -fn '$FONT' -bg '${BACKGROUND}' -fg '${FOREGROUND}'"
################################################################################
esc -bg black -fg white  -cr red  # set colors
full 20                           # pick large font and entire screen
raise                             # raise window
################################################################################
   vim \
   --cmd 'set syntax' \
   --cmd 'set nocompatible' \
   --cmd 'set history=50' \
   --cmd 'set ruler' \
   --cmd 'syntax on' \
   --cmd 'set hlsearch' \
   $*
eval $ORIGINAL
################################################################################
exit
################################################################################

Using the esc(1) script with screen(1)

The otherwise very useful screen(1) command intercepts the escape sequences going to the screen, and only translates and forwards those it recognizes. Unfortunately, the xterm(1) extensions to the vt102/vt220 set of sequences that do things like move, resize, and color the terminal are not recognized.

As long as you are on the same machine you started the xterm(1) on, you can get the esc(1) command and screen(1) to work together by setting the environmental variable OTHERTTY to the current tty setting. For example:

env OTHERTTY=`tty` screen  # Start screen(1) command

Similar problems occur if you try to use the Tektronix window from screen. Again, this is because the xterm(1) escape sequences not supported by a VT102 terminal (window resize, font change, ...) used by the scripts will be captured and ignored.

If you are not familiar with the screen(1) application, you should be. It allows you to start many shells in a single xterm(1). This lets you greatly reduce memory usage. A screen program running on a remove machine essentially uses no significant memory on the local machine; but even a local screen(1) session uses next to nothing compared to another X11 window. The screen program can be used for scrolling, meaning you can turn your xterm(1) scroll buffer off.


Fonts

The fixed(1) script described above should help you find fonts that will work with xterm(1). If you enter the command with no parameters, help text appears:

Usage: fixed fontsize [fontnumber|0|choose]

   fontsize:
      select fixed font of point size $1
   fontnumber:
     >0
         number of font to select from a font list
         when more than one font match is found
     =0
         fonts are listed instead of selected.
   choose
         list and use selected fonts until prompted to stop

examples:
   fixed 20          # choose first font size of 20 points
   fixed 20 3        # choose 3rd  fixed font of point size 20
   fixed 18 0        # just list all 18 point fonts
   fixed 18 choose   # list and use all 18 point fonts
   fixed '*' 0       # list all fixed-space fonts
   fixed 0 300       # use 300th fixed=space font

note : if you use a second number, you must have an X connection.

The fixed(1) font uses the esc(1) font to change the font dynamically:

esc -fn fontname

To get a list of all standard X11 fixed-space fonts on your machine the fixed(1) script uses the commands

xlsfonts "*-*-*-c-*"; xlsfonts "*-*-*-m-*"
The best tool for describing a font is
xprop -font 'fontname'
A GUI for selecting font names
xfontsel &

Title

You can statically set the title and icon name thru XResources or command line options; but the most common use of the title bar is to use it to display the current directory name dynamically. The esc(1) -nt TITLE command can be used to set the title and icon name (in most window managers). The most common technique for making your title bar display the current directory as you use the Unix cd(1) command is to change your prompt to echo the correct escape string. For the sh(1)/ksh(1)/pdksh(1)/bash(1) family of shells, you can place something like the following in the .profile/.bashrc/.kshrc in your $HOME directory, for example:

if [ "$TERM" = "xterm" ]; then
   PS1="\033]2;\u@\h:\w\007$ "
fi

An alternative superior solution for the bash(1) shell is to add the following lines to /$HOME/.bashrc:

###############################################################################
TITLE(){
if [ "${TERM}" = "xterm" -o "${TERM}" = "xterm-color" ]
then
   if [ -n "${XTITLE}" ]  # If fixed titlename is present, use it
   then
      echo -ne "\033]0;${XTITLE}\007" ;
   else  # build a title
      echo -ne "\033]0;${USER}@`hostname` ${PWD}\007" | sed -e "s@${HOME}@\~@"
   fi
fi
}
###############################################################################
export PROMPT_COMMAND=TITLE XTITLE
###############################################################################

Now, the XTERM titlebar is reset every time a command is run. It shows the current machine name, user name, and directory.

To force the titlebar to be a given static string, in the shell enter:

XTITLE='the title string'         # Bourne family of shells
setenv XTITLE  'the title string' # C-shell family of shells

To revert back to tracking the current directory, enter:

unset XTITLE    # Bourne
unsetenv XTITLE # C-shell

If you use the csh/tcsh family of shells, assuming the esc(1) script is in your search path, you can use the following in .cshrc (or sometimes .cshrc.local if present, or .tcshrc if using tcsh(1))

if ( ${?prompt} ) then
# This is an interactive session
   if ( "$term" == "xterm" ) then
      alias cd 'chdir \!*;esc -nt'
   endif
endif

There are other ways that vary from shell to shell.

Showing the current command in the title in the bash(1) shell

If you source this file in a bash(1) shell the title will show the currently executing command.

################################################################
# show the currently running command in the title bar
################################################################
# source this file from a bash(1) shell running in an xterm(1)
# and the xterm(1) title bar will show the current
# command being executed or the current directory when idle.
################################################################
if tty -s
then
   TTY=`tty`
else
   TTY=/dev/tty
fi
################################################################
# clever "trap DEBUG" idea accredited to David Pashley
case $TERM in
   rxvt|*term)
      trap 'echo -ne "\e]0;$BASH_COMMAND\007" >>$TTY' DEBUG
      export PS1="\033]2;\u@\h:\w\007\000\h$"
      set -o functrace # show sub-processes
   ;;
esac
################################################################

Colors

The kolor script and esc described above let you set colors. The named colors on your machine can be listed by entering

showrgb

If you want to use more colors than that, or want to calculate them note that color names can be in the form

#RRGGBB

where RR, GG, and BB are hexadecimal values for the red, green, and blue components of the RGB additive color model. For example:

esc -bg "#ffff00" -fg "#ffaa33"

There are many excellent descriptions of the RGB additive color model on the Web.

If you are having problems accessing more than eight colors and your xterm(1) is built to support many colors then you probably need to read a more detailed description of issues dealing with accessing 256 colors in an xterm(1) on GNU/Linux and Unix platforms".


Colored Text

Some applications use multiple colors simultaneously to distinguish text for various reasons... the vim(1) editor can use color to provide syntax information for a variety of file types. The dialog(1) and tcdialog(1) commands use colors to designate function. Most GNU and Unix shells can use color to distinguish file types. The lynx(1) and w3m WWW browsers, pinfo(1), and many other utilities use color.

But depending on how it was built, your xterm(1) may support 8,16,88 or 256 syntax colors. To check how many colors your xterm supports enter:

strings `which xterm` | grep '^color[0-9]' | wc -l
# On CygWin:
strings `which xterm`.exe | grep '^color[0-9]' | wc -l
# on some systems you may have to be root to read the xterm(1) executable

You can rebuild xterm(1) with 256 colors if your version has only 16.

In the vim(1) editor, you can enter the command:

:syntax on
:syntax off

to turn syntax color highlighting on and off

The ls(1) commands that support color should display color if you enter

ls --color=auto # color codes are output only if stdout is connected to a terminal.

The environment variable LS_COLORS can influence the colors, and can be set easily by the dircolors(1) command. Linux systems often have shell aliases set up so the ls(1) command appears to use color by default.

You can use Xresources to change the colors associated with particular color numbers. Note that doing so might make documentation incorrect, for instance. There are some excellent resources on how to set up vim(1) and gvim(1) for use with colors on 256-color xterms on the Web. The most complete one I am aware of is

http://www.stanford.edu/~gi1242/per/opensource/xterm16/xterm16-doc.html

Copy and Paste

Copying and pasting within and between xterm(1)s is easy.

Copy with the mouse

Depress the left mouse button at the beginning of your selection. As you drag the mouse over the text you want selected, the text is highlighted. By default, releasing the mouse button copies the selected text to the X clipboard. To select a word, click twice on it; to select a line, click three times on it. To extend a marked region, click the right mouse at the new end.

Paste with the mouse

Move the cursor to the xterm(1) window where you want the text to be copied and then depress the middle button (or both buttons on two-mouse systems). Note that In order for the vi(1) editor to accept your paste, the editor must be in insert mode.

Note that there is is a limit on the amount of text that can be copied, especially in older versions of xterm(1). Too large of a paste to input also hangs older xterm(1) windows. In general, if you keep below 9080 characters you should be OK.

The complicated stuff

Surprisingly, cut and paste operations are a complex topic, especially if you are cutting and pasting between an xterm(1) and non-xterm(1) windows. If you are interested, you can pursue some of the following information...

By default, your selections are in CUT_BUFFER0. There are other buffers you can use. There are 10 cut buffers, CUT_BUFFER[0-7], and special buffers PRIMARY and SECONDARY. You can program different keys and mouse combinations to operate on all these buffers using key maps and translations in your XResources file. Most other applications use the PRIMARY buffer instead of CUT_BUFFER0.

If you have the programs wxpaste(1), xclip(1), xcutsel(1), xprop(), wxcopy(1), and xclipboard(1) read their man pages.

paste buffer filters

If you have wxpaste(1) and wxcopy(1), then you can use commands to read and write the paste buffer. This can be very useful. If you don't have wxpaste(1) try the pb(1) script. xclip(1) is an alternative to wxcopy(1) and wxpaste(1). Here are a few ideas:

pb|mail -s 'Paste Buffer' $USER          # mail yourself the marked text
pb > /tmp/pb.$$;xterm -e vi /tmp/pb.$$   # edit the paste buffer in a new xterm
pb >> $HOME/pb.txt                       # append marked text to a file
pb|xargs command
#NB: Because of a bug in xprop(X11), pb is only good up to 10,000 characters.

Commands like these samples are especially handy when mapped to a key or menu item of your window manager.

defining a word for Cut & Paste

Which characters are considered to build a single word when doing double-click Cut&Paste?

Basically, every character is assigned to an arbitrary numeric "class". The man(1) page goes into this in great detail, but the format of each class specification is values:class, where "values" is a comma-delimited list of ADE(ASCII Decimal Equivalent) values or ranges (in the form small-large). The default class alphameric letters are assigned to is "48". Most other letters are assigned to a class equal to their ADE; so that each letter is in it's own "class".

So If I wanted to make the list of characters

! # $ % + - . / @ ~

be treated the same as the alphameric characters, I would look up their ADE values:

! 33
# 35
$ 36
% 37
+ 43
- 45
. 46
/ 47
@ 64
~ 126

and then set them all to class "48" using the appropriate XResource

xrdb -merge <<EOF
! I don't want these to separate 'words':   ! # $ % + - . / @ ~
XTerm.VT100.charClass: 33:48,35-37:48,43:48,45-47:48,64:48,126:48
! ASCII DECIMAL EQUIVALENTS
! *-------*-------*-------*-------*-------*-------*-------*-------*
! | 00 nul| 01 soh| 02 stx| 03 etx| 04 eot| 05 enq| 06 ack| 07 bel|
! | 08 bs | 09 ht | 10 nl | 11 vt | 12 np | 13 cr | 14 so | 15 si |
! | 16 dle| 17 dc1| 18 dc2| 19 dc3| 20 dc4| 21 nak| 22 syn| 23 etb|
! | 24 can| 25 em | 26 sub| 27 esc| 28 fs | 29 gs | 30 rs | 31 us |
! | 32 sp | 33  ! | 34  " | 35  # | 36  $ | 37  % | 38  & | 39  ' |
! | 40  ( | 41  ) | 42  * | 43  + | 44  , | 45  - | 46  . | 47  / |
! | 48  0 | 49  1 | 50  2 | 51  3 | 52  4 | 53  5 | 54  6 | 55  7 |
! | 56  8 | 57  9 | 58  : | 59  ; | 60  < | 61  = | 62  > | 63  ? |
! | 64  @ | 65  A | 66  B | 67  C | 68  D | 69  E | 70  F | 71  G |
! | 72  H | 73  I | 74  J | 75  K | 76  L | 77  M | 78  N | 79  O |
! | 80  P | 81  Q | 82  R | 83  S | 84  T | 85  U | 86  V | 87  W |
! | 88  X | 89  Y | 90  Z | 91  [ | 92  \ | 93  ] | 94  ^ | 95  _ |
! | 96  ` | 97  a | 98  b | 99  c |100  d |101  e |102  f |103  g |
! |104  h |105  i |106  j |107  k |108  l |109  m |110  n |111  o |
! |112  p |113  q |114  r |115  s |116  t |117  u |118  v |119  w |
! |120  x |121  y |122  z |123  { |124  | |125  } |126  ~ |127 del|
! *-------*-------*-------*-------*-------*-------*-------*-------*

Whether a word can span lines or not when wrapped depends. It only works if xterm is wrapping the line itself; which is usually controlled by the application doing the writing and not the user.


Backspace and Delete

There are several methods that alter the behavior of the backspace key. This can become complicated...

The two keys that often do not work as desired are the backspace and/or delete keys. The backspace character (ASCII char BS) is 08 in decimal, 0x08 in hex. It is typically generated by pressing the H key while holding down the "ctrl" key; so it is often represented by the string'^H'. The delete character (ASCII char DEL) is 127 in decimal, 0x7f in hex, and often represented by '^?'.

On most Unix and GNU/Linux platforms, the following script should work:

#!/bin/sh
exec xterm -xrm '
*ttyModes:         erase ^h kill ^x
XTerm.VT100.backarrowKey   true
XTerm.backarrowKeyIsErase  false
*VT100.Translations: #override \
   None<KeyPress>Delete:    string(0x08)\n\
   None<KeyPress>BackSpace: string(0x7f)\n
'

If that produces the results you want, consider adding the resource strings to your $HOME/.Xdefaults or $HOME/.Xresources file, or using the script xterm_Xresources in your X11 startup scripts.

You can actually program a key to redefine other keys, so you could set a function key that changes what the backspace key transmits, as this might vary depending on what type of system you are logged into or what program (emacs(1), vim(1), vi(1), ...) you might be using.

See also:

Using stty(1) to view and change the erase variable. stty(1) is a tool for changing and viewing the terminal settings.

stty -a # see what erase is set to 
stty erase '^H'
stty erase ^?

tset (used for setting terminal settings) will also work:

tset -e ^?

Wish List

It would be a great boon to standard Unix shells (and Fortran programs, for instance) if there were a better graphics interface to an xterm than a Tektronix 4014 interface. Color, line thickness, and polygon fill and pixel addressing would be very handy. Some other terminal emulators (rxvt(1), ...) provide some support; but these are not as commonly available by default as an xterm(1). If you are desperate, you can use colored letters and the "unreadable" font to create low-resolution pixmap graphics.

It would be very handy to have an easy way to dump the scroll history to standard output so that it could be easily processed by scripts.

This isn't really an xterm(1) issue per-se, but more a comment on the state of terminal-based CLI (command-line interfaces) since the advent of X11 Windows... A friendlier screen interface for simple programs would be great. Does anyone remember the TDU panel facility and CCL on CDC NOS machines? All command line scripts written in CCL had an automatic screen mode interface available! You could make a screen panel interface for a program in practically any compiled language in minutes. Or what about Rexx? The closest thing I know of years later on Unix variants is lynx(1) when it is recompiled with "Simulated CGI support".


References

The home page and additional references can be found with a wikipedia xterm search.

SEE ALSO:

stty(1), xterm(1), resize(1), tset(1), terminfo(5), termcap(5), xrdb(1), xfontsel(1), xlsfonts(1), X(1), xmodmap(1), xkeycaps(1), editres(1), appres(1), xprop(1), ncurses(1), curses(1), xev(1), xcutsel(1), wxpaste(1), wxcopy(1), xclip(1), xmakemap(1), clear(1), reset(1)

# to dump out your current in-memory X11 resource customizations
xrdb -query
# to find out interesting things about your X11 display
xrdb -symbols
# see options used to start the window, it's X11 name, 
# the current title and icon name, and which virtual screen
# you are on and whether you are iconified or not ...
xprop -id $WINDOW_ID
# to get the names of keys for making translation tables
xev&
#
appres XTerm xterm
# make your programs aware of your screen size ( most of the time)
eval `resize`

Author: John S. Urban; Copyright 1991, 2008 (Last updated Mar 31, 2008)
FODDER

tcsh and cwdcmd

      Tcsh supports a special alias, "cwdcmd", which if set holds a
      command that will be executed after changing the value of $cwd.  For
      example, if the user is running on an X window system xterm(1), and a
      user enters:

            alias cwdcmd  'esc -nt'

      then the shell will change the title of the running xterm(1) to be the
      full current working directory, an "at" (@) character, and the hostname.

Seeing the resources

APPRES(1)										  APPRES(1)



NAME
       appres - list X application resource database

SYNOPSIS
       appres [[class [instance]] [-1] [toolkitoptions]

DESCRIPTION
       The appres program prints the resources seen by an application (or subhierarchy of an appli-
       cation) with the specified class and instance names.  It can  be	 used  to  determine  which
       resources a particular program will load.  For example,

	    % appres  XTerm

       will list the resources that any xterm program will load.  If no application class is speci-
       fied, the class -AppResTest- is used.

       To match a particular instance name, specify an instance name  explicitly  after	 the  class
       name, or use the normal Xt toolkit option.  For example,

	    % appres  XTerm  myxterm
       or
	    % appres  XTerm  -name  myxterm

       To  list	 resources  that match a subhierarchy of an application, specify hierarchical class
       and instance names.  The number of class and instance components	 must  be  equal,  and	the
       instance name should not be specified with a toolkit option.  For example,

	    % appres  Xman.TopLevelShell.Form  xman.topBox.form

       will  list  the	resources  of widgets of xman topBox hierarchy.	 To list just the resources
       matching a specific level in the hierarchy, use the -1 option.  For example,

	    % appres  XTerm.VT100  xterm.vt100	-1

       will list the resources matching the xterm vt100 widget.

SEE ALSO
       X(7), xrdb(1), listres(1)

AUTHOR
       Jim Fulton, MIT X Consortium



X Version 11				XorgManVersionString				  APPRES(1)

#cat -v -e -t
# ctrl-v in vi(1) to insert control characters
# scroll buffer is probably biggest memory hog when set large
# ADD DIRECTORY TO PATH
# Assuming you created the directory in your home directory
PATH=$PATH:$HOME/scripts_xterm;export PATH # sh/ksh/pdksh/bash
#  or
set path=($path $home/scripts_xterm)  # csh/tcsh
#######################
# change colors and size and font size
esc -bg gray -fg yellow -rows 40 -columns 132 -fn 5
full 10 # make xterm(1) full screen using 10pt font
fullback # go back to screen size before full(1) command

xrdb -query > HOLD.x11 # snapshot X11 resources
xterm_Xresources  # set up X11 xresources
xterm & # hit the keys alt-5  in the new window
xterm -name VMS &
xrdb -merge < HOLD.x11  # restore X11 resources

xterm -e ftp&
# prompt for a filename
xterm -g 80x48-0+0 -bg red \
 -e csh -fc 'echo -n Name of file to edit: ; set F = ($<); vi $F;sleep 2' &