Change directory cd to another terminal's cwd

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.

physicsmichael

Posted 2009-10-12T01:55:11.933

Reputation: 918

Answers

4

Aliases don't accept parameters. You'll have to use a function. You should also use the command builtin.

function cd () { command cd "$@"; echo "$PWD" > /tmp/CWD; }

alias cdp='cd "$(</tmp/CWD)"'

See this for another approach that's specific to OS X. It's a script that can launch a new Terminal window or tab opening with its current directory the same as that of the current Terminal window or tab.

Paused until further notice.

Posted 2009-10-12T01:55:11.933

Reputation: 86 075

That will break with spaces in the filename, won't it? You'd need cdp='cd "$(</tmp/CWD)'". Or set your cd override to ln -sf "$PWD" /tmp/CDW, and cdp='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 cd, instead of PROMPT_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

– Peter Cordes – 2015-02-06T17:27:11.520

0

I like the solution at http://hints.macworld.com/article.php?story=20051231110014263 better:

Open new xterm windows in current Terminal directory Authored by: TomP on Jan 04, '06 10:05:12PM

As a slightly more flexible alternative, I have a little shell script ("openterminal.sh") that will open a new Terminal window in the same directory as the Terminal session from which it was invoked. Here's the script:

#!/bin/sh
# 
# Open another terminal window for the current directory
#
# Copyright 2004 by Tom Pollard - All rights reserved.
#
#set -x
CWD=`pwd`
osascript<<END
set thePath to "$CWD"
set myPath to (POSIX file thePath as alias)
try
    tell application "Terminal"
        activate
        do script with command "cd \"" & thePath & "\""
    end tell
end try
END

I have this aliased as 'ot'. So, when I want another Terminal window open to the same directory as some other Terminal window (not necessarily the last one I used or opened, I just say 'ot' in that Terminal session.

mdoar

Posted 2009-10-12T01:55:11.933

Reputation: 100