Chapter 14. Keeping Track of Where You Are

In This chapter:

  • Types of Location Reporting

  • Displaying Your Location in the Prompt

  • Display Your Location in the Window Title

  • Putting It All Together

  • Displaying Other Types of Information

When you’re actively working in a directory, you can usually remember your current location. However:

  • It’s easy to forget where you are when you’re interrupted by a phone call, take a coffee break, or pause to discuss a project with a coworker.

  • When you work in a multiple-window environment that allows you to maintain several simultaneous shell sessions, it can be difficult to remember which directory window is current.

  • If you have accounts on multiple systems, remembering your location becomes more complicated because you must also consider the machine you’re logged in on.

One way to determine your location is to use the command line. pwd (“print working directory”) shows the name of your current directory, dirs prints the directory stack, and, on most machines, either hostname, uname –n, or who am i will show the name of the machine you’re logged into. However, there are better ways to get feedback on your location. This chapter discusses how to set up your ˜/.cshrc file to have the shell provide that information automatically, so you don’t have to waste time running commands to figure out where you are.

Types of Location Reporting

One method of getting location information is to have the shell announce directory changes when you use cd, pushd, or popd. pushd and popd print the directory stack automatically. To have cd do the same, alias it to your ˜/.cshrc file, like this:

alias cd 'cd !*;dirs'

An alternative strategy arranges for continuous location reporting. This strategy lets you determine your location instantly, whether or not you change your directory. Continuous reporting is particularly valuable in a multiple-session environment that involves a lot of switching between windows.

To have the shell produce a continuous display of your location, first decide where you want to have the information displayed. Your prompt is a good location. Another likely place, if you use a window system, is the titlebar of your terminal windows. Commands to produce each type of display are discussed in the following sections.

Because the point of displaying the current location is to keep you informed as you work at the command line, the commands in ˜/.cshrc that set up the display need to be executed only for interactive shells. You should place those commands inside the following construct, which determines whether or not the shell is interactive by examining the prompt shell variable:

if ($?prompt) then
    commands to set up location display
endif

prompt is given a default value only by interactive shells. Therefore, non-interactive shells (e.g., those started to run scripts, or to run shell escapes from within an editor or mailer) skip the commands inside the if-statement.

As you try the commands shown in this chapter, play close attention to the quote characters used in the examples. Some examples use as many as three kinds of quotes, and won’t work unless you type them exactly as shown.

Displaying Your Location in the Prompt

csh and tcsh differ in the facilities they provide for putting information in your prompt. First, I’ll show commands that work for either shell, but are intended primarily for csh users. Second, I’ll discuss tcsh’s special formatting facilities for the prompt string. For both shells, I’ll show some typical prompt settings. You can, of course, experiment to find a setting that better suits your preferences.

Using csh To Display Your Location

To add the hostname to your prompt, set the prompt variable in your ˜/.cshrc file as follows:

set prompt = "`hostname`% "

If a command other than hostname displays the machine name on your system, make the appropriate substitution.

To include the current directory name in your prompt, create a setprompt alias that sets the prompt using the current location. (setprompt uses the cwd variable, which always holds the pathname of your current working directory.) Then, alias the directory-changing commands (cd, pushd, popd ) so that they invoke setprompt. Add the following commands to your ˜/.cshrc file so that your prompt will be a % character preceded by the pathname of your current directory:

alias setprompt 'set prompt = "$cwd% "'
setprompt
alias cd 'cd !*;setprompt'
alias pushd 'pushd !*;setprompt'
alias popd 'popd !*;setprompt'

Note that setprompt is executed after it is defined. This is necessary to set the prompt to the proper initial value. Otherwise, the prompt would not be set until you changed directory for the first time.

If you prefer to display only the tail (last component) of the current directory’s pathname, change $cwd to $cwd:t in the definition of setprompt:

alias setprompt 'set prompt = "$cwd:t% "'

If you want both the hostname and the current directory displayed in your prompt, change the definition of setprompt to one of those shown below. Each definition displays the host name and a directory name, but the first definition displays the full pathname, whereas the second definition displays only the last component:

alias setprompt 'set prompt = "`hostname`:$cwd% "'
alias setprompt 'set prompt = "`hostname`:$cwd:t% "'

For the first definition, your prompt might look like the following example as you move around:

viper:/usr/staff/dubois% cd /usr/spool/mqueue
viper:/usr/spool/mqueue% cd ~/Projects/Programs/tokenscan
viper:/usr/staff/dubois/Projects/Programs/tokenscan%

The second definition produces a different prompt, like this:

viper:dubois% cd /usr/spool/mqueue
viper:mqueue% cd ~/Projects/Programs/tokenscan
viper:tokenscan%

Using a multiple-line prompt

If you are displaying a lot of location information in your prompt, such as the full pathname of your current directory, the prompt string can become so long that you have little room left to type on the current line. Or, you may find it distracting to have the prompt change its length whenever you change directory. One solution to such problems is to display location information on a line separate from the % character by embedding a newline character in the definition of setprompt. For example, if your setprompt alias looks like this:


alias setprompt 'set prompt = "$cwd% "'

then change it to this:

alias setprompt 'set prompt = "-- $cwd --\
% "'

After you complete the modification given above, your prompt will look like this:

-- /usr/staff/dubois --
% cd /usr/spool/mqueue
-- /usr/spool/mqueue --
% cd ~/Projects/Programs/tokenscan
-- /usr/staff/dubois/Projects/Programs/tokenscan --
%

Breaking the prompt into multiple lines gives you ample room to display information and gives you the entire terminal width for typing commands. The format also helps you to find the % character by keeping it at a constant distance from the edge of the screen, even when the directory name varies in length.

Note that I surrounded the directory name with dashes in the multiple-line prompt. You can use other characters, but you should always use something visually distinct so that the pathname line stands out from any output produced by the previous command.

Using tcsh To Display Your Location

If you use tcsh, you have access to special prompt variable formatting sequences expressly intended for putting location information in the prompt string. Consequently, although the commands shown in the previous section do work for tcsh, you can achieve the same goal without messing around with a bunch of aliases.

Some useful sequences for displaying the directory or machine name are:

%/

The full pathname of the current directory.

The current directory, using ˜ or ˜name abbreviations for the first part of the path (if possible). usually results in shorter prompts than %/.

%c

The last component of the current directory (everything after the last slash in the full pathname).

%cn

The last n components of the current directory, e.g., %c2 for the last 2 components. %cn abbreviates pathnames in a manner similar to that of .

%c0n

Like %cn, except that when the prompt is printed and the directory pathname has more than n components, the number of skipped components is indicated by a special prefix. For example, /3 indicates 3 skipped components. Alternatively, if you want skipped components to be indicated by a leading ellipsis ( … ), set the ellipsis shell variable in your ˜/.cshrc file as follows:

set ellipsis
%C, %Cn, %C0n

Like %c, %cn, and %c0n, but without the use of ˜ pathname abbreviation.

%m, %M

The first component of the machine, or the full name. For a name like xyz.corp.com, %m is xyz and %M is xyz.corp.com.

%%

A % character. % followed by a non-special character (like a space) also produces a %.

For example, to set your prompt so that it displays the current directory using ˜ notation, you need add only the following command to your ˜/.cshrc file:

set prompt = "%~% "

To set your prompt to host:dir%, where host is the first component of the hostname and dir is the current directory, use the following command:

set prompt = "%m:%~% "

If you want a multiple-line prompt, embed a newline in the prompt string. The previous set prompt command can be rewritten as follows:

set prompt = "-- %m:%~ --
% "

Display Your Location in the Window Title

If you use a window system, you can also display location information in the titlebar of your terminal windows. The following discussion explains how to do so for xterm (the X Window System terminal program) and gives some hints for adapting the xterm instructions to other programs.

Several short startup file command sequences are used in this section. If you don’t want to type them, you can retrieve them from the archive mentioned in Appendix C, Other Sources of Information. The archive also contains titlebar changing commands for programs other than those discussed here, as well as documentation for the xterm control sequences mentioned below.

Communicating with xterm

You can specify a window title as a command line argument when xterm starts up, or as a resource value in ˜/.Xdefaults, but neither method is satisfactory for changing the title on an ongoing basis as you move around the file system. However, xterm also understands certain control sequences, one of which sets the window title. This sequence can be sent to xterm at any time using echo, and the title can then be changed on demand. For BSD UNIX systems, the echo command to set the window titlebar to title is as follows:

echo -n "ESC]2;titleCTRL-G"

For versions of UNIX based on System V, the command is slightly different:

echo "ESC]2;titleCTRL-Gc"

Both commands suppress the newline that echo normally prints. (A newline is not part of the control sequence; unless you suppress it, xterm lets the newline pass through to your window, and you get an unwanted blank line.) The echo commands differ only in that BSD echo uses the –n flag to suppress the newline, whereas the System V echo uses c instead. You’ll need to choose the command that is appropriate for your system. If you don’t know which command to use, try the first and then, if you see –n in your window every time the titlebar changes, switch to the second command. The examples shown below use the BSD version; make the appropriate substitution as necessary.

Setting the Window Title in xterm

To set up the shell to report your location in the titlebar, arrange to send xterm a title-change sequence whenever a directory change occurs. You can do so by aliasing cd, pushd, and popd in the same way that we set up the prompt-changing commands for csh earlier in the chapter.

Create a file in your home directory named .settitle that contains the commands shown below:

alias settitle 'echo -n "ESC]2;$cwdCTRL-G"'
settitle
alias cd 'cd !*;settitle'
alias pushd 'pushd !*;settitle'
alias popd 'popd !*;settitle'

To type literal ESC and CTRL–G characters into the file using your editor, you may need to precede them with CTRL–V.

You can vary the settitle alias definition if you like. To display only the last component of your directory in the title, change the $cwd to $cwd:t in the echo command, as shown below:

alias settitle 'echo -n "ESC]2;$cwd:tCTRL-G"'

Alternatively, you could display the directory stack. Being able to see the entire stack at a glance is helpful for using pushd +n and for using =n notation to refer to stack entries in command arguments. To display the directory stack, use the following command:

alias settitle 'echo -n "ESC]2;'dirs'CTRL-G"'

After you create .settitle, add the following line to your ˜/.cshrc file:


if (xterm =~ $TERM) source ~/.settitle

xterm sets the TERM environment variable to xterm, which allows the shell to tell whether or not it is running in an xterm window. If an xterm window is active the commands in .settitle are executed; otherwise, the commands are skipped (e.g., if you are using an ASCII terminal or are logging in through a modem from your microcomputer at home).

Setting the Window Title in Other Terminal Programs

If you are using a terminal program similar to xterm, you may be able to set the window title by using a slightly modified version of the method outlined in the previous section. Some examples are discussed below. If the program you use to provide terminal windows is not covered, the principles may still be similar, although the details will vary. Consult your local documentation for more specific information.

HP-UX hpterm

The HP-UX terminal program hpterm uses a different sequence than xterm to set the window title. Unfortunately, part of that sequence is the length of the title string. tcsh has string length operators, but csh does not; additionally, you can more easily generate the sequence using the following short perl script:

#!/usr/local/bin/perl
printf "e&f0k%dD%s", length ($ARGV[0]), $ARGV[0] unless $#ARGV < 0;

If you name the script hptitle, you can use hptitle string in the settitle alias to set an hpterm window title.[25] Create a file called .settitle-hp in your home directory, that looks like this:

alias settitle 'hptitle "$cwd"'
settitle
alias cd 'cd !*;settitle'
alias pushd 'pushd !*;settitle'
alias popd 'popd !*;settitle'

Then, put the following line in ˜/.cshrc to process .settitle-hp when you start up an hpterm window:

if (hpterm =~ $TERM) source ~/.settitle-hp

If you use both xterm and hpterm, you can use a more extensive test to process the file that is appropriate for the window type:

if (xterm =~ $TERM) then
    source ~/.settitle
else if (hpterm =~ $TERM) then
    source ~/.settitle-hp
endif

NCSA Telnet for Macintosh

The xterm control sequence for setting the titlebar works with NCSA Telnet 2.6 for Macintosh if you select “Xterm sequences” in your terminal preferences dialog.[26] Unfortunately, NCSA Telnet does not set TERM, so you cannot test that variable to determine whether to execute .settitle automatically. To work around this problem, put an ncsa alias in your ˜/.cshrc file, in addition to the TERM test:

if (xterm =~ $TERM) source ~/.settitle
alias ncsa source ~/.settitle

If you are using NCSA Telnet, then you can tell the shell to execute .settitle by typing ncsa after logging in.

Putting It All Together

The following is a complete example for ˜/.cshrc that puts location information in your window titlebar when you run xterm or hpterm, and in your prompt otherwise:

if ($?prompt) then                         # verify that shell is interactive
    if (xterm =~ $TERM) then               # xterm is running
        source ~/.settitle
    else if (hpterm =~ $TERM) then         # hpterm is running
        source ~/.settitle-hp
    else                                   # xterm/hpterm are not running
        if ($?tcsh) then                   # shell is tcsh
            set prompt = "%~% "
        else                               # shell is csh
            alias setprompt 'set prompt = "$cwd:t% "'
            setprompt
            alias cd 'cd !*;setprompt'
            alias pushd 'pushd !*;setprompt'
            alias popd 'popd !*;setprompt'
        endif
        alias ncsa source ~/.settitle
    endif
endif

The outermost if-test ensures that only interactive shells process the inner commands. Those inner commands determine whether or not xterm or hpterm are running, and take action accordingly:

  • If xterm or hpterm are running, .settitle or .settitle-hp are executed to set up location display in the window title.

  • If neither xterm nor hpterm are running, location information is displayed in the prompt. This is done using tcsh’s prompt formatting sequences if the shell is tcsh, and with the prompt setting aliases otherwise. Also, the ncsa alias is defined in case you are connecting with NCSA Telnet, so that you can display location information in the window title by typing ncsa after logging in.

Displaying Other Types of Information

Your prompt or window title can be used to display other kinds of information, such as your username. You can also set the text of an xterm window’s icon so that useful information is displayed even when the window is iconified. This section discusses some of the possibilities.

Displaying the Username

If you have multiple accounts under different logins, you may find it useful to display the current username in your prompt or window title.

To display your name in the prompt with tcsh, just include %n in your prompt variable setting. The following example produces a prompt of name@host: dir%:

set prompt = "%n@%m:%~% "

To display the name in the prompt with csh, modify your setprompt alias to reference the appropriate environment variable (probably USER or LOGNAME, depending on what kind of system you have; we will use USER here). For example, to display the username and the last component of the directory name, use a definition of setprompt that looks like this:

alias setprompt 'set prompt = "${USER} $cwd:t% "'

To put the username in the window title, modify the settitle alias in your .settitle file. The following definition displays name@host followed by the directory stack:

alias settitle 'echo -n "ESC]2;${USER}@'hostname' 'dirs'CTRL-G"'

In tcsh, the hostname is available in the HOST environment variable. Referencing a variable is more efficient than running hostname, so the alias can be rewritten as follows:

alias settitle 'echo -n "ESC]2;${USER}@${HOST} `dirs`CTRL-G"'

Setting the Window Icon in xterm

The xterm control sequence to change a window’s icon text is similar to the one that sets the titlebar, as shown:

echo -n "ESC]1;stringCTRL-G"

Putting the echo command in an alias lets you set the icon text easily. The following seticon alias sets the icon to name@host :

alias seticon 'echo -n "ESC]1;${USER}@`hostname`CTRL-G"'

If you use tcsh, use the following seticon alias:

alias seticon 'echo -n "ESC]1;${USER}@${HOST}CTRL-G"'

If you want seticon to take an argument so that you can specify the title yourself, write the seticon alias, as shown:

alias seticon 'echo -n "ESC]1;!*CTRL-G"'


[25] Install the perl script in a directory that’s listed in your path variable, such as ˜/bin. You might need to change the first line of the script to reflect the location of perl on your system. The archive mentioned in Appendix C, Other Sources of Information, has a short C program that is equivalent and faster, if you’re up to compiling it.

[26] You get to this dialog by selecting Preferences/Terminals from the Edit menu.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset