## Share Eclipse snippets among workspaces, or even different computers

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

C:\Users\YOURNAME\AppData\Roaming\Eclipse\workspace-YOUR_WS_NAME\.metadata\.plugins\org.eclipse.wst.common.snippets\user.xml

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.

## 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.

1. 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”).
2. Change directories into your workspace/(etc.)/org.eclipse.wst.common.snippets/ folder.
3. 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.

This entry was posted in Uncategorized. Bookmark the permalink.

### 3 Responses to Share Eclipse snippets among workspaces, or even different computers

1. Superb article. can’t wait to try this out!
Shame you can’t have multiple xml files which Eclipse could merge on the fly, but then I guess it wouldn’t know which file to write to for new ones.
Or maybe have multiple instances of the snippets plugin installed, each pointing at a different user.xml file. that way you could have a shared set and a single user set

• Martin Mapes says:

I should mention that I discovered a problem with my clever technique. As soon as SkyDrive gets involved, the hard link is broken, and my laptop and desktop drift out of sync. Don’t know if Dropbox does it any differently. Don’t know why it happens or how to stop it. Maybe there’s another way.

2. Nam Nguyen says:

Thanks, very useful article