Change templates in Xcode

50,462

Solution 1

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

Solution 2

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

Solution 3

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].

Solution 4

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!

Solution 5

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

Share:
50,462

Related videos on Youtube

Misha M
Author by

Misha M

Updated on July 08, 2022

Comments

  • Misha M
    Misha M almost 2 years

    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.

  • mk12
    mk12 almost 15 years
    I changed the actual templates in /Developer .. I don't have a "Developer" folder in my library.
  • Chris Hanson
    Chris Hanson almost 15 years
    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.
  • mk12
    mk12 almost 15 years
    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/...
  • v01pe
    v01pe over 12 years
    @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.
  • Martin Tuskevicius
    Martin Tuskevicius over 12 years
    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.
  • Elisabeth
    Elisabeth about 12 years
    For just changing the company name you can also change your user contact info: stackoverflow.com/questions/10245689/…
  • Takagi
    Takagi almost 12 years
    I use Xcode 4.4. I don't have the folder: ~/Library/Application Support/Developer
  • Stu P.
    Stu P. about 11 years
    Just dead wrong but I can't down this vote until my reputation is 125, help me out guys!
  • huyleit
    huyleit almost 11 years
    In Xcode 4.5, the path here: /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneO‌​S.platform/Developer‌​/Library/Xcode/Templ‌​ates
  • seufagner
    seufagner over 10 years
    In XCode5 you should copy templates to path: ~/Library/Developer/Xcode/Templates/Application/Project Templates. If Templates/Application/Project Templates not exists, create it
  • Yunus Nedim Mehel
    Yunus Nedim Mehel over 10 years
    @seufagner But the answer says don't modify anything under the developer hierarchy?
  • Chris Hanson
    Chris Hanson over 10 years
    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.
  • Lukas Kalinski
    Lukas Kalinski over 10 years
    If you want the template to appear under iOS too, then add "com.apple.platform.iphoneos" to the Platforms list in TemplateInfo.plist.
  • carbo18
    carbo18 over 8 years
    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.
  • ajmccall
    ajmccall over 8 years
    If you change files to this folder, will they be overwritten when new versions of xCode are installed?
  • carbo18
    carbo18 over 8 years
    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.
  • Lapidus
    Lapidus about 8 years
    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!
  • carbo18
    carbo18 about 8 years
    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.
  • Brynjar
    Brynjar about 8 years
    As of xcode 7.3.1 this doesn't appear to work anymore
  • Michael
    Michael over 5 years
    Does this still apply to Xcode 10? And where are the original templates (so I can copy them to "~")?
  • Isaaс Weisberg
    Isaaс Weisberg about 5 years
    @StuartP. joining the cause i guess
  • BrunoLoops
    BrunoLoops over 4 years
    @Michael this is still working for Xcode 10 and 11, the updated path would be: ~/Library/Developer/Xcode/Templates/File Templates/Source
  • Byron Coetsee
    Byron Coetsee over 4 years
    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?
  • IceFire
    IceFire about 3 years
    @BrunoLoops hm, no, in Xcode 12, this folder does not exist
  • IceFire
    IceFire about 3 years
    Different folders in Xcode12, answer here might be more up to date: stackoverflow.com/a/67682572/2573127 (and works for me)
  • BrunoLoops
    BrunoLoops about 3 years
    @IceFire you're right, I've commented that applied to 10 and 11 but the answer isn't specific :(