Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

Click here to return to the 'Use 'precmd' and 'postcmd' in tcsh' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Use 'precmd' and 'postcmd' in tcsh
Authored by: Viridian on May 24, '02 09:29:02PM

robg has stated his preference for an alias that puts the current job in the title bar using the 'postcmd' variable, but when I put directory info in the alias, I noticed that when I changed directories, it would display the directory I had just left, not the one I was in, until I entered another command or pressed Return. After much experimentation, I found that setting both the 'precmd' AND 'postcmd' variables did what I wanted.

alias precmd 'echo -n "^[]0;"`date "+%H:%M:%S"`" ${cwd}^G"'
sched +0:00 alias postcmd 'echo -n "^[]0;`date "+%H:%M:%S"` ${cwd} => \!#^G"'

[Note: there are three spaces for separation after the back-quoted 'date' command in both aliases.]
They both echo the current time (as a time stamp) and working directory, but the 'postcmd' alias also echoes the current job. Here's an example of what they do:

When a Terminal window is opened, the title bar displays the time and home directory:

19:04:21 /Users/<username>

Navigate to ~/Library/init/tcsh:

19:07:41 /Users/<username>/Library/init/tcsh

Edit aliases.mine:

19:11:32 /Users/<username>/Library/init/tcsh => pico aliases.mine

What's interesting is that all current processes are displayed in the title bar while they are being executed, so on a slow Mac you can make out entire scripts flashing by before your eyes. Talk about dynamic!

On an unrelated note (well, it's about aliases so it's somewhat related), I also use the 'cwdcmd' special variable, which executes on a directory change. I set it to display a brief message and produce a color listing of the directory contents.

Here's how: put the following Perl script (mine is named "cwdmessage") in ~/bin and set its permissions to executable.

#! /usr/bin/env perl

print ("\n\033[37;100mCURRENT DIRECTORY:\033[0m ") ;

Then add these lines to ~/Library/init/tcsh/aliases.mine

alias lx 'ls -F --color' # Assuming you have 'color ls' installed.
alias lc 'ls -GAhlk --color | more' # 'man ls' for options.
alias cwdcmd 'cwdmessage ; cwd ; ls --color' # Put your preferred 'ls' alias here.

Why did I use a Perl script instead of 'echo' ? Because I discovered by accident (complete newbie here) that Perl recognizes ANSI color tags, so I set the message to a different color than normal text. When I change directory, a line is skipped and the message "CURRENT DIRECTORY:"(in white on a dark grey background) is displayed, followed by the directory name and a color listing of its contents.
See my tip about ANSI color codes.

The tcsh man pages are a wealth of information about special variables, and much, much else.[Tip: in the man page type '/' to enter search mode and enter the string you want. Press ctrl-n to skip to the next occurence.]

[ Reply to This | # ]
Use 'precmd' and 'postcmd' in tcsh
Authored by: liyanage on May 27, '02 03:44:22PM

Wow this stuff is awesome, exactly what I always wanted!!

I removed the date as I don't need that, but I prefer to see the "~" sign in place of my home directory in the path as that makes the string a bit shorter. So I changed the line to:

alias precmd 'echo -n "^[]0;${cwd}^G" | sed -e "s%$HOME%~%"'

I noticed however that sometimes newly opened terminal windows close again immediately. When that happens, I have to hit Cmd-n again, sometimes twice or three times... Anybode else seen this?

[ Reply to This | # ]