Please help me make my terminal work!
April 4, 2008 10:24 PM   RSS feed for this thread Subscribe

How can I get new tabs in Mac OS X Terminal.app to open with the same current working directory as the tab from which I opened the new one?

I use Mac OS X and Linux. Many Linux terminal emulators I've used (gnome-terminal,xfce-terminal) have this great feature where, when I open a new tab, the current working directory is set to the working directory of the shell I opened the new tab from. This is great when I'm working with my webapps - I cd into my project directory, start the server on one tab, open a new one to use for tests, open a third to use for editing. When I'm working on my homework for school I can open up one tab as my editing tab, another to use an interactive Python shell, another I can use to run my homework, etc etc.

As far as I can tell, there is no way to do this with Terminal.app or iTerm. iTerm's sourceforge page shows an outstanding request for this feature. I would add it in myself but I don't have any Objective-C skills :( Are there any other terminal apps for OS X that would allow me to do something like this?

Are there any good terminals for OS X/X11 that don't have too much in the way of dependencies/bloat that support this feature? I don't normally use X11 on my Mac, but am willing to for this awesome, time-saving feature.
posted by mebibyte to computers & internet (8 comments total) 2 users marked this as a favorite
This exact question came up on the Mac OS X Hints forums a week ago. Was that you, by chance? :)

Perhaps you could use the /usr/bin/osascript utility to tell Terminal to create a new tab in the current document of the window (or however it's referenced, this can get weird sometimes), and then issue the appropriate cd command to the new tab.

Use Script Editor.app to open the Terminal dictionary and peek around at what it supports, then have a look around MacScripter.net and its forums for examples of scripting Terminal.app.
posted by Mikey-San at 11:03 PM on April 4


(And when you have your one-liner, add it as a command alias or .sh script somewhere in your $PATH.)
posted by Mikey-San at 11:04 PM on April 4


Hm. You could alias cd to write the directory you're changing to into a hidden file in your home directory, then have your profile script cd to the content of that directory.
posted by kindall at 11:05 PM on April 4


Open Terminal Here.
posted by derMax at 6:33 AM on April 5


Hmm, upon re-reading is not be exactly what you are looking for. Still useful though.
posted by derMax at 6:36 AM on April 5


if you like gnome-terminal, try installing MacPorts and then "sudo port install gnome-terminal"... MacPorts is a framework for installing unix software on OSX, and is well useful for all your unix development needs...
posted by russm at 7:38 AM on April 5


The following will do roughly what you want. Open Script Editor, and enter the following script:

on run argv
tell application "System Events" to tell process "Terminal" to keystroke "t" using command down -- open a new tab
tell application "Terminal" to do script "cd " & item 1 of argv in front window -- change directory
end run

Save this script as /Users/[username]/scripts/newtab.scpt

Now you can open a new tab in the current directory using:
osascript ~/scripts/newtab.scpt `pwd`

Finally, add a line to ~/.bashrc which aliases this to something convenient, e.g.:
alias newtab='osascript ~/scripts/newtab.scpt `pwd`'

If you don't normally use a .bashrc, you might need to make a file ~/.bash_profile containing the single line:
source ~/.bashrc

Now close your current terminal window, open a new one and typing newtab should do roughly what you want.
posted by beniamino at 12:05 AM on April 6


I use tcsh rather than bash (never got around to changing). I just added these two lines to the end of my .login:
cd "`cat ~/.lwd`"
alias precmd 'echo -n "$cwd" >~/.lwd'
Seems to have the desired effect... when you open a new window/tab, you're in whatever directory you last changed to. Not the one in the current tab, unfortunately (you could have switched tabs since the last time you cd'd... terminal doesn't know what directory you're in in any of its windows) but it may be close enough.

I like this behavior and will probably keep it.
posted by kindall at 1:57 AM on April 6


« Older Help me understand US Treasury...   |   Is there a cheap (free) way to... Newer »

You are not logged in, either login or create an account to post comments



Related Questions
OSX: Future as a programmer dashed by failure to... August 20, 2008
In OS X, how to open an application with command... August 10, 2008
.chm to .pdf conversion in Mac OS X? September 23, 2007
How do I reset the settings on Terminal for OS X? March 15, 2006
Viewpoint terminal emulation on a Mac mini? February 25, 2005