home *** CD-ROM | disk | FTP | other *** search
/ Magazyn Enter 1999 June / enter_06_1999.iso / doc / HOWTO / mini / Xterm-Title < prev    next >
Text File  |  1998-10-14  |  11KB  |  362 lines

  1.  
  2.                       HOW TO CHANGE THE TITLE OF AN XTERM
  3.                                        
  4. Richard Lister, ric@giccs.georgetown.edu
  5.  
  6.    
  7.    
  8.    $Revision: 1.6 $, Last modified: Wed Jan 7 14:34:31 EST 1998
  9.    
  10.    
  11.      _________________________________________________________________
  12.    
  13.    _This document explains how to use escape sequences to dynamically
  14.    change window and icon titles of an xterm. Examples are given for
  15.    several shells, and the appendix gives escape sequences for some other
  16.    terminal types._
  17.      _________________________________________________________________
  18.    
  19.      * Where to find this document
  20.      * Static titles
  21.      * Dynamic titles
  22.      * Examples for different shells
  23.           + zsh
  24.           + bash
  25.           + tcsh
  26.           + csh
  27.           + ksh
  28.      * Appendix: escapes for other terminal types
  29.           + SGI wsh, xwsh and winterm
  30.           + CDE dtterm
  31.           + Sun cmdtool and shelltool
  32.           + Example
  33.      * Credits
  34.        
  35.    
  36.      _________________________________________________________________
  37.    
  38. Where to find this document
  39.  
  40.    
  41.    
  42.    This document is now part of the Linux HOWTO Index and can be found at
  43.    http://sunsite.unc.edu/LDP/HOWTO/mini/Xterm-Title.html.
  44.    
  45.    The latest version can always be found at
  46.    http://www.giccs.georgetown.edu/~ric/howto/Xterm-Title.html.
  47.    
  48.    
  49.      _________________________________________________________________
  50.    
  51. Static titles
  52.  
  53.    
  54.    
  55.    A static title may be set for any of the terminals xterm, color-xterm
  56.    or rxvt, by using the -T and -n switches:
  57.  
  58.   xterm -T "My XTerm's Title" -n "My XTerm's Icon Title"
  59.  
  60. Dynamic titles
  61.  
  62.    
  63.    
  64.    Many people find it useful to set the title of a terminal to reflect
  65.    dynamic information, such as the name of the host the user is logged
  66.    into, the current working directory, etc.
  67.    
  68.    This may be done by using XTerm escape sequences. The following
  69.    sequences are useful in this respect:
  70.  
  71.   ESC]0;_string_BEL    Set icon name and window title to _string_
  72.   ESC]1;_string_BEL    Set icon name to _string_
  73.   ESC]2;_string_BEL    Set window title to _string_
  74.  
  75.    where ESC is the _escape_ character (\033), and BEL is the _bell_
  76.    character (\007).
  77.    
  78.    _Note_: these sequences apply to most xterm derivatives, such as
  79.    nxterm, color-xterm and rxvt. Other terminal types often use different
  80.    escapes; see the appendix for examples. For the full list of xterm
  81.    escape sequences see the file ctlseq2.txt, which comes with the xterm
  82.    distribution, or xterm.seq, which comes with the rxvt distribution.
  83.    
  84.    These escapes really need to be applied every time the prompt changes.
  85.    This way the string is updated with every command you issue and can
  86.    keep track of information such as current working directory, username,
  87.    hostname, etc. Some shells provide special functions for this purpose,
  88.    some don't and we have to insert the title sequences directly into the
  89.    prompt string.
  90.    
  91.    
  92.      _________________________________________________________________
  93.    
  94. Examples for different shells
  95.  
  96.    
  97.    
  98.    In all the examples below we test the environment variable TERM to
  99.    make sure we only apply the escapes to xterms. We test for
  100.    TERM=xterm*; the wildcard is because some variants (such as rxvt) can
  101.    set TERM=xterm-color.
  102.    
  103.   zsh
  104.   
  105.    
  106.    
  107.    zsh provides some functions and expansions, which we will use:
  108.  
  109.   precmd ()   a function which is executed just before each prompt
  110.   chpwd ()    a function which is executed whenever the directory is changed
  111.   \e          escape sequence for escape (ESC)
  112.   \a          escape sequence for bell (BEL)
  113.   %n          expands to $USERNAME
  114.   %m          expands to hostname up to first '.'
  115.   %~          expands to directory, replacing $HOME with '~'
  116.  
  117.    There are many more expansions available: see 'man zshmisc'.
  118.    
  119.    Thus, the following inserted in ~/.zshrc will set the xterm title to
  120.    "_username_@_hostname_: _directory_" ...
  121.  
  122.   case $TERM in
  123.       xterm*)
  124.           precmd () {print -Pn "\e]0;%n@%m: %~\a"}
  125.           ;;
  126.   esac
  127.  
  128.    This could also be achieved by using chpwd() instead of precmd().
  129.    
  130.   bash
  131.   
  132.    
  133.    
  134.    bash supplies a variable PROMPT_COMMAND which contains a command to
  135.    execute before the prompt. This example (inserted in ~/.bashrc) sets
  136.    the title to "_username_@_hostname_: _directory_":
  137.  
  138.   PROMPT_COMMAND='echo -ne "\033]0;${USER}@${HOSTNAME}: ${PWD}\007"'
  139.  
  140.    where \033 is the character code for ESC, and \007 for BEL. Note that
  141.    the quoting is important here: variables are expanded in "...", and
  142.    not expanded in '...'. So PROMPT_COMMAND is set to an unexpanded
  143.    value, but the variables inside "..." are expanded when PROMPT_COMMAND
  144.    is used.
  145.    
  146.    However, PWD produces the full directory path. If we want to use the
  147.    '~' shorthand we need to embed the escape string in the prompt, which
  148.    allows us to take advantage of the following prompt expansions
  149.    provided by the shell:
  150.  
  151.   \u          expands to $USERNAME
  152.   \h          expands to hostname up to first '.'
  153.   \w          expands to directory, replacing $HOME with '~'
  154.   \[...\]     embeds a sequence of non-printing characters
  155.  
  156.    
  157.    
  158.    Thus, the following produces a prompt of "bash$ ", and an xterm title
  159.    of "_username_@_hostname_: _directory_" ...
  160.  
  161.   case $TERM in
  162.       xterm*)
  163.           PS1="\[\033]0;\u@\h: \w\007\]bash\$ "
  164.           ;;
  165.       *)
  166.           PS1="bash\$ "
  167.           ;;
  168.   esac
  169.  
  170.    Note the use of \[...\], which tells bash to ignore the non-printing
  171.    control characters when calculating the width of the prompt. Otherwise
  172.    line editing commands get confused while placing the cursor.
  173.    
  174.   tcsh
  175.   
  176.    
  177.    
  178.    tcsh has functions and expansions similar to those of zsh:
  179.  
  180.   precmd ()   a function which is executed just before each prompt
  181.   cwdcmd ()   a function which is executed whenever the directory is changed
  182.   %n          expands to username
  183.   %m          expands to hostname
  184.   %~          expands to directory, replacing $HOME with '~'
  185.  
  186.    
  187.    
  188.    Unfortunately, there is no equivalent to zsh's print command allowing
  189.    us to use prompt escapes in the title string, so the best we can do is
  190.    to use shell variables (in ~/.tcshrc):
  191.  
  192.   switch ($TERM)
  193.       case "xterm*":
  194.           alias precmd 'echo -n "\033]0;${HOST}:$cwd\007"'
  195.           breaksw
  196.   endsw
  197.  
  198.    but this gives the directory's full path instead of using '~'. Instead
  199.    you can insert the string in the prompt:
  200.  
  201.   switch ($TERM)
  202.       case "xterm*":
  203.           set prompt="%{\033]0;%n@%m:%~\007%}tcsh%% "
  204.           breaksw
  205.       default:
  206.           set prompt="tcsh%% "
  207.           breaksw
  208.   endsw
  209.  
  210.    which sets a prompt of "tcsh% ", and an xterm title and icon of
  211.    "_username_@_hostname_: _directory_". Note that the "%{...%}" must be
  212.    placed around escape sequences (and cannot be the last item in the
  213.    prompt: see 'man tcsh' for the details).
  214.    
  215.   csh
  216.   
  217.    
  218.    
  219.    This is very difficult indeed in csh, and we end up doing something
  220.    like the following (in ~/.cshrc):
  221.  
  222.   switch ($TERM)
  223.       case "xterm*":
  224.           set host=`hostname`
  225.           set user=`whoami`
  226.           alias cd 'cd \!*; set prompt="^[]0;${user}@${host}: ${cwd}^Gcsh% "'
  227.           breaksw
  228.       default:
  229.           set prompt='csh% '
  230.           breaksw
  231.   endsw
  232.  
  233.    where we have had to alias the cd command to do all the work of
  234.    setting the prompt. Note that the '^[' and '^G' in the prompt string
  235.    are single characters for ESC and BEL (can be entered in emacs using
  236.    'C-q ESC' and 'C-q C-g').
  237.    
  238.    I strongly recommend abandoning csh in favour of a more advanced
  239.    shell: zsh, bash or tcsh.
  240.    
  241.   ksh
  242.   
  243.    
  244.    
  245.    ksh provides little in the way of functions and expansions, so we have
  246.    to insert the escape string in the prompt to have it updated
  247.    dynamically. This example produces a title of "_username_@_hostname_:
  248.    _directory_" and a prompt of "ksh$ ".
  249.  
  250.   case $TERM in
  251.       xterm*)
  252.           HOST=`hostname`
  253.           PS1='^[]0;${USER}@${HOST}: ${PWD}^Gksh$ '
  254.           ;;
  255.       *)
  256.           PS1='ksh$ '
  257.           ;;
  258.   esac
  259.  
  260.    However, PWD produces the full directory path. We can remove the
  261.    prefix of $HOME/ from the directory using the ${...##...} construct.
  262.    We can also use ${...%%...} to truncate the hostname:
  263.  
  264.   HOST=`hostname`
  265.   HOST=${HOST%%.*}
  266.   PS1='^[]0;${USER}@${HOST}: ${PWD##${HOME}/}^Gksh$ '
  267.  
  268.    Note that the '^[' and '^G' in the prompt string are single characters
  269.    for ESC and BEL (can be entered in emacs using 'C-q ESC' and 'C-q
  270.    C-g').
  271.    
  272.    
  273.      _________________________________________________________________
  274.    
  275. Appendix: escapes for other terminal types
  276.  
  277.   SGI wsh, xwsh and winterm
  278.   
  279.    
  280.    
  281.    These terminals set TERM=iris-ansi and use the following escapes:
  282.  
  283.   ESC P 1 .y _string_ ESC \\        Set window title to _string_
  284.   ESC P 3 .y _string_ ESC \\        Set icon title to _string_
  285.  
  286.    For the full list of xwsh escapes see xwsh(1G).
  287.    
  288.   Sun cmdtool and shelltool
  289.   
  290.    
  291.    
  292.    cmdtool and shelltool both set TERM=sun-cmd and use the following
  293.    escapes:
  294.  
  295.   ESC ] l _string_ ESC \            Set window title to _string_
  296.   ESC ] L _string_ ESC \            Set icon title to _string_
  297.  
  298.    These are truly awful programs: use something else.
  299.    
  300.   CDE dtterm
  301.   
  302.    
  303.    
  304.    dtterm sets TERM=dtterm, and appears to recognise both the standard
  305.    xterm escape sequences and the Sun cmdtool sequences. (I've only
  306.    tested this on a Sun ... anyone have HP and DEC versions they can test
  307.    for me?).
  308.    
  309.   Example
  310.   
  311.    
  312.    
  313.    Here's my full setup using zsh to handle all these terminal types:
  314.  
  315.   case $TERM in
  316.  
  317.     xterm*|dtterm)
  318.       precmd () {print -Pn "\e]0;%n@%m: %~\a"}  ## window & icon title
  319.       ;;
  320.  
  321.     iris-ansi)
  322.       precmd () {
  323.         print -Pn "\eP1.y%n@%m: %~\e\\"         ## window title
  324.         print -Pn "\eP3.y%n@%m: %~\e\\"         ## icon title
  325.       }
  326.       ;;
  327.  
  328.     sun-cmd)
  329.       precmd () {
  330.         print -Pn "\e]l%n@%m: %~\e\\"           ## window title
  331.         print -Pn "\e]L%n@%m: %~\e\\"           ## icon title
  332.       }
  333.       ;;
  334.  
  335.   esac
  336.  
  337.    
  338.      _________________________________________________________________
  339.    
  340. Credits
  341.  
  342.    Thanks to the following people.
  343.    
  344.    Paul D. Smith <psmith@BayNetworks.COM> and Christophe Martin
  345.           <cmartin@ipnl.in2p3.fr>
  346.           both pointed out that I had the quotes the wrong way round in
  347.           the bash PROMPT_COMMAND. Getting them right means variables
  348.           _are_ expanded dynamically.
  349.           
  350.    Paul D. Smith <psmith@BayNetworks.COM>
  351.           suggested the use of \[...\] in the bash prompt for embedding
  352.           non-printing characters.
  353.           
  354.    Christophe Martin <cmartin@ipnl.in2p3.fr>
  355.           provided the solution for ksh.
  356.           
  357.    Keith Turner <keith@silvaco.com>
  358.           supplied the escape sequences for Sun cmdtool and shelltool.
  359.           
  360.    
  361.      _________________________________________________________________
  362.