Fork me on GitHub

article

Open a file’s parent folder as a Project in TextMate

May 27, 2010 | TextMate

So, I can’t count how many times I’ve opened a single file for editing in TextMate and then realized that I need to do more than just edit that one file. And most of the time the other files that I need to edit are in the same folder as the file that I’m currently editing. Rather than head back out to the finder each time I do this I finally wrote a command to do it for me. The following code will close the current document and open its parent folder as a TextMate project.

So, create a new command in your personal Bundle (you do have your own bundle where you store common snippets and commands, right? I keep mine here) and create a new command. Paste the following in to the Command(s) box:

  1.  #!/bin/bash
  2.  
  3.  # safeguard against the vars changing after closing the frontmost file
  4.  CURRENT_FILE=$TM_FILEPATH
  5.  CURRENT_DIR=$TM_DIRECTORY
  6.  CURRENT_LINE=$TM_LINE_NUMBER
  7.  
  8.  # close the file
  9.  /usr/bin/osascript -e "tell application \"TextMate\" to close (every window whose name is \"$TM_FILENAME\")"
  10.  
  11.  # open the project and file
  12.  mate "$CURRENT_DIR"
  13.  mate -l $CURRENT_LINE "$CURRENT_FILE"

Set the Input select box to “None” and the Output select box to “Discard”. Set a key equivalent if you’d like, I found that control-option-command-o works pretty well for me. Move focus in the bundle editor to a different bundle to save your changes and then close the bundle editor.

Now when you’ve got a single file open you can quickly get its containing folder open as a project.

Bonus Tip

I know you’ve been there. You have to remote edit a few files (yeah, sometimes it just can’t be avoided) so you’ve opened your favorite FTP client and used its “Open in external editor” feature to open a file or two in TextMate. But you’ve already got a few windows open and you’re starting to get cluttered. No problem. With this command you’re covered. After you open the first file in TextMate issue this command to open the file’s parent folder as a project. The file will be closed when the project opens (at least it was when I tested this with Cyberduck) but after you return to your FTP client and re-open that file and then open some other they’ll all open in the project window instead of separate windows. This is a huge brain saver if you’re trying to work with more than a few remote files.