Sebastian Zeki
Sebastian Zeki

Reputation: 6874

How to create snippet subfolders in textmate bundle

I've just discovered textmate and I love it. I would love to use it as a way of storing my own snippets. I know how to do this in textmate but as I want to add loads of snippets I don't want them to become disorganised. I would like to add them into organised subfolders under the relevant bundle. eg under the shell script bundle I would like to add a folder that keeps all my networking snippets together. I cant find anywhere how to do this but I know it can be done as some bundles are organised like this.....Help

Upvotes: 1

Views: 307

Answers (2)

tim
tim

Reputation: 3608

Just some extra info on the excellent answer above

As of TM2 rc23, you can access the UUID of an existing item by right-clicking on it in the bundle editor window (accessed with cntrl-option-command-B)

You can create a UUID in terminal.app with the command uuidgen (you could also make this into a command if you use this often)

TM2 doesn't need the cache clearing - it watches for changes in the plist.

@user96157 is clear about this, but note you have to also add your new submenu to the mainMenu. So:

<key>mainMenu</key>
<dict>
    <key>items</key>
    <array>
    <string>COPY-UUID-FOR-YOUR-NEW-SUB-MENU-HERE!</string>

Upvotes: 1

user96157
user96157

Reputation: 36

I managed to figure this out with a lot of trial and error and starting with this Superuser answer.

The only way to do it in TextMate 2.0 is to manually edit the info.plist file for your bundle. I suggest doing this on an exported copy of the bundle and reloading it into textmate. To export a bundle right click on the bundle in bundle editor and click Export Bundle....

Start by adding a mainMenu section at the top level of the plist file:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN"  "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
  <dict>
    ...
    <key>mainMenu</key>
    <dict>
    </dict>
  </dict>
</plist>

From what I can tell, the mainMenu dict supports 3 keys: excludedItems, items, and submenus. The excludedItems and items are arrays containing a list of UUIDs of either bundle items (snippets, commands, etc.) or submenus. You can get the UUID of a command or snippet by opening it in textmate and finding the uuid key; it'll look like:

<key>uuid</key>
<string>0A2DB1AC-3049-4BD5-8931-641E716990F9</string>

Once you have the UUIDs of your items you can list them in items to populate the bundle menu in the order you wish, e.g.:

<key>items</key>
<array>
  <string>409b0e74-9ab5-4d35-b957-9ddf23a71c0c</string>
  <string>------------------------------------</string>
  <string>d2c991dc-a00e-4247-8479-f2d29f387319</string>
</array>

If you use a series of - characters in lieu of a UUID, it will create a separator in the menu. Likewise, if there are snippets you don't want to display in the bundle's menu you can add them to excludedItems.

To create submenus you have to define them inside submenus, like so:

<key>submenus</key>
<dict>
    <key>71BE58B2-E486-4B21-93F1-C208D4914099</key>
    <dict>
        <key>items</key>
        <array>
            <string>6D0B2B9D-62C7-4842-BA28-F3379E887D93</string>
            <string>CADC55BD-0D0A-48C8-B296-35FA7AAE09CA</string>
        </array>
        <key>name</key>
        <string>C++ Snippets</string>
    </dict>
</dict>

Each submenu needs to have an associated UUID inside <key>...</key>. I created one from an online UUID generator. You can then add the submenu's UUID to the top-level items array to add the submenu to your bundle's menu.

Finally, when loading your bundle into textmate you must first delete all cached instances of the bundle from:

~/Library/Application Support/Avian/Bundles
~/Library/Application Support/Avian/Pristine Copy/Bundles

If you don't, textmate will sometimes ignore the newly loaded bundle. Not sure if there's a cleaner way to reload a bundle, but deleting it worked for me.

For a complete example see this commit in the ROS bundle on my Github.

Upvotes: 2

Related Questions