Sync Your Xcode Code Snippets Across Devices (Because You Can)

Nic Laughter
2 min readOct 21, 2017

--

*Updated for MacOS Catalina, if you have Desktop and Documents folders backed up to iCloud Drive

Code Snippets are great. Why not store them in iCloud?

To get straight to the magic, scroll to the bottom.

We’ve all been there.

Maybe you just started a new job, maybe you just upgraded your personal dev box, or maybe your employer is replacing the old rust bucket you’ve been crying about for the past 3 months, but one of the few bad things about switching to a new Mac is losing all your sweet Xcode code snippets. Code snippets are a beautiful tool for Apple developers (if you’re not using them, you should rethink your life), but until Apple gives us developers specific directories in iCloud for which to store our cargo (wink-nudge, Apple), we’ll need to come up with an alternative. I mean, really, who has the time to re-type all those snippets?

Enter the symbolic link. I think most developers know this solution is available, but maybe think it takes a lot more work to implement than it’s worth. I was shocked to talk to so few developers that have their snippets synced across all their machines. At any rate, it’s super easy, and if you’re already somewhat familiar with the macOS file structure, you can do a lot more than what I give here.

Note: this approach works perfectly well with Dropbox. I won’t go into it here (because iCloud Drive FTW), but you can easily customize the steps below to accomplish the same thing. It should also work with Google Drive if you’re into that sorta thing, though I haven’t tested it personally.

So let’s get started!

First make sure you have iCloud Drive signed in and enabled on your machine. Assuming you’ve been using Xcode and have some snippets already saved, this will move them all inside your iCloud Drive’s already-existing Documents folder and create a symbolic link from which they can be read by Xcode.

On your primary machine, open Terminal and copy/paste this line:

mkdir ~/Documents/XcodeDocs

This will create a folder named XcodeDocs inside your iCloud Drive’s Documents directory where your code snippets will live. Next, copy/paste this line (being sure to replace “YOUR_USERNAME” with…your username):

mv -v /Users/YOUR_USERNAME/Library/Developer/Xcode/UserData/CodeSnippets ~/Documents/XcodeDocs

This will take the whole CodeSnippets folder out of your Xcode folder and pop it right into iCloud Drive at the right directory. Then copy/paste this line next (again, replacing YOUR_USERNAME):

ln -s ~/Documents/XcodeDocs/CodeSnippets "/Users/YOUR_USERNAME/Library/Developer/Xcode/UserData/"

This creates the link to where you moved the folder, et voilà! Then when you set up that sweet new Mac, simply run that last line in Terminal (after downloading and running Xcode at least once, of course), then your code snippets will magically be waiting for you!

Thanks for reading!

EDIT:

If you’re doing this on a secondary machine you’ve already created snippets on, you have to first run this line (which will delete those snippets):

rm /Users/YOUR_USERNAME/Library/Developer/Xcode/UserData/CodeSnippets

After that, you can run that last line above and it will be working.

--

--