Tcsh

配置 cshrc 以設置特定目錄的路徑

  • January 27, 2016

每當我 cd 進入某個目錄時,我都需要設置各種路​​徑。我很猶豫是否將它們設置在我的 .cshrc 文件中,因為如果我在不同的目錄中工作,我可能需要將它們指向其他地方。有沒有辦法進行設置,以便在我 cd 進入指定目錄時自動設置我的路徑?

您可以使用特殊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

引用自:https://unix.stackexchange.com/questions/258024