AppCode 2017.3 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:
    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:
    lett variables 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!

Last modified: 27 March 2018

See Also