I frequently need to open a new tab in the same directory as my current tab to do something else while my current tab is occupied by a long running process. However, by default when you create a new tab, Terminal.app starts at ~/. Any idea how to make it auto jump?
In OS X 10.7 (Lion), Terminal.app supports this natively:
One must be very careful when passing strings through different environments.
I run 10.4, so my ‘tfork’ script always opens a new window instead. It should be easy to adapt it to use a tab:
Amendment: cwd of an already running process “occupying” a Terminal tab
The idea of “the current directory of the program occupying the current tab” is not as obvious as one might expect.
Each Terminal tab has a single tty device that is used by the processes it runs (initially, a shell; thereafter, whatever the shell starts).
Each (normal) Terminal tty has a single foreground process group that one might consider as “occupying” the tty.
Each process group can have multiple processes in it.
Each process can have its own current working directory (cwd) (some environments give each thread their own cwd or cwd-equivalent, but we will ignore that).
The preceding facts establish a kind of trail that from tty to cwd: tty -> foreground process group -> processes of the foreground process group -> cwds.
The first part (from tty to foreground processes) of the problem can be solved with the output from ps:
(where “ttyp6” is the name of the tty of interest)
The mapping from process (PID) to cwd can be made with lsof:
(where “2515,2516” is a comma-separated list of the processes of interest)
But under Tiger, I see no direct way to get the tty device name of a particular Terminal window. There is a horribly ugly way of getting the tty name in Tiger. Maybe Leopard or Snow Leopard can do better.
I put it all together in an AppleScript like this:
Save it with Script Editor (AppleScript Editor in Snow Leopard) and use a launcher (e.g. FastScripts) to assign it to a key (or just run it from the AppleScript menu (enabled via /Applications/AppleScript/AppleScript Utility.app)).
I've posted a script that uses Chris Johnsen's code above and another script to open the new tab in the current directory with the current settings, mostly because I colour-coordinate my terminals. Thanks Chris, for that script, I've been using this for a few months now and it's a great time saver.
(* This script opens a new Terminal.app tab in the directory of the current tab with the same settings. You’ll need to, if you haven’t already, enable access for assistive devices as described here: http://www.macosxautomation.com/applescript/uiscripting/index.html
It’s almost all the work of two scripts put together, thank you to them:
Chris Johnsen’s script opens a new tab in the current directory: OS X Terminal.app: how to start a new tab in the same directory as the current tab?
Jacob Rus’s “menu_click” lets me create the tab with the same settings, as Terminal’s API doesn’t: http://hints.macworld.com/article.php?story=20060921045743404
If you change the name of a Terminal profile, the AppleScript API returns the old name until you restart the application, so the script won’t work on renamed settings until then. Ugh. Also, the necessity of activating Terminal to execute the menu command brings all the terminal windows to the front.
I use this alias/shell script to do it.