TextMate rulez!!!

Posted on March 17, 2006

I've been using TextMate for sometime now and all I can say is wow. I'd never think that someday I'll find another text editor that can replace vim for me.

Sure for simple shell scripts I still prefer vim, especially when I work on a remote host via ssh. But when I'm using my Mac I'm more and more apt to type

$ mate file-name

nowdays.

There are couple of things that I like about TextMate. And one major one is that it is very easy to extend with some help of shell programming. You don't have to learn another language in order to do it. You can always use your favorite one, like Ruby.

For example, one of the features that I miss from my vim days was ability of the editor to complete file names for me.

This simple script provides this functionality for me:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30

    #!/usr/bin/env ruby

    index = ENV['TM_LINE_INDEX'].to_i
    line = ENV['TM_CURRENT_LINE'][0...index]

    filename_char = '(?:\\\\[ \'"&,()]|[\w~.-])'
    filename_regexp = 
      /((?:\/)?(?:#{filename_char}+\/)*)(#{filename_char}*)$/

    if line =~ filename_regexp
      dir, prefix = $1, $2
      Dir.chdir File.expand_path(dir) rescue exit
      matched = Dir["#{prefix}*"]
      if matched.size == 1
        print matched.first.sub(/^#{prefix}/, '')
      else
        args = matched.map{|f| "\"#{f}\""}.join(', ')
        result, value = %x{
      "#{ENV['TM_SUPPORT_PATH']}/bin/CocoaDialog.app/Contents/MacOS/CocoaDialog\"  dropdown \
      --title 'Possible file names' \
      --text 'Choose correct filename for insertion, or escape to cancel.' \
      --button1 Ok --button2 Cancel \
      --string-output --no-newline \
      --items #{matched.map{|f| "\"#{f}\""}.join(' ')}
        }.split(/\n/)
        print value.sub(/^#{prefix}/, '').
          gsub(/(['"&,() ])/){"\\#$1"} if result == 'Ok'
      end
   end

Now when you create this command in TextMate Bundle Editor, make sure that you use None for Input, and Insert as Text for Output. That's it.

Post a comment
Comment





You may use Markdown in your comments, so please give your code snippets a four-space indent.