5
I would like to create a command like cd -
(lets call it cdp
) that will change directories to the last changed-to directory from another terminal window similar to the option to open a new terminal in the directory that previous window/tab was in (I don't see that option in Mac OS X terminal).
To do so I figure I could alter cd
with something like alias cd='cd $1;echo $PWD > /tmp/CWD'
and then add
alias cdp='cd `cat /tmp/CWD`
Can someone key in a better solution? Or, fill me in on an existing program, feature, etc., please let me know. On Mac OS X 10.6 with default terminal. Thanks.
That will break with spaces in the filename, won't it? You'd need
cdp='cd "$(</tmp/CWD)'"
. Or set your cd override toln -sf "$PWD" /tmp/CDW
, andcdp='cd -P /tmp/CWD'
. This is still clunky and only works for one directory, even if you have multiple terminals CDed to multiple places. (most recent cd wins, not most-recently-used terminal). I guess Mac OS's Terminal app doesn't have an option to open new tabs / windows with the same CWD as the child process in the currently-focused terminal, like gnome-terminal does. – Peter Cordes – 2015-02-06T13:34:10.780@PeterCordes: Thanks for spotting that omission. – Paused until further notice. – 2015-02-06T15:29:45.880
Hooking
– Peter Cordes – 2015-02-06T17:27:11.520cd
, instead ofPROMPT_COMMAND='pwd > /tmp/"CWD.$USER"'
is a nice idea, though. I used it for http://superuser.com/questions/874574/how-do-you-cd-to-the-directory-of-another-screen-window/874575