MacOS – How to get the terminals in terminal.app to go to the right directory after restart with tcsh

macosresumeterminal

The terminal.app keeps the tabs after a restart and it even shows the content of the buffers. But all open tabs are at the home directory rather than the working directory they were in at shutdown. I am using tcsh and I learned that this is the problem. It works for bash and Chris Page posted something for zsh, but I do not know how to adapt this to tcsh.

Best Answer

Add this to ~/.cshrc:

if ( "$TERM_PROGRAM" == Apple_Terminal ) then
    alias precmd 'printf "\033]7;file://"; hostname | tr -d "\n"; printf %s "$PWD" | xxd -p | sed -E "s|(..)|%\1|g;s|%2f|/|g"; printf "\a"'
endif

http://www.stairways.com/blog/2011-08-07-tcsh-remembering-path uses Perl, doesn't escape characters like % or ä, and doesn't ignore iTerm.