Reputation: 2154
Anyone use default code snippets in Xcode? There are too many useless code snippets there, such that I don't even want to create my snippets there. So looked for answers on ST, but they did not help much.
Is there any improvement so I can remove code snippets in Xcode 6?
For further question, can I import or export customised snippets, so I can use them across different computers?
Thanks a lot,
Upvotes: 3
Views: 1389
Reputation: 63667
In Xcode 10 the default snippets are at
/Applications/Xcode.app/Contents/PlugIns/IDESourceEditor.framework/Versions/A/Resources/SystemCodeSnippets.codesnippets
To remove contents change it to
<?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">
<array></array>
</plist>
Upvotes: 3
Reputation: 12446
Yes you can delete the default snippets.
Since snippets are saved in XML
in a plist
file at the path:
/Applications/Xcode.app/Contents/Frameworks/IDEKit.framework/Versions/A/Resources/SystemCodeSnippets.codesnippets
You can create an empty snippets file in TextEdit
with the following code:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<array></array>
</plist>
And save it as SystemCodeSnippets.codesnippets
.
Replace it with the file in the folder named above and there are no default system snippets anymore.
Upvotes: 5
Reputation: 7826
The answer is: YES,
These code snippets generated by users are saved in path
~/Library/Developer/Xcode/UserData/CodeSnippets
You can simply pass them around to save and restore them. So you can share them with others,on the other hand, use them in other Mac.
Upvotes: 1
Reputation: 151
I'm only using swift snippets and also wanted to remove all these c, c++ and objC snippets. A cleanup in the file "/Applications/Xcode.app/Contents/Frameworks/IDEKit.framework/Versions/Current/Resources/SystemCodeSnippets.codesnippets" seemed to work: I had only three swift snippets left in the code snippet library panel. Filtering and selection of these snippets worked but Xcode crashed several times when hitting the enter key, while in the filter field of the snippets. So I restored the old file and had the full list again.
Searching here in stackoverflow convinced me not to delete the default snippets but to store them with the same ID and a higher version number as a user snippet.
As a little programming task I created a OS X Projekt and with the following code right in the applicationDidFinishLaunching of the generated AppDelegate.swift:
func applicationDidFinishLaunching(aNotification: NSNotification) {
let filepathOfSystemSnippets = "/Applications/Xcode.app/Contents/Frameworks/IDEKit.framework/Versions/Current/Resources/SystemCodeSnippets.codesnippets"
let folderForUserSnippets = "\(NSHomeDirectory())/Library/Developer/Xcode/UserData/CodeSnippets"
print("reading \(filepathOfSystemSnippets)")
let arrayOfDicts = NSMutableArray(contentsOfFile: filepathOfSystemSnippets)
// print("arry has \(arrayOfDicts?.count) entries")
for (ix, dict) in arrayOfDicts!.enumerate() {
let userDict = NSMutableDictionary(dictionary: dict as! [NSObject : AnyObject])
userDict["IDECodeSnippetVersion"] = 2
userDict["IDECodeSnippetTitle"] = "free user snippet \(ix) "
userDict["IDECodeSnippetSummary"] = ""
userDict["IDECodeSnippetContents"] = ""
userDict["IDECodeSnippetLanguage"] = "Xcode.SourceCodeLanguage.Swift"
userDict["IDECodeSnippetCompletionPrefix"] = ""
userDict["IDECodeSnippetUserSnippet"] = true
if let id = dict["IDECodeSnippetIdentifier"] as? String {
let pathOfUserSnippet = "\(folderForUserSnippets)/\(id).codesnippet"
if NSFileManager.defaultManager().fileExistsAtPath(pathOfUserSnippet){
print("snippet with id \(id) already exists in \(pathOfUserSnippet) -> don't write.")
} else {
userDict.writeToFile(pathOfUserSnippet, atomically: false)
// print("write snippet with id \(id) in \(pathOfUserSnippet)")
}
}
}
}
I use the hardcoded folder names I found in the answers. So if they change in future versions of Xcode the may be needed to change.
After a run of the application the users snippet folder is populated with about 60 cleaned snippets ready to be filled with own source code.
Upvotes: 2
Reputation: 12446
There is an app called SnippetEdit
It can transform all your default Xcode snippets to user snippets. I tried it with Xcode 7 Beta and Xcode 6.
But you should not delete the user snippets in Xcode, when they were default snippets. Because Xcode just recreates them :-(.
At least you can change them that way. Change the title and the content.
Upvotes: 2