Change templates in Xcode
Asked Answered
R

10

114

How would I change the initial templates created by Xcode when creating a new Cocoa Class.

I am referring to the comments and class name created when using Xcode's new class wizard.

Reasonless answered 29/8, 2008 at 0:44 Comment(0)
G
113

You wouldn't change the existing templates. In other words, don't modify anything under the /Developer hierarchy (or wherever you installed your developer tools).

Instead, clone the templates you want to have customized variants of. Then change their names and the information in them. Finally, put them in the appropriate location in your account's Library/Application Support folder, specifically:

  • File templates: ~/Library/Application Support/Developer/Shared/Xcode/File Templates/
  • Target templates: ~/Library/Application Support/Developer/Shared/Xcode/Target Templates/
  • Project templates: ~/Library/Application Support/Developer/Shared/Xcode/Project Templates/

That way they won't be overwritten when you install new developer tools, and you can tweak them to your heart's content.

Update For newer versions of Xcode the updated path will be: ~/Library/Developer/Xcode/Templates/File Templates/Source

Glaucoma answered 29/8, 2008 at 1:14 Comment(16)
I changed the actual templates in /Developer .. I don't have a "Developer" folder in my library.Decoct
If you don't have the paths above, you can create them. Don't change the templates in /Developer, because they won't necessarily survive uninstall and reinstall of the tools. Treat it like /System.Glaucoma
I still just change the ones in /Developer because then it actually changes it instead of creating new ones which are almost the same. Using the home directory library doesn't overwrite the /Developer one, it considers it a different template. I only use the home library for creating new templates, not changing existing ones. I can remember to move the templates before installing a new xcode, and by the way its not ~/Library/Developer/...., its ~/Library/**Application Support/**Developer/...Decoct
Yeah I have to do that too, because adding it in the home directory just creates a new template. As a side question, where can I find the template for the main file? Because when I write C, 'main.c' is written by default and it uses some separate template.Theogony
I use Xcode 4.4. I don't have the folder: ~/Library/Application Support/DeveloperForepleasure
In Xcode 4.5, the path here: /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/Library/Xcode/TemplatesMaurizio
In XCode5 you should copy templates to path: ~/Library/Developer/Xcode/Templates/Application/Project Templates. If Templates/Application/Project Templates not exists, create itLodgings
@Lodgings But the answer says don't modify anything under the developer hierarchy?Almaalmaata
In the answer I mean don't modify the files under /Developer (for Xcode 4.2.1 or earlier) or inside Xcode.app itself (for Xcode 4.3 or later). Note that I'm using Unix filesystem conventions in describing these: /Developer, because it starts with a leading /, means it's an absolute path. ~ on the other hand means the user's home directory.Glaucoma
If you want the template to appear under iOS too, then add "com.apple.platform.iphoneos" to the Platforms list in TemplateInfo.plist.Forensic
Does this still apply to Xcode 10? And where are the original templates (so I can copy them to "~")?Urine
@Urine this is still working for Xcode 10 and 11, the updated path would be: ~/Library/Developer/Xcode/Templates/File Templates/SourceGiannagianni
Creating that path and simply adding a file into /Source doesn't seem to work. Do I name that file? Do I copy paste the whole folder structure of the default templates?Plotkin
@Giannagianni hm, no, in Xcode 12, this folder does not existTrinitroglycerin
Different folders in Xcode12, answer here might be more up to date: https://mcmap.net/q/169168/-change-xcode-11-template-files (and works for me)Trinitroglycerin
@Trinitroglycerin you're right, I've commented that applied to 10 and 11 but the answer isn't specific :(Giannagianni
L
47

This may be useful for somebody:

As for XCode 6 to XCode 9 the file templates are in:

/Applications/Xcode.app/Contents/Developer/Library/Xcode/Templates/File Templates/Source

Update: As @carbo18 noted, it's proably better to create the directory ~/Library/Developer/Xcode/Templates/File Templates/Source and put your custom templates there. The best way may be to use as base one of the ones in /Applications/Xcode.app/Contents/Developer/Library/Xcode/Templates/File Templates/Source

Luminescent answered 23/12, 2014 at 12:13 Comment(2)
It is better not to modify the content of the Xcode.app because Apple can just overwrite the template folders. A better option is to create the ~/Library/Developer/Xcode/Templates/File Templates/Source directory and put the template there. They will appear correctly in the dialog box.Rezzani
As of xcode 7.3.1 this doesn't appear to work anymoreGalvani
L
34

Xcode uses template files for file and project templates and does variable expansion in both at creation time.

Xcode 3.0 templates can be found in [Dev Installation]/Library/Xcode/, likely /Developer/Library/Xcode. If you want to modify these templates or add your own, use the following directories to save your new/modified templates so that they are not wiped out by future Developer Tool upgrades:

  • File templates: ~/Library/Developer/Shared/Xcode/File Templates/
  • Target templates: ~/Library/Developer/Shared/Xcode/Target Templates/
  • Project templates: ~/Library/Developer/Shared/Xcode/Project Templates/

I think that you can also use the /Library/Developer/Shared/Xcode/[File|Target|Project] Templates/ directory for templates shared by all users.

If you just want to change the MyCompanyName in the templates, the following command line will do the trick:

defaults write com.apple.Xcode PBXCustomTemplateMacroDefinitions '{ "ORGANIZATIONNAME" = "NewCompanyName";}'

A good tutorial on writing file templates is here [MacResearch.org].

Lightyear answered 29/8, 2008 at 1:44 Comment(1)
For just changing the company name you can also change your user contact info: #10246189Riviera
T
13

In Xcode 4 and Xcode 5 the user file templates can be placed at:
~/Library/Developer/Xcode/Templates/[Category]

[Category] can be used to categorize your templates (choose a name of your choise)

If the folder doesn't exist already, create it!

Tropaeolin answered 6/1, 2012 at 10:51 Comment(1)
@Dimitris: This are the internal templates… I was talking about the USER templates, if you want to create new ones or customized variants like Chris Hanson suggested, and don't mess around with the original ones.Tropaeolin
R
8

As of Xcode 7

  • File templates: ~/Library/Developer/Xcode/Templates/File Templates
  • Project templates: ~/Library/Developer/Xcode/Templates/Project Templates

For example if I place a Empty Application.xctemplate in ~/Library/Developer/Xcode/Templates/Project Templates/Application

It will appear in the template dialog as Template Dialog

Rezzani answered 1/1, 2016 at 23:23 Comment(4)
If you change files to this folder, will they be overwritten when new versions of xCode are installed?Dakar
No they wouldn't since you are creating your own folder and this folder is empty by default and is there for that purpose. I do wish Apple would document this stuff better. There is a drawback to this method, since it is per user. You would have to copy the templates to each user on the computer, if there are many. But this would be uncommon, I think, since most developers have their own computer.Rezzani
How would I change the default UIViewController template for Swift files? Dropping a file in ~/Library/Developer/Xcode/Templates/File Templates named UIViewContoller.swift does not work.. Thx!Pellerin
Adding a file template is a little more involved than just adding a swift file. The UIViewController.swift is actually created through the Cocoa Touch Template. If you want to look at what the Apple Template looks like so that you can create your own. Show the contents of the Xcode app. And Navigate to Contents/Developer/Library/Xcode/Templates/File Templates/Source and look at the Cocoa Class.xctemplate. Here you will get a better idea of how that template was created, you can then copy this template to the folder specified above and modify it to your specs.Rezzani
A
6

In xcode 5.0.1:

1.Go->Applications

2.right click "xcode" application

3.chose "Show Package Contents"

4.contents/Developer/Library/Xcode/Templates

Armed answered 3/12, 2013 at 2:23 Comment(0)
G
2

Right click on xCode and select Show Package contents, then go to contents/Developer/Library/Xcode/Templates. Here you can find the templates for all programming languages.

Here some visualization:


enter image description here


enter image description here

Ghost answered 28/8, 2014 at 12:7 Comment(0)
V
2

If you are simply looking to change the Author Name and Organization see this answer.

It's much easier than modifying the templates.

Valorievalorization answered 3/1, 2017 at 17:32 Comment(0)
T
1

For Xcode 4.4, none of the previously mentioned methods work. This gist provides a partial hacky solution. Please fork and enhance if you know a better way.

Takahashi answered 29/8, 2012 at 14:45 Comment(0)
P
-8

In XCode 4.5 right click on project, click Show File Inspector, then change Organization name in the file inspector's second tab (Project Document group)

Pappy answered 12/11, 2012 at 17:38 Comment(2)
Just dead wrong but I can't down this vote until my reputation is 125, help me out guys!Ike
@StuartP. joining the cause i guessDoubleton

© 2022 - 2024 — McMap. All rights reserved.