Here’s how to make a new custom code template in XCode 4. It’s surprisingly simple to customise your empty C++ files to remove the useless iostream include and the boilerplate junk at the top.
That’s what we’re aiming for, a separate entry in the list on the left and then a template to choose from.
Step 1 – Copy some files
We have XCode from the Appstore, it doesn’t live in /Developer any more so ignore anything that tells you to copy stuff out of /Developer/Xcode because they’re now wrong.
Go into /Applications and right click XCode and choose Show Package Contents. Then navigate to Contents/Developer/Library/Xcode/Templates/File Templates.
This is where all your templates live.
Do not edit these files, ignore anyone who tells you to, they are going to cause you pain and suffering in six months when you absent-mindedly update Xcode.
Instead we will make our own copy in our home directory; this is Unix and we can do clever things that save us time and effort later when how to do this has leaked from our brains.
So open a second Finder window and navigate to ~/Library/Developer/Xcode. If these folders don’t exist you will need to create them, but bear in mind that certain folders are hidden in Lion. Try using the ‘Go’ menu in Finder and typing in the path manually.
You’ll need a folder called Templates and inside that one called File Templates
In there copy the relevant folder from the equivalent in Xcode’s package. I copied the C and C++ folder. Once copied, rename this folder to something more meaningful, the name of this folder appears on the left of the ‘Choose a new template’ window. I called mine ‘NCOT C and C++’.
Start XCode and check you see the new template choice. If you don’t, you put the files in the wrong place.
Step 2 – Edit the template
This is the easy bit, just open the files you copied and make changes. If you do this on Lion you’ll need to ‘unlock’ the files first.
If you copy the C++ header template and the C++ code template into the same folder Xcode will generate both at the same time for you… just like it used to do in Xcode 3.
Oh, and I know this is more effort than simply hacking the contents of the master templates but at least this way if you delete Xcode and reinstall it your changes are preserved.