配置 cshrc 以设置特定目录的路径

配置 cshrc 以设置特定目录的路径

每当我进入某个目录时,我都需要设置各种路径。我犹豫是否将它们设置在我的 .cshrc 文件中,因为如果我在不同的目录中工作,我可能需要将它们指向其他地方。有没有办法进行设置,以便当我 cd 到指定目录时自动设置我的路径?

答案1

您可以使用特殊cwdcmd别名。从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.

我建议在你的~/.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

然后在~/.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

相关内容