Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

Open new xterm windows in current Terminal directory UNIX
UNIX / X11 users could always easily open a new xterm in the same directory as an existing xterm by typing xterm at the prompt. OS X's Terminal.app functions differently, making such a shortcut difficult. With some bash scripting, however, you can have Terminal open up each new window in the last directory you had a prompt in among your other open Terminal windows.

The trick is to add some shell code in your prompt string itself. This allows recording the current directory to a file whenever you get a new shell prompt. Then bash's default login and logout scripts take up the rest of the slack. To implement this hint, you need to edit three files: .bash_profile, .bashrc and .bash_logout.

In .bash_profile, add the following:
# nuke stale .last_cwd files.
cd
for I in .last_cwd_* ; do
  [ -r "$I" ] && ( kill -0 ${I#.last_cwd_} || rm $I ) 2> /dev/null
done

# find youngest viable last_cwd and change there
L="`ls -1tr .last_cwd_* 2> /dev/null | tail -1`"

[ ! -z "$L" -a -r "$L" ] && cd "`cat $L`"

# set up PS1
export PS1='h:w u$ `echo w > ~/.last_cwd_$$`'
In .bashrc, add the following:
# set up PS1
export PS1='h:w u$ `echo w > ~/.last_cwd_$$`'
And in .bash_logout, put the following:
[ -r ~/.last_cwd_$$ ] && rm ~/.last_cwd_$$
That's it! Now, if you use a different prompt string than the default, that should still work with this hint. Just add `echo w > ~/.last_cwd_$$` to the prompt string. Also make sure the prompt string itself is specified between single quotes, so that bash defers evaluating the echo statement until it actually prints the prompt string.

[robg adds: I haven't tested this one...]
    •    
  • Currently 1.00 / 5
  • 1
  • 2
  • 3
  • 4
  • 5
  (3 votes cast)
 
[15,534 views]  

Open new xterm windows in current Terminal directory | 5 comments | Create New Account
Click here to return to the 'Open new xterm windows in current Terminal directory' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Huh? xterm does open in current directory
Authored by: SOX on Jan 04, '06 02:44:16PM

On my computer typing xterm in the terminal.app opens xterm in the current directory.



[ Reply to This | # ]
Open new xterm windows in current Terminal directory
Authored by: ershler on Jan 04, '06 04:11:42PM

Yup, mine works as well, without touching anything.

Phil



[ Reply to This | # ]
Open new xterm windows in current Terminal directory
Authored by: tlindner on Jan 04, '06 07:47:12PM

The title of this hint is misleading. This hint is for Terminal.app. When you hit Command-N in Terminal.app it will start a new terminal window with the home directory as your current working directory.

If you add these scripts Terminal.app will start new terminal windows with the same current working directory as the previous window.



[ Reply to This | # ]
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.

[ Reply to This | # ]
Open new terminal windows in current directory
Authored by: Lliwynd on Sep 12, '06 12:03:08AM
I agree that the title is misleading. I also like the solution presented here:

http://www.entropy.ch/blog/Mac+OS+X/2005/02/28/Terminal_tricks_8220_term_8221_and_8220_clone_8221.html

It is a script that will open a new terminal in the current directory. It will also optionally execute a command in that terminal, and possibly even close the window afterwards.

[ Reply to This | # ]