AppCode 2016.2 Help

Live Templates

Shortcuts and settings

Surround with Live Template ⌃⌥T
Insert Live Template ⌃J
Expand Live Template
Save as a Live Template Tools | Save as Live Template
Preferences Editor | Live Templates

Essentials

Live Templates contain predefined code fragments you can use to insert frequently-used or custom code constructs into source code quickly, efficiently, and accurately. There are numerous pre-defined templates for Objective-C, Swift, C++, JavaScript, CSS, HTML, XSL and other languages.

In fact, Live Templates are similar to Xcode snippets with one important exception: by using simple predefined variables and constructions, you can trigger various automated actions for template placeholders, such as type suggesting, copying the entered value, automatic copying of entered expression in the snippet, and others.

Template types

There are two types of live templates:

  • Parameterized templates, which contain plain text and/or variables that enable user input. You can invoke the full list of such templates depending on the code context via ⌃J, or by typing the template abbreviation and using Tab to make it do coding for you:
    ac_LvTutorial_parametrized_templates@2x
  • Surround With templates, which allow you to surround selected text with some expression (⌃⌥T):

Adding custom templates

Let’s check how can we create a custom sout template which will print values in the following form in Swift:

print (expression = \(expression))
.
  1. Go to Preferences | Editor | Live Templates. Here you’ll find a list of predefined Live Templates grouped by language. Select the Swift group and click the Add (+) button or press ⌃N:
    ac_LvTutorial_add_live_template@2x
  2. Enter an abbreviation (the short name by which we are going to invoke this template) and a description:
    ac_LvTutorial_add_abbr@2x
  3. Select the context where this template should be used:
    ac_LvTutorial_select_context@2x
  4. Let’s add the template text. For our template we need to specify the following:
    ac_LvvTutorial_template_text@2x
    Note that you can simply select the text in the editor, invoke Tools | Save as a Live Template, and quickly get a template stub with the selected text.
    Here $EXPR$ is a template variable. Template variables in live templates are what enables user input. After a template is expanded, variables appear in the editor as input fields. Variables are declared as $VARIABLE_NAME$, and here you can specify any VARIABLE_NAME you want. They are defined by expressions which can also accept default values.

    Two predefined live template variables are supported:

    • $END$ (position of the cursor after template is expanded and all values are entered).
    • $SELECTION$ (for surround templates this is the code fragment to be wrapped).

  5. You can edit each variable by selecting Edit variables:
    ac_LvTutorial_edit_variables@2x
  6. Finally, our template will insert the snippet, accept value for the expression, and duplicate it in the interpolated string. To duplicate the variable value into another input field, just specify its name omitting $ signs in the Expression field. After pressing , it will move the cursor to the end of line as specified by the $END$ variable:
    Note, that you can jump between template inputs in the same way as you do between method parameters, by using /⇧⇥.

Surround with templates

Let’s see how we can create a similar template for surrounding our code block with if/else:

  1. Here we need to add the following text and use the $SELECTION$ variable:
    ac_LvTutorial_surround_with@2x
  2. $IF_EXPR$ and other variables allow us to specify conditional statements. Let’s save the template and see it in action:

Live Templates scripting

For each variable specified in the template, you can use a number of built-in functions to script the created live templates. This is how we can use them:

  1. To apply the built-in function, select it in the Expression field - see example in alloc template for Objective-C where className() function allows to guess the class name needed based on variable type:
  2. You can also apply the built-in function to any variable value in your template. For example, we can force the camelCase based on type for Swift using the copy of type name with camelCase() function applied:
    Here we use modified let template:
    ac_LvTutorial_modified_lett@2x
    with the following variables:
    ac_LvTutorial_modified_lett@2x
  3. Finally, to specify the default value for any variable, just enter it in the Default value field with quotes:
    ac_LvTutorial_default_value@2x
That’s it. Make use of Live Templates and minimize routine coding!

See Also

Last modified: 13 September 2016