Custom Xcode File Template
When you create a new empty file in Xcode—e.g., a Swift source file—it usually comes pre-populated with an appropriate header content such as
//
// FooBar.swift
// MyApp
//
// Created by Jane Doe on 10/29/23.
//
import SwiftUIWhat if you don’t like the content of this header and want it to look different, including not be there at all?
I did and so I started poking around for ways to do that. It turned out this is a not so well documented area so I recorded here what I’ve learned and done so far, and, will keep updating as I learn more.
Location of Standard Xcode Templates
Broadly speaking, the content of these headers seems to be controlled by templates that can be found in the Xcode bundle:
% ls -al "/Applications/Xcode.app/Contents/Developer/Library/Xcode/Templates/File Templates/MultiPlatform/Source"
total 0
drwxr-xr-x 13 root wheel 416 Apr 17 05:59 .
drwxr-xr-x 6 root wheel 192 Apr 17 05:59 ..
drwxr-xr-x 5 root wheel 160 Apr 17 06:46 C File.xctemplate
drwxr-xr-x 5 root wheel 160 Apr 17 06:46 C++ File.xctemplate
drwxr-xr-x 4 root wheel 128 Apr 17 06:46 Header File.xctemplate
drwxr-xr-x 4 root wheel 128 Apr 17 06:46 Metal File.xctemplate
drwxr-xr-x 7 root wheel 224 Apr 17 06:46 Objective-C File.xctemplate
drwxr-xr-x 5 root wheel 160 Apr 17 06:46 Objective-C new superclass.xctemplate
drwxr-xr-x 4 root wheel 128 Apr 17 06:46 Playground Page.xctemplate
drwxr-xr-x 4 root wheel 128 Apr 17 06:46 Sources Folder Swift File.xctemplate
drwxr-xr-x 4 root wheel 128 Apr 17 06:46 Swift File.xctemplate
drwxr-xr-x 7 root wheel 224 Apr 17 06:46 UI Test Case Class.xctemplate
drwxr-xr-x 7 root wheel 224 Apr 17 06:46 Unit Test Case Class.xctemplate
% ls -al "/Applications/Xcode.app/Contents/Developer/Library/Xcode/Templates/File Templates/MultiPlatform/Source/Swift File.xctemplate"
total 16
drwxr-xr-x 4 root wheel 128 Apr 17 06:46 .
drwxr-xr-x 13 root wheel 416 Apr 17 05:59 ..
-rw-r--r-- 1 root wheel 818 Apr 17 05:59 TemplateInfo.plist
-rw-r--r-- 1 root wheel 38 Apr 17 05:59 ___FILEBASENAME___.swiftYou can poke around and start getting a feeling for what’s going on. The key observation is these underscore-surrounded key string such as ___FILEBASENAME___.
Get Giik Web Developer’s stories in your inbox
Join Medium for free to get updates from this writer.
There is nothing I was able to find in the Developer Documentation that is part of Xcode. However, the older online Xcode help page at Welcome to Xcode contains some useful information:
Using this Text Macros Reference is at least helpful in beginning to decipher the templates content.
Making My Own Template
Apparently, you can add your own templates under ~/Library/Developer/Xcode/UserData/IDETemplateMacros.plist (the location and name for the file must be as shown, I suppose). How do you find this out from any documentation? I couldn’t find any so I can’t be of help but definitely please let me know if you are aware of where this is documented. I pieced it together from answers of questions on online forums, such as this one.
As soon as I created the IDETemplateMacros.plist with my custom desired value for FILEHEADER content, Xcode started using it.
You can experiment by putting all the documented text macros in the template and seeing what their values are (just avoid FILEHEADER because you cannot use it in its own definition).
I ended up with the following
<?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>ORGANIZATIONNAME</key>
<string>My Org Name</string>
<key>FILEHEADER</key>
<string>
// ___FILEBASENAME___.swift
//
// Created for ___PROJECTNAME___
// on ___DATE___
// by ___ORGANIZATIONNAME___</string>
</dict>
</plist>Note the newline after <string> on the line immediately following <key>FILEHEADER</key> . Xcode appears to actually append the [macro-expanded] value of FILEHEADER to a comment starting with // . So you need one line with whatever (appended to the // ) and any other number of lines following it—if you want these next lines to be Swift comments then they should start with //.
This works for creating a new Swift source file specifically, from the file templates that pop up when create a new file in Xcode. How to affect other types of files—I haven’t gotten so far yet. The forum post linked above goes into more detail on that but I have not tried the advice yet.









