I’m not a fan of the default file header comment Xcode adds to new source files. I’d argue that most of the information in the file header is either irrelevant or better tracked through source control. Moreover, these comments can quickly become outdated as files and projects get renamed.
In my personal projects, my first step after creating a new file is always to delete this header comment.
Until now, that is, because Xcode 9 allows you to customize the file header and other so-called text macros using a plist file. The process is described in Xcode Help on a page titled Customize text macros:
Create a property list file named
For every text macro you want to customize, add a new key to the plist’s dictionary. For example, to change the default file header, add an entry with the key
Xcode’s plist editor only displays a single line for the value, but you can insert newlines with Option + Return.
See Text macros reference below for a full list of available macros. You can use other text macros in a macro’s value by wrapping them with three underscores, such as
___DATE___. Some macros can also be customized with a
:modifiersyntax. See Text macro format reference below for more on this.
Copy the file to one of the following locations. The directory specifies in which context the customized text macros should be applied:
For a single project and user:
For all team members in a single project:
For all projects in a workspace for a single user:
For all projects in a workspace for all team members:
For everything you work on, regardless of project:
When you now create a new file, it looks like this:
Note that for the
FILEHEADER macro, Xcode currently (Xcode 9 beta 3) adds comment markers (
//, with no space) to the first line but not to subsequent lines. You’ll need to include those manually in the macro text. I’m not sure if this is the intended behavior or a bug (it looks like a bug to me).
Unfortunately, this also means that you can’t make the file header comment go away completely — even if you set it to an empty string in the plist, new files will begin with an empty comment line. I hope this gets fixed in a future version. I filed a bug at rdar://33451838.
Text macros reference
Here’s a list of all available text macros in Xcode 9. I copied this almost verbatim from the Text macros reference page in Xcode Help.
- Xcode’s default copyright string that uses the organization name you’ve set in your project, e.g. "Copyright © 2017 MyCompany. All rights reserved."
- The current date.
- The version of Swift used for the default toolchain.
- The name of the current file without any extension.
- The name of the current file encoded as a C identifier.
- The text placed at the top of every new text file.
- The full name of the current file.
- The full name of the current macOS user.
The entry for the human readable copyright string in the Info.plist file of a macOS app target. For example, a valid value is:
<key>NSHumanReadableCopyright</key> <string>Copyright © 2017 Apple, Inc. All rights reserved.</string>
Notice that the value includes a newline.
- The company name for the team used for the provisioning profile.
- The name of the package built by the current scheme.
- A C-identifier encoded version of the package name built by the current scheme.
- The app name of the product built by the current scheme.
- The name of the current project.
- The version of macOS that is running Xcode.
- The name of the current target.
- The current time.
- The login name for the current macOS user.
Return a unique ID. The first time the macro is used, it will generate the ID before returning it. You can use the macro to create multiple unique IDs by using a modifier. Each modifier returns the an ID that is unique for that modifier.
For example, the first time
UUID:firstPurposeit will generate and return a unique ID for that macro and modifier combination. Subsequent uses of
UUID:firstPurposereturn the same ID. Adding
UUID:secondPurposewill generate and return a different ID that will be unique to
UUID:secondPurpose, and different from the ID for
- The name of the current workspace. If there is only one project open, then the name of the current project.
- The current year as a four-digit number.
Text macro format reference
Copied from the Text macro format reference page in Xcode Help.
A text macro can contain any valid unicode text. It can also contain other text macros.
Including other text macros
To include another text macro, add three underscore (_) characters before and after the macro name:
Modifying text macro expansion
You can modify the final expansion of the text macro by adding one or more modifiers. Add a modifier to a text macro by placing a colon (:) at the end of the macro followed by the modifier. Add multiple modifiers by separating each one with a comma (,).
For example, the following macro will remove the path extension from the
To turn the modified macro above into a valid C identifier, add the identifier macro:
- Replaces any non-bundle identifier characters with a hyphen (-).
- Removes the last path component from the expansion string.
- Removes any path extension from the expansion string.
- Removes any trailing dots (.).
- Replaces any non-C identifier characters with an underscore (_).
- Returns just the last path component of the expansion string.
- Returns the path extension of the expansion string.
- Replaces any non-rfc1034 identifier characters with a hyphen (-).
Replaces special xml characters with the corresponding escape string. For example, less-than (
<) is replaced with