14.06.2014 Views

Unix Power Tools

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

4.8<br />

in the status area or window title, depending on which terminal type I’m using.<br />

Of course, you can change the format of the status line. Change the following<br />

command string, ${host:h}:${cwd}, to do what you need; see your shell’s manual<br />

page for a list of variables, or create your own custom information.<br />

set e=`echo x | tr x '\033'`<br />

# Make an ESCape character<br />

set g=`echo x | tr x '\07'` # And a Ctrl-g<br />

stattitle.csh<br />

stattitle.sh set host=`uname -n`<br />

# Puts $host and $cwd in VT102 status line. Escape sequences are:<br />

# ${e}7 = save cursor position, ${e}[25;1f = go to start of status<br />

# line (line 25), ${e}[0K = erase line, ${e}8 = restore cursor<br />

:h 28.5 alias setstatline 'echo -n "${e}7${e}[25;1f${e}[0K ${host:h}:${cwd}${e}8"'<br />

alias settitle 'echo -n "${e}]2;${host:h}:${cwd}${g}"'<br />

switch ($TERM)<br />

case vt10?:<br />

&& 35.14 alias cd 'cd \!* && setstatline'<br />

alias pushd 'pushd \!* && setstatline'<br />

alias popd 'popd \!* && setstatline'<br />

breaksw<br />

case xterm*:<br />

alias cd 'cd \!* && settitle'<br />

alias pushd 'pushd \!* && settitle'<br />

alias popd 'popd \!* && settitle'<br />

breaksw<br />

endsw<br />

(Article 5.15 has more about how this works in xterms.)<br />

The ESC and CTRL-g characters are stored with a trick that should work on all<br />

<strong>Unix</strong> shells. Most modern echos will let you make a nonprintable character<br />

directly, like this: g='echo '\07''.<br />

If you always use a VT102-type terminal (and many people do), the setstatline<br />

alias will work fine. If you use a different terminal, try it anyway! Otherwise,<br />

read the terminal manual or its termcap/terminfo entry and find the escape<br />

sequences that work for it; then add a new case to the switch statement.<br />

Note that you might have some trouble here: if this code is in your .cshrc file but<br />

your terminal type is set in your .login file, the terminal type may not be set until<br />

after the alias has been read. There are workarounds (3.8).<br />

The status line or titlebar can also get out of sync with reality if you use remote<br />

logins (1.21), subshells (24.4), etc. These might make a new status line or titlebar<br />

but not reset the original one when needed. To fix this, just type setstatline or<br />

settitle at a shell prompt. Or, if you don’t want to bother to think of the name of<br />

the alias, use the following command to change to the current directory (.),<br />

which will use the correct alias and reset the status or title:<br />

% cd .<br />

78 Part II: Customizing Your Environment<br />

This is the Title of the Book, eMatter Edition<br />

Copyright © 2009 O’Reilly & Associates, Inc. All rights reserved.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!