113

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.

icodebuster
  • 8,890
  • 7
  • 62
  • 65
Misha M
  • 10,979
  • 17
  • 53
  • 65

10 Answers10

111

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

BrunoLoops
  • 554
  • 5
  • 20
Chris Hanson
  • 54,380
  • 8
  • 73
  • 102
  • I changed the actual templates in /Developer .. I don't have a "Developer" folder in my library. – mk12 Aug 20 '09 at 21:57
  • 6
    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. – Chris Hanson Aug 23 '09 at 01:06
  • 3
    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/... – mk12 Aug 28 '09 at 20:45
  • 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. – Martin Tuskevicius Feb 24 '12 at 00:01
  • 1
    I use Xcode 4.4. I don't have the folder: ~/Library/Application Support/Developer – neoneye Jul 31 '12 at 08:44
  • 5
    In Xcode 4.5, the path here: /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/Library/Xcode/Templates – huyleit Aug 14 '13 at 01:05
  • 6
    In XCode5 you should copy templates to path: ~/Library/Developer/Xcode/Templates/Application/Project Templates. If Templates/Application/Project Templates not exists, create it – seufagner Oct 12 '13 at 05:28
  • @seufagner But the answer says don't modify anything under the developer hierarchy? – Yunus Nedim Mehel Feb 12 '14 at 09:06
  • 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. – Chris Hanson Feb 16 '14 at 10:24
  • 3
    If you want the template to appear under iOS too, then add "com.apple.platform.iphoneos" to the Platforms list in TemplateInfo.plist. – Lukas Kalinski Feb 27 '14 at 12:17
  • 1
    Does this still apply to Xcode 10? And where are the original templates (so I can copy them to "~")? – Michael Oct 06 '18 at 11:40
  • @Michael this is still working for Xcode 10 and 11, the updated path would be: `~/Library/Developer/Xcode/Templates/File Templates/Source` – BrunoLoops Jan 08 '20 at 17:34
  • 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? – Byron Coetsee Mar 23 '20 at 15:54
  • @BrunoLoops hm, no, in Xcode 12, this folder does not exist – IceFire May 25 '21 at 05:55
  • Different folders in Xcode12, answer here might be more up to date: https://stackoverflow.com/a/67682572/2573127 (and works for me) – IceFire May 25 '21 at 06:09
  • @IceFire you're right, I've commented that applied to 10 and 11 but the answer isn't specific :( – BrunoLoops May 26 '21 at 17:07
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

pconcepcion
  • 5,591
  • 5
  • 35
  • 53
  • 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. – carbo18 Jan 01 '16 at 23:08
  • As of xcode 7.3.1 this doesn't appear to work anymore – Brynjar Jun 01 '16 at 08:22
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].

logancautrell
  • 8,762
  • 3
  • 39
  • 50
Barry Wark
  • 107,306
  • 24
  • 181
  • 206
  • For just changing the company name you can also change your user contact info: http://stackoverflow.com/questions/10245689/xcode-4-change-company-name-and-username – Elisabeth Apr 22 '12 at 21:04
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!

v01pe
  • 1,096
  • 2
  • 11
  • 19
  • 3
    @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. – v01pe Jan 21 '12 at 00:01
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

carbo18
  • 468
  • 3
  • 8
  • If you change files to this folder, will they be overwritten when new versions of xCode are installed? – ajmccall Feb 01 '16 at 13:09
  • 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. – carbo18 Feb 07 '16 at 21:28
  • 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! – Lapidus May 16 '16 at 21:14
  • 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. – carbo18 May 18 '16 at 03:51
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

tanyanzhi
  • 149
  • 2
  • 3
2

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

It's much easier than modifying the templates.

ChrisJF
  • 6,822
  • 4
  • 36
  • 41
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.

Manav
  • 10,094
  • 6
  • 44
  • 51
1

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

Alex Cio
  • 6,014
  • 5
  • 44
  • 74
-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)

Denis Kutlubaev
  • 15,320
  • 6
  • 84
  • 70