Please help me make my terminal work!
April 4, 2008 10:24 PM   Subscribe

How can I get new tabs in Mac OS X 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 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 answers 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 to open the Terminal dictionary and peek around at what it supports, then have a look around and its forums for examples of scripting
posted by secret about box at 11:03 PM on April 4, 2008

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

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, 2008

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

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, 2008

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, 2008

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, 2008

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, 2008

« Older Help me understand US Treasury securities.   |   I've lost the AC adapter, and I need to turn this... Newer »
This thread is closed to new comments.