I use Eclipse as my editor of choice. I also use multiple Workspaces (see File > Switch Workspace). I also use Snippets for common code templates.
I created some useful snippets in one workspace that I wanted to be able to use in another. Eclipse doesn’t provide an easy way to share snippets across workspaces. Using Eclipse, you would have to copy the snippet, switch workspaces (which requires restarting the program), and then paste it… for each snippet you want to reuse. And that’s awful.
Phase 1: Copy the XML nodes
One way to copy a bunch of snippets across workspaces is to merge the various snippet XML files into a master file. Your snippets are stored in
I was able to use WinMerge to compare my new snippets file with my older one. It was easy to use WinMerge’s Copy Left command to push just the new lines into my older file, and then save the merged master. Restarting Eclipse with the new master file confirmed that I successfully copied the snippets.
But wait a minute…. My snippets files are in sync, but there is nothing to prevent them from drifting out of sync again.
Hard links to the rescue.
Phase 2: Make a hard link
A hard link is a pointer. It looks like a file, but it doesn’t have any contents, it has a reference to another file, somewhere else on the file system. I can create a hard link called “user.xml” in each of my workspaces that points to a single, XML master file.
I saved my merged master XML file to a location outside of the Eclipse folder tree. Then I backed up my individual user.xml files by renaming them. Then I created the hard links.
To create a hard link,
- Open a command prompt. You may have to open the command prompt as an administrator (on Windows 7, right-click the link to command prompt and choose “Run as administrator”).
- Change directories into your workspace/(etc.)/org.eclipse.wst.common.snippets/ folder.
- Make the link like so:
mklink /H user.xml "C:\path\to\your\master\user.xml"
… and repeat steps 2 and 3 for each of your workspaces. Easy!
To carry the idea a step further, I decided to put my real master XML file in a folder that is shared via LiveMesh (you could do the same thing with Dropbox or a network drive). That way, I can sync my snippets between my laptop and my desktop computer.