TextMate News

Anything vaguely related to TextMate and macOS.


This is great, really useful.

It goes even easier: just drag the TextMate icon from the “Applications” folder to the finder titlebar.

For people who like like living in the commandline, I thought I’d share a couple of little scripts I made for working in projects (eg: websites, source trees…) They just want to sit in your $PATH somewhere and be made executable:

the first: tm

# tm <pattern>...
# Open all files under this location with the
# given (wildcarded) name(s).
# eg: tm \*Servlet.java

for i; do
    if [-f "$i"] ; then
        mate "$i"
        find . -path './{arch}' -prune -o -name "$i" -print | xargs mate

And the second: tmgrep

# tmgrep <text> <pattern>...
# Open all files matching the given pattern(s)
# under this location that contain
# the given text.
# eg: tmgrep "extends SomeClass" \*.java

for i; do
    if [-z "$GREP"] ; then
    find . -path './{arch}' -prune -o -name "$i" -print | xargs grep -l "$GREP" | xargs mate

You can call them what you like; On my system I still call them xcode and xcgrep because the original of these scripts opened files in XCode rather than TextMate and that’s what my fingers are used to typing now. (I also have a variant that opens in gedit under Linux, also called xcode and xcgrep.) But textmate is way nicer for doing this stuff especially as, when you use the above and it matches a bunch of windows, they all turn up in a sidebar in one new window, rather than covering your desktop in windows… which can get especially tiresome when you accidentally tell it to open a couple of thousand files…

Obvious and probably easy enhancements might include:

  • allow regexp expressions (optionally) in tmgrep. I didn’t especially miss it myself as regexp has never quite become second-nature.
  • be more generic in what application it uses to open the files. On OS X of course, you could actually just replace mate with open as long as you’ve already configured the system to open the files in the right application. The grepping variant obviously is unlikely to be very useful on anything other than text files.
  • cope with folders and filenames with spaces in them. I think I know how, I just haven’t needed it enough to make it happen and get it all tested.

The -path './{arch}' -prune bit is just because we use bazaar for our source control and I don’t want this command picking up the files of the same name inside its working directories. You can lose it if you want; I’ve left it in as an example.

Rachel: As for spaces in filenames, use -print0 instead of -print and then give -0 to xargs (this makes the printed names be null-terminated, and likewise has xargs expect them to be that).

And for naming it xcgrep, I need to put the following on all my servers to avoid having my sessions littered with command not found :)

alias mate='nano -w'

I know about -print0 and xargs -0. :-) The part I wasn’t sure about whether it would work is the tmgrep one where we pipe through xargs twice, and I’d just taken some headache pills when it occurred to me and didn’t feel like testing it out. :-) In all the time I’ve used these scripts I’ve not found myself needing to use them where it’s a problem, or it would have been fixed.

find . -name "$i" -print0 | xargs -0 grep -lZ "$GREP" | xargs -0 mate

I think that would work, but wasn’t really sure there might not be some wonky edge cases.

That should work, also find can be made to execute the grep like this:

find . -name "$i" -exec grep -sq "$GREP" '{}' \; -print0 | xargs -0 mate

I also suggest this addition to the script:

echo -n "$GREP"|pbcopy -pboard find

Then when the file(s) open, one can use ⌘E to jump to the match :)

14 November 2007

by Darryl Zurn

This is very close to what I want to do. I already have a command bound in TextMate that will take whatever text is selected and open either or both files with these filenames:

open “file://path/to/$TM_SELECTED_TEXT.pdf” open “file://path/to/$TM_SELECTED_TEXT%20No%20PDF”

What I want is to open any and all files in this directory that starts with $TM_SELECTED_TEXT.

And there are spaces in the filename but not the path.

Any help is appreciated! Thanks Darryl

Darryl: The context is not entirely clear here, but simply using an unquoted * in the file to open should work for “completion”, for example:

mate "$DIR/"*"$EXT"

That will open all files with extension EXT (.php) in the DIR folder (/tmp). And both DIR and EXT can contain spaces.

Hey, cool idea!

Here’s a little something I whipped up…fits a little better with OS X toolbar.


[…] helpful, since I don’t have TM set as my default editor for HTML and CSS files.Check out the full post, as well as this other that talks about customizing the icon for the script. […]

[…] Open in Textmate Copiez cette petite extension n’importe où sur votre Mac, puis clic-droit dans la barre d’outil d’une fenêtre du Finder (dans la partie avec les petites icônes) et “Personnaliser la barre d’outils…”. Faites glisser l’extension dans la barre et cliquez sur “Terminé”. […]