CLion 2023.2 Help

Live template variables

When you expand a live template abbreviation, its variables either appear as input fields where you can type values or are replaced with values. These may be default values that you can modify or values calculated using functions.

To declare variables within templates, use the following format: $VAR$.

In expressions, use variable names without opening and closing dollar characters $, for example, lowercaseAndDash(ComponentName).

Define each variable using an expression and provide a default value for cases when the expression fails to evaluate.

This expression may contain the following constructs:

  • String constants in double quotes

  • Names of other variables defined in a live template

  • Predefined functions with possible arguments

Configure template variables

  1. In the Settings dialog (Control+Alt+S), go to Editor | Live Templates.

  2. Select a template where you want to configure variables.

  3. Specify variables in the template text and click Edit Variables….

  4. In the Edit Template Variables dialog, you can do the following for each variable:

    • Change the variable name.

    • Define an expression using predefined functions.

    • Specify the default value for cases when the expression fails to evaluate. The default value should be enclosed in double quotation marks.

    • Specify whether you want to skip the variable when prompting the user for input if the expression evaluated successfully.

    Live template variables

Predefined template variables

Functions for live templates variables

CLion supports the following predefined live template variables that cannot be modified:

  • $END$ indicates the position of the caret when the code snippet is complete, and you can no longer press Tab to jump to the next variable.

  • $SELECTION$ is used in surround templates and denotes the code fragment to be wrapped. After the template expands, it wraps the selected text as specified in the template. For example, if you select EXAMPLE in your code and invoke the "$SELECTION$" template via the assigned abbreviation or by pressing Control+Alt+T and selecting the desired template from the list, CLion will wrap the selection in double quotes as follows: "EXAMPLE".

Functions used in live template variables

The following functions can be used to define live template variables:

Function

Description

autoTypeForRangedForLoop()

Provides the variants of type deduction in range-base for loops:

Auto type deduction for a loop interator

blockCommentEnd()

Returns the characters that indicate the end of a block comment in the current language context.

blockCommentStart()

Returns the characters that indicate the start of a block comment in the current language context.

camelCase(<String>)

Converts a string into camelCase.

For example, camelCase("my-text-file"), camelCase("my text file"), and camelCase("my_text_file") all return myTextFile.

capitalize(<String>)

Capitalizes the first letter of a string.

For example, capitalize("name") returns Name. Or you can combine it into capitalize(camelCase("my awesome class")) to get MyAwesomeClass.

capitalizeAndUnderscore(<String>)

Capitalizes all the letters of a string, and inserts an underscore between the parts.

For example, capitalizeAndUnderscore("FooBar"), capitalizeAndUnderscore("foo bar"), and capitalizeAndUnderscore("foo-bar") all return FOO_BAR.

className()

Returns the name of the current class (where you expand the template). Can be useful for constructor templates.

clipboard()

Returns the contents of the system clipboard.

collectionElementName()

Removes _list and plural ending (s).

commentEnd()

Returns the characters that indicate the end of a comment in the current language context. For languages with line comments, the return value is empty.

commentStart()

Returns the characters that indicate the start of a comment in the current language context. For languages with line comments, the return value is the start of a line comment, same as lineCommentStart().

complete()

Invokes code completion at the position of the variable.

completeSmart()

Invokes smart type completion at the position of the variable.

concat(<String>, ...)

Returns a concatenation of all the strings passed to the function as parameters.

For example, concat(date()," ",user()) returns the current system date and username separated with a space.

date([format])

Returns the current system date.

By default, without a parameter, it returns the date in the current system format. To use a different format, provide a parameter according to the SimpleDateFormat specification. For example, the date("Y-MM-d, E, H:m") returns the date formatted as 2020-02-27, Thu, 16:11.

dbColumns()

Returns a list of columns for a table or a view. The dbColumns() is used in context live templates (for example, ins). You can access context live templates by right-clicking an object and selecting SQL Scripts.

dbObjectName()

Returns a name of a table or a view. The dbObjectName() is used in context live templates (for example, top). You can access context live templates by right-clicking an object and selecting SQL Scripts.

decapitalize(<String>)

Replaces the first letter of a string with the corresponding lowercase letter.

For example, decapitalize("Name") returns name.

enum(<String>, ...)

Returns a list of strings suggested for completion when the template expands.

For example, enum("Foo","Bar","Baz") shows a list from which you can choose one of the specified strings.

escapeString(<String>)

Escapes special characters so that the result can be used in a Java string.

For example, it replaces the tab character with \t, the newline character with \n, escapes the backslash as \\, quotes as \", and so on.

executionContext()

Returns the name of the current method using the sel_getName(_cmd) function.

expressionFormatCode()

Returns the string format specifier for the object which is passed as a parameter of this function.

fastEnumeration()

Returns the names of collections and enumerations available in the current scope.

For an example, see the each live template in the Objective-C group. The function is available for Objective-C only.

fileName()

Returns the name of the current file with its extension.

fileNameWithoutExtension()

Returns the name of the current file without its extension.

filePath()

Returns the absolute path to the current file.

fileRelativePath()

Returns the current file path relative to the current project. To check what the relative path is for a given file, right-click it and select Copy Reference, or press Control+Alt+Shift+C.

firstWord(<String>)

Returns the first word of the string passed as the parameter.

For example, firstWord("one two three") returns one.

lineCommentStart()

Returns the characters that indicate the start of a line comment in the current language context.

lineNumber()

Returns the current line number.

lowercaseAndDash(<String>)

Converts a string into lower case and inserts n-dashes as separators. For example, lowercaseAndDash("MyExampleName") and lowercaseAndDash("my example name") both return my-example-name.

methodName()

Returns the name of the method in which the template expands.

objectWithFunctions(sFunctionName1, sFunctionName2,...)

Returns the list of visible objects that contain member functions, listed as parameters.

pyFunctionName()

Returns the name of the current Python function.

regularExpression(<String>, <Pattern>, <Replacement>)

Finds all occurrences of Pattern in a String and replaces them with Replacement. You can specify the pattern as a regular expression to find everything that matches it in the string.

For example, the regularExpression(NAME, "a", "b") expression will replace any letter "a" in the $NAME$ template variable with the letter "b". You can use regexp character classes in the pattern part; for example, regularExpression(NAME, "[a-z]", "b") will replace any letter in the variable. If you want patterns, such as \d or \w, to be considered as regexp character classes, you have to escape the slash so that your pattern looks like "\\d".

rightSideType()

Declares the left-side variable with a type of the right-side expression.

Some predefined templates in the iterations group use the rightSideType() function to have a single template for both raw types and generic types.

showParameterInfo()

Returns the parameter details when adding a parameter to a function or method.

snakeCase(<String>)

Converts a string into snake_case. For example, snakeCase("fooBar") and snakeCase("foo bar") both return foo_bar.

spaceSeparated(<String>)

Returns the specified string with spaces as separators. For example, spaceSeparated("fooBar") returns foo Bar and spaceSeparated("Foo_BAR") returns Foo BAR.

spacesToUnderscores(<String>)

Replaces spaces with underscores in the string passed as the parameter. For example, spacesToUnderscores("foo bar BAZ") returns foo_bar_BAZ.

substringBefore(<String>, <Delimeter>)

Returns the substring up to the specified delimiter. This is helpful for removing the extensions in test file names. For example, substringBefore(fileName(),".") returns component-test if used in a file named component-test.js.

suggestIndexName()

Returns a suggested name for the index variable in an iteration from most commonly used ones: i, j, k, and so on (the first one that is not used in the current scope).

Some of the predefined templates in the iterations group (fori, itar, ritar) use the suggestIndexName() function to suggest a suitable index variable name.

suggestVariableName()

Suggests the name for a variable based on the variable type and its initializer expression, according to your code style settings that refer to the variable naming rules.

For example, if it is a variable that holds an element within an iteration, CLion makes a guess on the most reasonable name taking into account the name of the iterated container. Some of the predefined templates in the iterations group (itar, itco, iten) use the suggestVariableName() function to suggest a suitable variable name for iterated elements.

time([format])

Returns the current system time.

By default, without a parameter, it returns the time in the current system format. To use a different format, provide a parameter according to the SimpleDateFormat specification. For example, the time("H:m z") returns the time formatted as 13:10 UTC.

underscoresToCamelCase(<String>)

Transforms a string with underscores (like snake_case) into camelCase. For example, underscoresToCamelCase(foo_bar_baz) and underscoresToCamelCase(FOO_BaR_baZ) both return fooBarBaz.

underscoresToSpaces(<String>)

Transforms underscores in a string to spaces. For example, underscoresToSpaces(foo_bar_baz) returns foo bar baz and underscoresToSpaces(FOO_BaR_baZ) returns FOO BaR baZ.

user()

Returns the name of the current user.

variableForIteration()

Returns a variable that is an instance of the object used as a range expression in a loop.

variableForRangeBasedIteration()

Returns a variables that is an instance of the object that contains begin and end functions.

variableOfType(<String>)

Returns all variables that may be assigned to the type passed as the parameter. For example, .

If you pass an empty string "" as the parameter, the function suggests all variables regardless of their types.

Example

As an example, let's create a live template for a simple C++ class, using variables and functions. The template will contain three variables:

  • $ClassName$: a name for a new class

  • $Food$: a list of three possible values: "meat", "grass", and "honey"

  • $AnimalName$: the class name starting with a lowercase letter so that it can be used in a sentence

Create a template with variables

  1. Press Control+Alt+S to open the IDE settings and then select Editor | Live Templates.

  2. Select the C/C++ group, click the Add button, and select Live Template.

  3. In the Abbreviation field, specify the characters that will be used to expand the template. For example, animal.

  4. In the Template text field, paste the following template:

    class $ClassName$: public Animal { public: string Food = "$Food$"; void printFood () { std::cout << "The $AnimalName$ eats " << Food << std::endl; } };
    Live template example
  5. Click Edit Variables… and, in the Edit Template Variables dialog, configure the variables:

    • $ClassName$ remains untouched meaning we expect users to type a value when they apply this template.

    • $Food$: in the Expression field, enter enum("meat","grass","honey"). This function allows selecting one of the predefined values when users apply this template.

    • $AnimalName$: in the Expression field, enter decapitalize(ClassName). This function takes the value of the ClassName variable and converts its first letter to the lower case. Select Skip if defined because we don't need to prompt a user for input.

    Edit template variables
  6. Click the link in the bottom left corner to define or change the language context where the template will be applicable:

    Live template context

Use the created template

  1. Start typing the name of a live template (animal in our example).

  2. Type a value of the variable (a class name in our example) and press Tab to jump to the next variable.

  3. Using the keyboard arrows, select one of the values for the food string.

Live template usage
Last modified: 25 September 2023