I'm making some custom templates (among other things) for my favorite languages, and I need some advice/best practices about them.
1. I heard that people are moving away from normal Templates and towards Snippets for this purpose; is this correct?
2. One advantage I see with Snippets is that it lets me set the filename (which I can then use) before calling the Snippet, which is not possible with a Template unless I'm in a project. Is there some way around that with Templates?
3. As I make these Templates, I'm finding a lot of common text between them, and when I want to tweak it across the board...well, it's like programming without functions. What, if anything, can I do to reduce redundancy in my templates?
Thanks, Neil.
From: Neil kngspook@gmail.com
- As I make these Templates, I'm finding a lot of common text between them,
and when I want to tweak it across the board...well, it's like programming without functions. What, if anything, can I do to reduce redundancy in my templates?
I'm sure this is possible, and I've fiddled about with doing something similar, but haven't put too much effort into this. You can execute Ruby from within snippets, and in theory you could put your repository of reusable snippet text within a file or a preference or something, and just put the text in through Ruby.
So, I just threw something together, and got this working.
1. In your bundle on the hard drive, create a folder called "Support" (right-click the bundle in ~/Library/Application Support/TextMate/Bundles/, choose show package contents)
2. In this folder, create a few ruby files that will contain your reusable snippet text.
3. Each file might look like this:
print 'Here is some reusable text.'
4. In your snippet, add this "tab stop" to load the file and insert the text:
This is my snippet... ${1:`#!/usr/bin/env ruby require ENV['TM_BUNDLE_SUPPORT'] + '/SnippetRepo' `}$0
Starting with the ${1: is the embedded ruby that executes; I don't know if you know ruby or not, but all it does is load and execute that file you created in the Support folder. Again, not sure how much you know, but TM_BUNDLE_SUPPORT provide the path to the bundle's Support folder. You have to manually create the Support folder (as far as I know), and it has to be at the root level of the bundle, spelled right, etc. But then TM_BUNDLE_SUPPORT provides easy access to files that your bundle will use, but isn't directly a Snippet, or Command, or whatever.
The disadvantage with this technique is that, as far as I know, ruby can only be executed from the tab stops, so when the snippet expands and you tab through the other stops, this boiler plate text will get highlighted, as well, and potentially altered.
Also, I'm not sure how to be more intelligent about the text inserted this way; I'm sure it's possible, but I don't know if the snippet can communicate with the script you run or vice versa. You can, however, run ruby as if it were a command, so you should have access to all of those shell variables that you can use in commands, like TM_FILENAME and the like.
I hope that helps; please post any further advancements you make in this area. I'm interested, as well!
+dru
On Wed, Jul 8, 2009 at 9:24 AM, Dru Kepple dru@summitprojects.com wrote:
From: Neil kngspook@gmail.com
- As I make these Templates, I'm finding a lot of common text between
them,
and when I want to tweak it across the board...well, it's like
programming
without functions. What, if anything, can I do to reduce redundancy in my templates?
I'm sure this is possible, and I've fiddled about with doing something similar, but haven't put too much effort into this. You can execute Ruby from within snippets, and in theory you could put your repository of reusable snippet text within a file or a preference or something, and just put the text in through Ruby.
So, I just threw something together, and got this working.
- In your bundle on the hard drive, create a folder called "Support"
(right-click the bundle in ~/Library/Application Support/TextMate/Bundles/, choose show package contents)
- In this folder, create a few ruby files that will contain your reusable
snippet text.
- Each file might look like this:
print 'Here is some reusable text.'
- In your snippet, add this "tab stop" to load the file and insert the
text:
This is my snippet... ${1:`#!/usr/bin/env ruby require ENV['TM_BUNDLE_SUPPORT'] + '/SnippetRepo' `}$0
Starting with the ${1: is the embedded ruby that executes; I don't know if you know ruby or not, but all it does is load and execute that file you created in the Support folder. Again, not sure how much you know, but TM_BUNDLE_SUPPORT provide the path to the bundle's Support folder. You have to manually create the Support folder (as far as I know), and it has to be at the root level of the bundle, spelled right, etc. But then TM_BUNDLE_SUPPORT provides easy access to files that your bundle will use, but isn't directly a Snippet, or Command, or whatever.
The disadvantage with this technique is that, as far as I know, ruby can only be executed from the tab stops, so when the snippet expands and you tab through the other stops, this boiler plate text will get highlighted, as well, and potentially altered.
Also, I'm not sure how to be more intelligent about the text inserted this way; I'm sure it's possible, but I don't know if the snippet can communicate with the script you run or vice versa. You can, however, run ruby as if it were a command, so you should have access to all of those shell variables that you can use in commands, like TM_FILENAME and the like.
I hope that helps; please post any further advancements you make in this area. I'm interested, as well!
+dru
Thanks for the response.
That will help quite a bit, for things like making #include guards in my C bundle. But for some other things, I'd like to be able to share across bundles (eg. copyright notices). I suppose I could do something like that by just putting it in the TM_SUPPORT folder instead of the TM_BUNDLE_SUPPORT?
I do know a little Ruby... Is it just Ruby that's supported, or any scripting language?
Thanks again, Neil.
From: Neil kngspook@gmail.com Reply-To: TextMate users textmate@lists.macromates.com Date: Wed, 8 Jul 2009 17:34:07 -0700 To: TextMate users textmate@lists.macromates.com Conversation: [TxMt] Re: Questions about Templates Subject: [TxMt] Re: Questions about Templates
I do know a little Ruby... Is it just Ruby that's supported, or any scripting language?
I haven't tried, but the fact that you need a shebang line suggests that any shell scripting language would work.
On 9 Jul 2009, at 02:34, Neil wrote:
That will help quite a bit, for things like making #include guards in my C bundle. But for some other things, I'd like to be able to share across bundles (eg. copyright notices). I suppose I could do something like that by just putting it in the TM_SUPPORT folder instead of the TM_BUNDLE_SUPPORT?
For the cases you mention, we already have once⇥ (include guard) and head⇥ (for the copyright header).
As for “moving from templates to snippets” there are two reasons for doing so, one is the technical, i.e. templates being amputated commands, the other that having multiple small snippets gives you more flexibility in your workflow, i.e. in how you can combine these and at what time you insert them.
On Thu, Jul 9, 2009 at 11:52 AM, Allan Odgaard mailinglist@textmate.orgwrote:
On 9 Jul 2009, at 02:34, Neil wrote:
That will help quite a bit, for things like making #include guards in my C bundle. But for some other things, I'd like to be able to share across bundles (eg. copyright notices). I suppose I could do something like that by just putting it in the TM_SUPPORT folder instead of the TM_BUNDLE_SUPPORT?
For the cases you mention, we already have once⇥ (include guard) and head⇥ (for the copyright header).
Yeah, I'm aware of those; I just needed slightly different conventions though. (I did look at how they were done, however, before I made my own.)
As for “moving from templates to snippets” there are two reasons for doing so, one is the technical, i.e. templates being amputated commands, the other that having multiple small snippets gives you more flexibility in your workflow, i.e. in how you can combine these and at what time you insert them.
Yeah, I'm starting to see that.
I'm actually look at Macros now, to see if they might do what I want, as far as combining Snippets and such...