Main features of TCSH

## Features of the TCSH Shell

> ## Abstract

> In this article, I will describe some of the main features of TCSH, which I believe makes it worth using as the primary login shell. This article is not meant to persuade bash users to change! I've never used bash, and by that reason I know very little about it.

> As some of you surely know, I've created a configuration tool called The Dotfile Generator, which can configure TCSH. I believe that this tool is very handy when one wants to get the most out of TCSH (without reading the manual page a couple of times.) Because of that I'll refer to this tool several times throughout this article to show how it can be used to set up TCSH.

## Why is the shell so important?

The shell is your interface to executing program, managing files and directories etc. Though very few people are aware of it, one uses the shell very much in the daily work. E.g. completing file names, using history substitution and aliases. The TCSH shell offers all of these features and a few more, which the average user very seldom takes advantages of.

With a high knowledge of your shell's power, you may decrease the time you need to spend in the shell, and increase the time spent on the original tasks

## Command line completions

An important feature that is used by almost all users of a shell is the command line completion. With this feature you don't need to type all the letters of a filename, but only the ambiguous ones. This means that if you wish to edit a file called `file.txt`, you may only need to type `fi` and hit the TAB key, then the shell will type the rest of the filename for you.

Basically one can complete on files and directories. This means that you can not complete on host names, process id's, options for a given program etc. Another thing you can not do with this type of completion is to complete on directory names only, when typing the argument for the command `cd`

In TCSH, the completion mechanism is enhanced so that it is possible to tell TCSH which list to complete from for each command. This means that you can tell TCSH to complete from a list of host names when completing on the commands `rlogin` and `ping`. An alternative is to tell it to complete only on directories when the command is `cd`.

To configure user defined completion with The Dotfile Generator (from now on called TDG) go to the page `completion` -> `userdefined`, this will bring up a page which looks like this:

As the command name, you tell TDG which command you wish to define a completion for. In this example it is `rm`. Next you have to tell TDG which arguments to the command, this completion should apply to. To do this, press the button labeled `Position definition`. This will bring up a page, which is split in two parts:

| In the first part, you tell TDG, that the position definition, should be defined from the index of the argument, which is trying to be completed (the one, where the tab key is pressed.) Here you can tell it that you wish to complete on the first argument, all the arguments except the first one etc.
| The alternative to position dependent completion is pattern dependent completion. This means that you can tell TDG, that this completion should only apply if the current word, the previous word or the word before the previous word conform with a given pattern.

Now you have to tell the TDG which list to complete from. To do this press the button labeled `List`. This will bring up a page, where you can select from a lot of different lists. E.g. aliases, user names, or directories.

### Files and Directories

Four of the lists you can select from are `Commands, Directories, File names` and `Text files`. If you give the optional directory to any of these, only elements from this directory is used.

### Predefined Lists

There are two ways to let completion be from a predefined list. One is to mark the option `predefined list`, and type all the options in this list.

This solution is a bad idea if the list is used several places (e.g. a list of host names) in that case, one should select the list to be located in a variable, and then set this variable in the `.tcshrc` file.

### Output from command

In many cases the list should be calculated when the completion takes place. This could e.g. be a list of users located at a given host, or targets in a makefile.

To set up such a completion, first develop the command, which return the list to complete from. The command must return the completion list on standard output as a space separated list. When this is done, insert this command in the entry saying `Output From Command`.

Here's a little Perl command, which find the targets in a makefile:

    perl -ne 'if (/^([^.#][^:]+):/) {print "$1 "}' Makefile

If this is inserted in the Entry, one can complete on targets from the file called Makefile, in the current working directory.

If someone should think that its only to promote TDG, that I describe TCSH through it, (s)he should take a look at the following line, which is the generated code for the make completion:

    complete make  'p@*@`perl -ne '"'"'if (/^([^.#][^:]+):/) {print "$1"}'"'"'Makefile`@'

### Restrict to pattern

With user defined completion, you can restrict the files, which are matched, for each command. Here are some very useful examples:

Restrict latex to *.{tex,dtx,ins}
  The latex command will only complete on files ending in `.tex`, `.dtx` or `.ins`
Restrict rm to ^*.{tex,html,c,h}
  This means that you can not complete rm to a `.tex`, `.html`, `.c` or `.h` file!
  I've done that a few times, when I e.g. wanted to delete a file called important.c~. Since the file important.c existed tcsh only completed to that name, and.. I deleted the wrong file, because I was to quick :-(

### Additional examples

Additional examples can be obtained from TDG, if you load the export file distributed with TDG. Please note that if you wish to keep the other pages, you have to tell TDG only to import the page `completion/userdefined`. This is done on the `Details` page, which is accessible from the reload page.

## Configuring the prompt

Configuring the prompt is very easy with TDG. Just enter the menu called `prompt`. On this page you can configure three prompts:

prompt
  This is the usual prompt, which you see on the command line, where you are about to enter a command.
prompt2
  This prompt is used in `foreach`, and `while` loops, and at lines continuing lines ended with a slash.
prompt3
  This prompt is used when TCSH tries to help you, when it meet commands it doesn't know (called spell checking.)

The prompts are mixed with tokens and ordinary text. The tokens are inserted by clicking on them in the menu below the scrollbar, and the ordinary text is simply typed in. When a token is inserted an indication will be shown in the entry. Here's an example of how this may look:

As has been discussed in issue 6 of the Gazette, some of the prompt may be located in the xterm title bar instead of on the command line. To do this, choose `font change` and select `Xterm`.

## History

The history mechanism of the shell is a valuable thing, which makes it easier to type similar commands after each other.

To see a list of the previously executed commands, type `history`.

The following table lists the event specifiers:

!n | This refers to the history event, with index `n`
!-n | This refers to the history event, which was executed, `n` times ago: `!-1` for the previous command, `!-2` for the one before the previous command etc.
!! | This refers to the previous command
!# | This refers to the current command
!s | This refers to the most recent command, whose first word begins with the string `s`
!?s? | This refers to the most recent command, which contain the sting `s`

With these commands, you can re-execute a command. E.g. just type `!!`, to re-execute the previous command. This is however often not what you want to do. What you really wants is to re-execute some part of a previous command, with some new elements added. To do this, you can use one of the following word designators, which is appended to the event specifier, with a colon.

0 | The first word (i.e. the command name)
n | The `n`th word
$ | The last argument
% | The word matched by an `?s?` search
x-y | Argument range from x to y
* | All the arguments to the command (equal to ^-$)

Now it's possible to get the last argument from the previous command, by typing `!!:$`. You'll however often see that you very often refer to the previous command, so if no event specifier is given, the previous command is used. This means that instead of writing `!!:$`, you may only write `!$`.

More words designators exists, and it's even possible to edit the words with different commands. For more information about this and for more examples, please take a look into the tcsh manual

It is possible to expand the history references on the command line before you evaluate them by pressing `ESC-SPC` or `ESC-!` (This is: first the escape key, and next the space key or the `!` key). On some keyboards you may use the meta key instead of the escape key. I.e.`M-SPC` (One keystroke!)

## Patterns

Many operations in the shell often works on many files, e.g. all files ending with `.tex` or starting with `test-`. Tcsh has the opportunity to type all these files for you, with file patterns. The following list shows which possibilities there exists:

* | Match any number of characters
? | Match a single character
[...] | Match any single character in the list
[x-y] | Match any character within the range of characters from x to y
[^...] | Match elements, which does not match the list
{...} | This expands to all the words listed. There's no need that they match.
^... | `^` in the beginning of a pattern negates the pattern.

### Examples

match all files ending with .tex
  *.tex
match all files which does not end with .tex
  ^*.tex
match xxxabyy xxxcdeyy and xxxhifjyy
  xxx{ab,cde,hifj}yy
match all .c and .h files
  *.[ch] or *.{c,h}

### The shell expand patterns

An important thing to be aware of is that it is the shell, which expand the patterns, and not the programs, which is executed with the pattern.

An example of this is the program `mcopy` which copy files from disk. To copy all files, you may wish to use a star as in: `mcopy a:* /tmp`. This does however not work since the shell will try to expand the star, and since it can not find any files, which starts with `a:`, it will signal an error. So if you wish to send a star to the program, you have to escape the star: `mcopy a:\* .`

There exists two very useful key bindings, which can be used with patterns: The first is `C-xg`, which list all the files matching the pattern, without executing the command. The other is `C-x*`, which expand the star on the command line. This is especially useful if you e.g. wishes to delete all files ending in .c except `important.c`, `stable.c` and `another.c`. To create a pattern for this, might be very hard, so just use the pattern `*.c`. Then type `C-x*`, which will expand `*.c` to all you .c files. Now it's easy to remove the three files from the list

## Aliases

When using the shell one will soon recognize that certain commands are typed again and again. The one at top ten is surly `ls -la`, which list all files in a directory in long form.

TCSH has a mechanism to create aliases for commands. This means that you can create an alias for `ls -la` just called `la`.

Aliases may refer to the arguments of the command line. This means that you can create a command called pack, which take a directory name and pack the directory with tar and gz. etc. Aliases can often be a bit hard to create since one often wants history/variable references expanded at time of use, and not at the definition time. This has been done easier with TDG, so go to the page `aliases`, to define aliases. If you end up with an alias you can not define on this page, but in tcsh, please send me an email. For more information about aliases, see the tcsh manual

## Timing programs

Have you ever needed to know how long a program took to run, how much CPU it used etc?. If so, you may recognize the output from the tcsh built-in time command:

    0.020u 0.040s 0:00.11 54.5%     0+0k 0+0io 21pf+0w

Informative? Yes but... The gnu time command is a bit more understandable:

    0.01user 0.08system 0:00.32elapsed 28%CPU (0avgtext+0avgdata 0maxresident)k
    0inputs+0outputs (0major+0minor)pagefaults 0swaps

But still...

In TDG you can configure the output from the time command on the page called jobs. It looks like this:

As for the prompt, here's an entry once again for mixed tokens and and ordinary text. Remember, if there is something in TDG that you do not understand, help is available by pressing the right mouse button over the given widget.

## References

As you may have guessed, TDG and this article will help you a lot of the way to use TCSH, BUT you may need to read a bit more to get more out of TCSH, here's a few references:

- The Tcsh manual page
- The O'Reilly book on tcsh
- The Tcsh mailing list (send mail to listserv@mx.gw.com with body text `SUBscribe TCSH your name`)

Jesper Kjær Pedersen <blackie@imada.ou.dk>

##### Copyright © 1996, Jesper Kjær Pedersen
Published in Issue 12 of the Linux Gazette