Live templates are one of the most powerful coding assistance features in AppCode. You can create a template for any code chunk and quickly add it to your code whenever you need it. In this blog post, we will show you how to use predefined live templates, and also how to create new ones and share them with your team members.
Predefined live templates
AppCode provides sets of live templates for Swift, Objective-C, and other supported languages out of the box. Go to Preferences | Editor | Live Templates to see the lists of predefined templates. For example, this is what we have for Swift:
Expand live templates
Each template has an abbreviation. Type this abbreviation and press
⇥ to insert the template:
The good thing is that you don’t have to remember all these abbreviations to use the templates. Simply press
⌘J to see all the templates available in the current context and select the one you want to use:
⇥ also allows you to jump between the placeholders within a template.
Create live templates
If you find yourself needing more templates than the list has to offer, you can easily create your own. Use one of these three ways:
In Preferences | Editor | Live Template, click + in the top right-hand corner and select Live Template. Your template will be created in the currently selected group:
Alternatively, you can create a new group and add your templates there: click +, select Template Group, and create a new group.
- In the editor, select the code chunk you want to add to a live template and then select Tools | Save as Live Template.
- Duplicate an existing template by clicking , modify it as needed, and save it with a different name.
Live templates come in three varieties: simple, parametrized, and surround. Let’s see how we can create each type of template.
All of our templates will be applicable for the SwiftUI framework, so we will save them in a separate group called SwiftUI (click + and select Template Group to create it):
Create a simple template
Simple templates contain fixed plain text. When you expand a simple template, the text is automatically inserted into your source code, replacing the abbreviation.
Let’s create a template that will add boilerplate for the SwiftUI
In Preferences | Editor | Live Templates, select the SwiftUI group that we have created, click +, and choose Live Template. Enter the template abbreviation and description in the corresponding fields, and then type the code block that you want this template to expand:
$END$ is a predefined variable that you can use to mark where the caret will appear when you expand the template.
Specify the context for the new template: click the Define link below and select Swift:
In the Options section, you can change the key that expands the template (by default, it is
⇥). You can also check the Reformat according to style option if you want the current formatting settings to be applied to the added code block. When you’re done, click OK to save the new template.
Now you can use the new template in the editor by typing its abbreviation (body) and pressing
⇥. Alternatively, you can use it by pressing
⌘J and selecting the template from the list:
Create a parameterized template
Parameterized templates contain variables that enable user input. When you expand a parameterized template, these variables are displayed either as placeholders for user input or are filled in automatically by means of special functions.
We will create a parameterized live template for the preview classes.
If you already have this code in the editor, simply select it and choose Tools | Save As Live Template from the main menu:
In the dialog that opens, we can modify this template before saving it:
First, let’s add the template abbreviation and description, for example, prev and The preview class.
Next, replace the code that can be changed with variables. In our case, all usages of
ConferenceList can be replaced with
As soon as we add a variable to the template text, the Edit variables button becomes available:
Click this button to go to the variable settings.
For each variable, you can specify a predefined function to calculate its value. For the
$CLASS$ variable we can choose the
fileNameWithoutExtension() function, which returns the name of the current file with no extension:
Finally, let’s move our new template from the default user group to SwiftUI using drag and drop, and then remove the user group:
It’s time to see the new template in action. In the editor, type
prev and press
The template variables are replaced with the name of the current file.
Surround templates allow you to wrap a block of code with any text you want. You can create a surround template by adding the predefined
$SELECTION$ variable in your template text. This variable denotes the code to be wrapped.
Take a look at the following example:
Here we have the
ForEach loop that you may want to reuse as a surround live template. Choose Tools | Save as Live Template from the main menu to create a live template out of the selected code block.
As usual, you’ll need to type an abbreviation and description in the corresponding fields. After that, replace
conference with the
$ITEM$ variables, respectively:
$COLLECTION$ variable we can use the
collectSequences() function. This function returns the name of the collection declared before the place where the template is expanded. Click Edit variables and select
collectSequences() in the Expression field of the
$COLLECTION$ variable. Also, add a default value for both variables:
Finally, set the template context. Click the Change link below and check all the options under Swift to make sure the template is available everywhere:
To expand the surround template, select the code you want to wrap, press
⌥⌘T (Code | Surround With), and choose your template from the list or just start typing the template abbreviation.
Share your live templates
Once you’ve created some custom templates, you may want to share them with your teammates. To do this, select File | Manage IDE Settings | Export Settings from the main menu. In the dialog that opens, uncheck everything (Select None), select Live templates, and specify the folder where you want to save settings.zip:
To import the templates, go to File | Manage IDE Settings | Import Settings and select the saved settings.zip file. Then restart the IDE to apply the imported settings.
You can significantly upgrade the IDE by adding new templates or modifying existing ones for any of the languages or frameworks you are working with. Add the code chucks you frequently use to live templates, adjust them using various predefined functions, and share them with your team. This will certainly reduce the time you spend on typing and allow you to focus on more important things.
Your AppCode team
Subscribe to Blog updates
AppCode 2023.1 Bug-Fix Update
Hi, AppCode 2023.1 has just been released with fixes for Xcode 14.3 compatibility, Swift refactorings and intentions, the IDE’s UI, and Kotlin Multiplatform Mobile. On December 14, 2022, along with the AppCode 2022.3 release, we sunsetted the product and stopped sales. While we’ve refocused the te…
AppCode 2023.1 Release Candidate
Hi, On December 14, 2022, along with the AppCode 2022.3 release, we sunsetted the product and stopped sales. While we’ve re-focused the team's efforts in other directions, we’ve kept providing technical support to our current users and are releasing bug-fix updates in 2023, targeting mostly the Xco…
AppCode 2022.3 Release and End of Sales and Support
AppCode 2022.3 has just been released. With the release of v2022.3, we are sunsetting the product.
AppCode 2022.3 Release Candidate Is Here
Hi everyone, The release of AppCode 2022.3 is approaching, and the release candidate (build 223.7571.233) is now available for download on our website. You need to have an active subscription or activate a free trial to use this build. The main highlights of this build include: We fixed the r…