coderrr

January 30, 2010

How to create a shortcut key to open a new terminal window in the current working directory on OSX

Filed under: osx — Tags: — coderrr @ 2:04 pm

Shameless Plug: Use a Mac OS X Compatible VPN Service to protect your privacy on the Internet.

In gnome-terminal I always used ctrl-shift-n to open a new terminal window in the current working directory (cwd). Having been forced to switch to OSX I was totally missing this functionality in the Terminal app. I often spawn a new terminal to quickly execute a quick command or series of commands and then close it. Not finding any satisfactory solutions online I hacked one together which works.

My Solution

First make sure the following checkbox is enabled: Terminal -> Preferences -> Window ->TTY Name

Now edit your ~/.bash_profile and add this line:

export PROMPT_COMMAND='pwd > /tmp/cwd.`ps -o tty= -p $$`'

Next create an applescript with the following content (if you don’t know how to create/compile an applescript just use the AppleScript Editor) and save it in your Scripts directory:

tell application "Terminal"
	set ttyname to (tty of (front window))
	set AppleScript's text item delimiters to "/"
	set cwd to do shell script "cat /tmp/cwd." & (item 3 of (text items of ttyname))
	activate
	do script "cd '" & cwd & "'"
end tell

Now that you have saved the script we only need to bind a key to it. This requires an extra application as I don’t know of any stock way to do this in OSX. I use FastScripts to do this. I believe KeyboardMaestro also has this capability, as I’m sure lots of other apps I don’t know about do. Feel free to leave a comment with suggestions on better/free-er ones.

With FastScripts you just drop down the menubar which should already contain your script if you saved it in your Scripts directory. Find the script and cmd-click it, this will bring up a popup which allows you to assign a global shortcut.

How it works

The $PROMPT_COMMAND is executed before each prompt (i.e. after any directory change). pwd > /tmp/cwd.`ps -o tty= -p $$` writes out the current working directory to a temp file containing the name of the current TTY. So for each Terminal session there will be a corresponding file which is kept up to date with its current directory. The applescript grabs the TTY from the current active Terminal and reads its cwd file to find the current directory. It then spawns a new terminal window and tells it to cd to that directory.

* note: if you are in a screen this will not work because the TTY will be different than the one in the Terminal’s window.

Let me know if you have a better solution, I’m sure there’s one out there.

9 Comments »

  1. I use Open Terminal Here:

    http://maururu.net/2007/enhanced-open-terminal-here-for-leopard/

    It works really well, also with zsh.

    Comment by Peter — January 30, 2010 @ 8:27 pm

    • It looks like that script is just for opening a new terminal in the current finder directory, not the current directory of the terminal

      Comment by coderrr — January 31, 2010 @ 11:22 am

  2. Sorry, you’re right, I misread that. For opening the current folder from terminal I use a modified version of a script also by Marc Liyanage:

    if [[ -d "$1" ]]; then
    WD=`cd “$1″; pwd`; shift;
    else
    WD=”‘`pwd`’”;
    fi
    COMMAND=”cd $WD”
    osascript >/dev/null <<EOF
    tell application "Terminal"
    activate
    do script with command "$COMMAND"
    end tell
    EOF

    But this probably won't work with a keyboard shortcut like you describe here, because it needs to be run from the shell… (I use "term" to run it).

    Comment by Peter — January 31, 2010 @ 8:26 pm

  3. My original script mentioned by Peter is here, it might be useful:

    http://github.com/liyanage/macosx-shell-scripts/blob/master/term

    One way to bind it to a keyboard shortcut is directly with bash’s “bind”:

    bind -x ‘”\C-n”‘:’term’

    This runs it on Ctrl-n, I just tested it.

    Nice idea by the way, I never thought about using it this way :) I use another script more often, going form a Finder window to a terminal window with Cmd-T:

    http://www.entropy.ch/blog/Mac+OS+X/2008/08/07/Open-Terminal-Here-Finder-Keyboard-Command.html

    Comment by Marc — February 10, 2010 @ 4:40 pm

    • yea that works for binding most ctrl-alt (but not shift) combinations, but the main problem is that it doesn’t work if you’re already inside some program on the terminal.

      Comment by coderrr — February 16, 2010 @ 7:08 pm

  4. another solution is to use iTerm http://iterm.sourceforge.net/
    instead of Terminal.app. Recent versions do the trick out of the box. (for new tabs, not for new windows, though).

    it has better xterm emulation, 256 color support and mouse reporting too.

    I’ve missed some features of gnome-terminal too, iTerm made this loss much easier for me.

    Comment by Anonymous — February 24, 2010 @ 4:32 am

  5. Thanks for the post, I got it working pretty easily!

    I was wondering though, is there an easy way to make this launch a new Terminal TAB instead of a Window?

    Comment by Ganesh — April 12, 2010 @ 12:31 am

  6. short cut keys on window 98 in bios
    enviroment.

    Comment by paul — June 16, 2010 @ 8:25 am

  7. i dont understand your answer.

    Comment by paul — June 16, 2010 @ 8:28 am


RSS feed for comments on this post. TrackBack URI

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

The Silver is the New Black Theme. Create a free website or blog at WordPress.com.

Follow

Get every new post delivered to your Inbox.

Join 27 other followers

%d bloggers like this: