136

Whenever I create a new .cpp/.h file in Xcode a comment is added to the top of the file. For example:

/*
 *  <file>.cpp
 *  <Name of project>
 *
 *  Created by <My name> on <Date>.
 *  Copyright <Year and company>. All rights reserved.
 *
 */

I want to change the default comment to be another license, like GPL/LGPL/ or something else. Is there somewhere I can change this behavior in Xcode?

Robert Harvey
  • 178,213
  • 47
  • 333
  • 501
Erik Rothoff
  • 4,826
  • 9
  • 41
  • 59
  • Possible duplicate of [Change templates in Xcode](https://stackoverflow.com/questions/33720/change-templates-in-xcode) – Fattie Sep 28 '19 at 14:51

15 Answers15

112

With Xcode 9 there is a built-in option which you can find in the details below.

  1. Create a property list file named IDETemplateMacros.plist
  2. Add a FILEHEADER value to the Root and set its string value with your copyright text like Copyright © 2017 ...
  3. Copy the file to one of the following locations
    • For a single project and user <ProjectName>.xcodeproj/xcuserdata/[username].xcuserdatad/IDETemplateMacros.plist
    • For all team members in a single project <ProjectName>.xcodeproj/xcshareddata/IDETemplateMacros.plist
    • For all projects in a workspace for a single user <WorkspaceName>.xcworkspace/xcuserdata/[username].xcuserdatad/IDETemplateMacros.plist
    • For all projects in a workspace for all team members <WorkspaceName>.xcworkspace/xcshareddata/IDETemplateMacros.plist
    • For everything you work on, regardless of project ~/Library/Developer/Xcode/UserData/IDETemplateMacros.plist
  4. Create a new file - you should see the new copyright header

Sample IDETemplateMacros.plist for copy and paste:

<?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">
<dict>
    <key>FILEHEADER</key>
    <string>
//  Created by Your Name on 29.12.17.
//  Copyright © 2017 Your Company. All rights reserved.
//  </string>
</dict>
</plist>
Legonaftik
  • 1,350
  • 3
  • 17
  • 33
abdullahselek
  • 7,893
  • 3
  • 50
  • 40
  • 10
    To add dynamic data, see the Text Macros Reference: https://help.apple.com/xcode/mac/9.0/index.html?localePath=en.lproj#/dev7fe737ce0 For examples, see: https://oleb.net/blog/2017/07/xcode-9-text-macros/ – mcw Mar 15 '18 at 02:28
  • Copyright symbol doesn't work when copy-pasting the above example into Vim. Replace it with `©` to make it work. – Alex Yursha Dec 10 '18 at 06:03
  • An addition note to this if you have subprojects within your project you will have to add the file to each subproject. – Dean Kelly Nov 20 '20 at 17:10
  • Do you know if there is a way to have absolutely no header at all? If I use ``, then files still always being with an initial empty comment line like this: `//`. Thanks! – Benjohn Apr 06 '22 at 11:06
  • I came across to this post https://jasonzurita.com/remove-xcode-file-header-comments/ but I didn't try. – abdullahselek Apr 07 '22 at 18:15
  • Thanks. This is mostly right and very helpful. However, the `//` is inserted automatically on the first line. So mine -- which is just one line -- reads ` My Header!` for the value line, all on one line. – Dan Rosenstark Mar 16 '23 at 21:38
84

First try changing your Name and Organization your contact card in Address Book.

If that doesn't work, open System Preferences > Users & Groups > Right click on your user (System Preferences must already be unlocked) > Advanced Options... > Change the name in the Full Name text box.

To change the organization name in Xcode click on the project file so it is selected (left sidebar of Xcode in the Project navigator) > In the File Inspector (right sidebar of Xcode) change the Organization text box.

Organization text box

ChrisJF
  • 6,822
  • 4
  • 36
  • 41
33

This blog post has some good steps for modifying file templates in Xcode 4.3:

http://error-success.piku.org.uk/2012/04/27/how-to-make-xcode-4-3-2-templates/

The /Developer folder no longer exists, so you copy the templates from within the Xcode.app bundle located in /Applications:

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

And place your modified copy here, as before:

~/Library/Developer/Xcode/Templates/File\ Templates/

UPDATE: I decided to write a script that would extract the built in Xcode templates and replace the headers. Source and instructions can be found at the following url:

https://github.com/royclarkson/xcode-templates

Roy Clarkson
  • 2,159
  • 18
  • 14
27
/Applications/Xcode.app/Contents/Developer/Library/Xcode/Templates

any update of your SDK will wipe changes here so keep your template backed up somewhere else

conorgriffin
  • 4,282
  • 7
  • 51
  • 88
  • http://www.macresearch.org/custom_xcode_templates Here I found information on how to create new File Templates. (Though it was a bit self-explanatory) – Erik Rothoff Mar 04 '10 at 18:59
  • 1
    this is good if you want to change each individual template, but I think it would be easier to just modify the 'organisation name'. It works across the board for all the templates. For anyone who is interested see my answer below. – Eric Brotto Feb 19 '11 at 17:48
  • Ok, this worked. Now does anyone know where templates get their env variables and how do I add one? Examples from template: `___FULLUSERNAME___`, `___PROJECTNAME___` – AzaFromKaza Sep 08 '17 at 04:12
  • The list of available text header macros can be found here https://help.apple.com/xcode/mac/current/#/dev7fe737ce0 – Vinayak Dec 05 '18 at 08:59
  • So hard to post the full path? it usually is: `/Applications/Xcode.app/Contents/Developer/Library/Xcode/Templates` – IceFire Apr 23 '20 at 08:46
  • fixed it, it's changed over the years so the answer was out of date :) @IceFire – conorgriffin Apr 23 '20 at 18:05
  • Anything under /Developer might be changed without warning by apple - see answer by mmmmmm – occulus Jun 25 '20 at 11:00
  • This doesn't seem like a complete answer to me – bobby123uk Dec 16 '20 at 11:06
  • 1
    The answer is outdated since I wrote it 10 years ago :) There are more up-to-date answers below but this is the accepted answer because it worked at the time – conorgriffin Dec 16 '20 at 11:15
16

Don't edit anything in /Developer as Apple can overwrite this at any time.

The following works for Xcode 4, 5 and 6 except later source differs and is under /Applications

Instead copy the templates that you want to change from /Applications/Xcode<version>.app/Contents/Developer/Library/Xcode/Templates to ~/Library/Developer/Xcode/Templates/File Templates and then edit the files keeping the same directory structure but edit the directory name that is the template to not show up a a duplicate in Xcode.

e.g for a new category of C/C+ files in Xcode 5 copy /Applications/Xcode.app/Contents/Developer/Library/Xcode/Templates/File Templates/C and C++ to ~/Library/Developer/Xcode/Templates/File Templates/GPL C and C++

this process is copied from Red Glasses's blog

For Xcode 4 the source path is or for Xcode 4 /Developer/Library/Xcode/Templates/File Templates

mmmmmm
  • 32,227
  • 27
  • 88
  • 117
  • In your third paragraph it should say _~/Library/Developer/Xcode/Templates/File Templates_ – ajh158 Dec 30 '11 at 06:55
  • @ajh158 I added an extra sub directory or is there another issue? – mmmmmm Dec 30 '11 at 10:19
  • 4
    No, you are missing _/Developer_ from the user path in the third paragraph. _~/Library/Xcode/Templates/File Templates_ should be _~/Library/Developer/Xcode/Templates/File Templates_. I upvoted your answer, it helped me, I just thought I'd comment because I wasted a little time puzzling over why I didn't have a _~/Library/Xcode_ directory. – ajh158 Dec 30 '11 at 19:30
10

– Press ⌘ 1 to display the File Navigator.
– Click on the name of your project at the top of the File Navigator.
– Press ⌘⌥⇧ ⏎ (that's Command Option Shift Return) to view the Version Editor.
– Make sure that the Comparison View is shown(View > Version Editor > Show Comparison View).

This lets you see the raw text of your project's project.pbxproj file.

– Press ⌘ f to search the project.pbxproj file, paste in ORGANIZATIONNAME and press Return.

This is where the name of the organization that follows the copyright text is defined.


Or open Xcode's plist at

~/Library/Preferences/com.apple.dt.Xcode.plist

And change the organizationName key's value.


Or select the project from the Navigator pane and display the File Inspector with ⌘⌥ 1.

Look at the Organization value under Project Document. Change the value there.

Alex Zavatone
  • 4,106
  • 36
  • 54
10

In Xcode 12.4

Using Xcode, create a plist file named IDETemplateMacros.

Save it to the Desktop initially while you configure it.

If you cannot see the file in the Project Navigator then from the Xcode menu, select View > Navigators > Project

Right click on the filename and select Open as > Source Code

The following is a template that works nicely for me so chances are it will for you too.

<?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">
    <dict>
        <key>FILEHEADER</key>
        <string>
//
// ___FILENAME___
// ___PACKAGENAME___
//
// Created by ___FULLUSERNAME___ on ___DATE___
// Copyright © ___YEAR___ ___FULLUSERNAME___. All rights reserved.
//
        </string>
    </dict>
</plist>

Copy and paste the above overwriting the existing contents.

Save the file and close it.

In Finder navigate to the following folder:

~/Library/Developer/Xcode/UserdData/

Drag the file from your Desktop to this Folder and then test using Xcode to create a new project.

Chrispy
  • 341
  • 3
  • 5
7

It's very simple:

  1. Open up terminal
  2. In one line, write the following:

    defaults write com.apple.Xcode PBXCustomTemplateMacroDefinitions -dict ORGANIZATIONNAME "Blah, Inc"
    

You don't have to worry about changing directories beforehand or anything else. It works instantly.

Eric Brotto
  • 53,471
  • 32
  • 129
  • 174
  • 3
    This only changes the organisation and not the license etc. – mmmmmm Dec 03 '11 at 20:03
  • 5
    In xCode 6 you can change the ORGANIZATIONNAME for the project from the GUI. Select the project, and select the File Inspector (default position is right side of screen), and you should see the "Organization" file under "Project Document" – Cesar Maiorino Apr 23 '15 at 19:06
  • 1
    Hmm. In Xcode 10.x, there are interesting results. `$ read com.apple.Xcode PBXCustomTemplateMacroDefinitions` and you get `The domain/default pair of (com.apple.Xcode, PBXCustomTemplateMacroDefinitions) does not exist`. And a simple read also causes an error. `$ read com.apple.Xcode` returns `Domain com.apple.Xcode does not exist`. Use `com.apple.dt.Xcode` instead. – Alex Zavatone Jan 01 '21 at 17:51
5

Works with Xcode 14

Say you want to modify (or get rid of) the Xcode Header comment.

  • First open Xcode, Use File > New File... (⌘N) and choose Property List from the file templates.
  • Name it file IDETemplateMacros.plist
  • On the navigator, select the file as right-click Open as source code. Xcode will show us the property file as text. Property files are really just XML files.
  • Copy paste the following content:
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>FILEHEADER</key>
    <string>Created for ___PROJECTNAME___ in ___YEAR___
// Using Swift ___DEFAULTTOOLCHAINSWIFTVERSION___</string>
</dict>
</plist>

On the root dict we have added an entry with key FILEHEADER and a two-lines string as a value:

Created for ___PROJECTNAME___ in ___YEAR___
// Using Swift ___DEFAULTTOOLCHAINSWIFTVERSION___

Save the file IDETemplateMacros.plist on the folder:

~/Library/Developer/Xcode/UserData/

That's it, now when creating a new project called MyProject, the header will be:

//Created for MyProject in 2022
// Using Swift 5.0

Note1. There is a list of macros on https://help.apple.com/xcode/mac/9.0/index.html?localePath=en.lproj#/dev7fe737ce0

Note 2. As an example you can write:

 Created ___DATE___
// ___COPYRIGHT___

Note that there is a leading space but you do not include the // for the comment on the first line.

Note 3. For a more list of options see: https://useyourloaf.com/blog/changing-xcode-header-comment/

Roland Lariotte
  • 2,606
  • 1
  • 16
  • 40
joan
  • 2,407
  • 4
  • 29
  • 35
3

Change the details in your Address Book - add a company name. It will pick it up from there.

bandejapaisa
  • 26,576
  • 13
  • 94
  • 112
2

Try modifying this file:

/Developer/Library/Xcode/File\ Templates/C\ and\ C++/Header\ File.pbfiletemplate/header.h
cit
  • 2,465
  • 5
  • 28
  • 36
0

I just changed the template of a SwiftUI file, you have to open Xcode's Contents and look for the template files you want to change. To change the SwiftUI template the path is this:

/Applications/Xcode.app/Contents/Developer/Library/Xcode/Templates/File\ Templates/MultiPlatform/User\ Interface/SwiftUI\ View.xctemplate

You nedd open with sudo

0

In Xcode 4.2, they're here:

/Developer/Library/Xcode/Templates/File Templates
Danyal Aytekin
  • 4,106
  • 3
  • 36
  • 44
-1

For AppCode users:

  1. Go to preferences (CMD + ,)
  2. Editor-File and Code Templates
  3. Change Scheme to Project
  4. Modify Files and/or Includes as you need.
  5. add to git .idea/fileTemplates if needed
Kirow
  • 1,077
  • 12
  • 25
-1

You can override the text macros globally, or for an individual workspace or project. You can also decide to keep the macros for a single user or share it with all users.

The full list of locations that Xcode searches, in order of priority:

Project - single user

<ProjectName>.xcodeproj/xcuserdata/[username].xcuserdatad/

Project - shared by all users

<ProjectName>.xcodeproj/xcshareddata/

Workspace - single user

<WorkspaceName>.xcworkspace/xcuserdata/[username].xcuserdatad/

Workspace - shared by all users

<WorkspaceName>.xcworkspace/xcshareddata/

Globally for Xcode

~/Library/Developer/Xcode/UserData/

I don't remember what was the default template but you can make a template in any way you like using textMacros:

//  ___FILENAME___
//  ___PACKAGENAME___
//
//  Created by ___FULLUSERNAME___ on ___DATE___.
//  ___COPYRIGHT___
//

There are some other textMacros if you like more customization, but these are enough for making something like the default one.

You can create the file yourself if no files found at the path

Mojtaba Hosseini
  • 95,414
  • 31
  • 268
  • 278
  • This question isn't about text macros. How does this answer the question? – Robin Daugherty Aug 18 '22 at 14:41
  • A path to the header file and a way to change the text. What else do you need? @RobinDaugherty – Mojtaba Hosseini Aug 18 '22 at 16:33
  • The question is "Whenever I create a new .cpp/.h file in Xcode a comment is added to the top of the file. I want to change the default comment". You described how to create a text macro. Is there a step missing? – Robin Daugherty Aug 18 '22 at 20:49
  • Xcode creates the default comments in the header from the files at the pathes which I have provided. Changing them will cause changing in the default header. And thats the point :) – Mojtaba Hosseini Aug 18 '22 at 21:11