Posts RSS Comments RSS 32 Posts and 1,017 Comments till now

TextMate Tip – Where Am I?

Apparently this question is quite common – since TextMate does not show the full path of the currently edited file, people are not entirely clear on the file’s location. So I was asked to do this tip, which will cover 4 different methods of discovering the path to your current file, and hopefully teach some new tricks too.

Tab Tool Tip

The simplest way is just to hover the mouse cursor over the relevant tab, and you’ll get a tool tip showing the full path to the file.

Reveal in Project

At any time while editing a file in a project you can press ⌃⌘R (or File → Reveal in Project) to select and reveal the file in the project tree. This is handy if you want to see where you are in the project, if you want to select another file in the same directory, or if you want to do a Subversion commit (since commit works on the currently selected item). You can also use ⌃⇧⌘R (or File → Reveal in Finder while holding down Shift) to reveal the file in a Finder window. On a similar note, you can use ⌃⇧O (Bundles → Shell Script → Open Terminal) to open a new Terminal window in the current directory.

Document Proxy Icons

TextMate document windows (and most OS X apps, in fact) with saved files will have a small icon representing the current file next to the filename in the window title. The icon will be faded out slightly if the document has unsaved changes (also visible by looking for the dot in the window’s close button), and solid otherwise. By dragging this icon you can use it to interact with other apps, for example if you wanted to e-mail the current document for someone you could drag the icon to the Mail dock icon, or if you wanted to open the current directory in a Finder window as a TextMate project then you could drag the proxy icon to the TextMate dock icon.

The other, more relevant, use is that you can Command-click the icon (or in fact the filename) to see a dropdown with the path to the file on disk – and you can click any item in the list to open it in Finder. This is great for quickly opening the project directory.

A Custom Command

TextMate sets many environment variables with contextual information, for use in writing commands, snippets etc. One of these environment variables is TM_FILEPATH – as the name suggests, this contains the full path to the current file. To try it out, go to a new, empty line in a document on disk and type:

echo "$TM_FILEPATH"

and then press ⌃R (Text → Execute Line Inserting Result) on the same line to run the command in a shell and see the result – you should get a new line below with the path to the current file.

This is handy, but if you want to view the path quickly at any time then there is a better way – we can make a new command to show us the path.

  • Open the bundle editor with ⌃⌥⌘B (Bundles → Bundle Editor → Show Bundle Editor…)
  • If you have not yet created a personal bundle, use the bottom left pop-up button (the plus sign with a drop-down arrow) to create a New Bundle – it should be automatically created with the current user’s name.
  • Use the same button to create a New Command
  • Rename the command to “Show Path to Current Document”
  • Put the same command – echo "$TM_FILEPATH" – in the big text view on the right side of the window
  • Use the pop-up next to the Input: field to set the input to “None”
  • Change the Output: to “Show as Tool Tip”
  • You can use the field next to Key Equivalent at the bottom of the window to set a custom key equivalent for activating the command if you wish – otherwise you will have to use the bundle menu or ⌃⌘T to activate it
  • Close the bundle editor and try activating your new command – you should see a tool tip displayed with the full path to your current document.

You could also set the Output: to “Insert as Text”, or even leave it as “Show as Tool Tip” and use:

echo -n "$TM_FILEPATH" | pbcopy && echo 'Path copied to clipboard'

See man pbcopy for more information on this.

8 Responses to “TextMate Tip – Where Am I?”

  1. on 27 Nov 2007 at 11:05 pmHenrik N

    In Leopard, you can +click/right-click the proxy icon/filename to reveal its path, in addition to -clicking as in Tiger.

  2. on 10 Dec 2007 at 11:28 amRuby, Rails, TextMate « exceptionz

    [...] Tips and Tricks – Where the hell am I [...]

  3. on 04 Jan 2008 at 4:35 amRuby, Rails, TextMate

    [...] Tips and Tricks – Where the hell am I [...]

  4. on 21 Jun 2008 at 11:06 pmmarios

    Another note on the proxy Icon thing. What I often do, is when uploading a document that I have in a finder window, is to drag it to the drop down file list in Safari for instance. What this does is , it automatically highlights the file you want to upload after switching to the directory it’s in. You just press the select button and you’re off.

  5. on 25 Jun 2008 at 11:04 pmAlex Melhuish

    Further to marios’ comment – that works in any Open/Save dialog. However for Safari, you can drag a file directly to a file upload form widget on a web page, instead of clicking the browse button. Just a bit quicker – I only wish Firefox supported it.

  6. on 24 Jan 2009 at 9:33 pmSpastik

    Tried another take on this. Maybe you folks can help improve it? http://snipplr.com/view/11490/textmate-command-snippet–show-document-path-in-window-title/

  7. on 20 Dec 2009 at 1:20 pmcredit repair rhode island

    credit repair rhode island…

    I like the way things are done around here….

  8. [...] found this beautiful article on the topic. It has nice explanation of different ways to access current file location in [...]

Fork me on GitHub