How to create an Xcode File template

Scott
2 min readJun 18, 2020

Credit to Brian Voong.

Create the Template

First create your template file in the same manner that you would create any file in Xcode.

Once in Xcode, click Command n to open the file selection view.

Some tokens you can use are as follows:

___FILEBASENAMEASIDENTIFIER______FILEHEADER____

Here is an example

//
// ___FILEHEADER____
//
import Foundation
import XCTest
@testable import TimeFountain
import Alamofire
extension TimeFountainTests {
func test___FILEBASENAMEASIDENTIFIER___() {

}
}

Place the template

Custom Templates Directory

~/Library/Developer/Xcode/Templates mkdir -p ~/Library/Developer/Xcode/Templates/File\ Templates/Custom Xcode 

Run

cd ~/Library/Developer/Xcode/Templates

If the command fails run

mkdir -p ~/Library/Developer/Xcode/Templates/File\ Templates/Custom Xcode

2. Go to Finder.

Click hotkeys: Command shift g

Paste in that path, then click GO.

You might see an empty version of this. If you don’t have the custom folder, add it. Then copy the following path and go to it using Finder.

/Applications/Xcode.app/Contents/Developer/Library/Xcode/Templates/File Templates
Open Source
Open Swift File.xctemplate
Copy the folder
Go back to the custom templates.
Paste it in the Custom folder.

Now you can change the Swift file.xc... folder to be named as you like. Let’s rename it to: ___FILEHEADER____Test.swift or whatever you like, and let's open the file to paste our template!

Now when we create a file, we can search for our custom file.

--

--