Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Xcode built-in snippets edit

Tags:

Is there a way to edit Xcode's built-in snippets? There is an edit button, but pressing it doesn't seem to allow changing the snippet's text.

Any insight is appreciated.

like image 261
Martin Cote Avatar asked Feb 10 '11 21:02

Martin Cote


People also ask

How do I edit snippets?

To edit an existing Snippet, click on the Pencil in the top right corner of any Snippet. A new window will pop up, where you can replace the default text or settings with something new. To save your changes, click Update. To delete a Snippet, click the Trash Can in the top right corner of that Snippet.

How do I use snippets in Xcode?

The only way to create a snippet in the current version of Xcode (11.3) is: Highlight the code you want to make a snippet. Right-click, then select Create Code Snippet or select Editor Menu > Create Code Snippet.

Where are Xcode snippets stored?

Code snippets are stored in your ~/Library/Developer/Xcode/UserData/CodeSnippets directory. To share your code snippets, simply copy them from this directory. Each code snippet file is a standalone plist file with a universally unique identifier (UUID) as the file name followed by .


2 Answers

You still can't edit the built-in system snippets. You can, however, edit "user" snippets.

The simplest solution in my mind was to create copies of all the default snippets, but modify them so that they are "user" snippets and override the default versions. I wrote a little Python script to do the job. It's very simple, and after running it all of Xcode's snippets will be magically editable via the Xcode GUI. No need to go mucking around in the plist by hand:

import plistlib import os.path  # Create user snippet directory if needed. user_snippet_path = os.path.expanduser("~/Library/Developer/Xcode/UserData/CodeSnippets") try:      os.makedirs(user_snippet_path) except OSError, err:     if err.errno != errno.EEXIST or not os.path.isdir(user_snippet_path):          raise  # Important, you'll need to quit and restart Xcode to notice the effects. # Important, change this if you're on a Developer Preview of Xcode. system_snippet_path = "/Applications/Xcode.app/Contents/PlugIns/IDECodeSnippetLibrary.ideplugin/Contents/Resources/SystemCodeSnippets.codesnippets"  print("Reading snippets from " + system_snippet_path) plist = plistlib.readPlist(system_snippet_path) for entry in plist:      # Create a new user snippet file with modified     # contents of the original snippet. Ignore paths that     # already contain a user snippet to prevent overwriting     # previously generated snippets.     snippet_id = entry["IDECodeSnippetIdentifier"]     snippet_path = user_snippet_path + "/" + snippet_id + ".codesnippet"     if os.path.exists(snippet_path):         print(snippet_path + " already exitsts: Skipping.")         continue      print("Writing " + snippet_path)      # Marks the snippet as a user snippet. Xcode will     # crash if a user snippet and a system snippet share     # the same identifier.     entry["IDECodeSnippetUserSnippet"] = True      # Given two snippets with the same identifier,     # Xcode will only show the snippet with the higher     # "version number". This effectively hides the     # default version of the snippet.     entry["IDECodeSnippetVersion"] += 1      plistlib.writePlist(entry, snippet_path)  print("Done writing snippets.") 

You'll notice that it doesn't actually change any of Xcode's internal files. It just adds files, and Xcode is smart enough to use the added files instead of the original snippets. You can roll back to the originals at any time by simply deleting the user version of the snippet. You can also run the script as many times as you want without worrying about overwriting any user snippets generated by previous runs of the script.

like image 77
Matt Wilding Avatar answered Oct 04 '22 01:10

Matt Wilding


There's a great little tool called "Snippet Edit". I just tried it, and highly recommend it. Apparently it used to be a for-pay app, but the author is now giving it away for free.

http://cocoaholic.com/snippet_edit/

like image 28
software evolved Avatar answered Oct 04 '22 02:10

software evolved