1

There are various paths I need to set whenever I cd into a certain directory. I'm hesitant to just set them in my .cshrc file, because I may need to point them somewhere else if I'm working in a different dir. Is there a way to set things up so my paths automatically get set when I cd into a specified directory?

JETM
  • 139
  • 1
  • 8

1 Answers1

2

You can use the special cwdcmd alias. From tcsh(1):

   cwdcmd  Runs after every change of working directory.  For example,  if
           the  user is working on an X window system using xterm(1) and a
           re-parenting window manager that supports title  bars  such  as
           twm(1) and does

               > alias cwdcmd  'echo -n "^[]2;${HOST}:$cwd ^G"'

           then the shell will change the title of the running xterm(1) to
           be the name of the host, a colon, and the full current  working
           directory.  A fancier way to do that is

               >          alias          cwdcmd          'echo          -n
               "^[]2;${HOST}:$cwd^G^[]1;${HOST}^G"'

           This will put the hostname and working directory on  the  title
           bar but only the hostname in the icon manager menu.

           Note  that  putting  a cd, pushd or popd in cwdcmd may cause an
           infinite loop.  It is the author's opinion that anyone doing so
           will get what they deserve.

I would recommend doing something like this in your ~/.tcshrc:

set basepath = (/sbin /bin /usr/sbin /usr/bin)
set path = ($basepath)

alias cwdcmd source ~/.tcsh/cwdcmd.tcsh

# Do this on startup as well
cwdcmd

And then in ~/.tcsh/cwdcmd.tcsh:

# Reset path
set path = ($basepath)

# cwd is exactly this
if ( "$cwd" == "/home/martin/code" ) then
    set path = ($path /code-path)
# cwd starts with this
else if ( "$cwd" =~ "/home/martin/tmp*" ) then
    set path = ($path /tmp-path)
endif
Martin Tournoij
  • 1,715
  • 2
  • 15
  • 35