Text Variables

Snippets may contain text variables (sometimes referred to as placeholders). A text variable is a special iden­tifier that is replaced with a particular value when the snippet is inserted. For example, $date$ is a text vari­able that gets replaced with the current date when you insert a snippet containing that text variable.

A text variable starts and ends with a dollar sign ($). For example $this$. When you select anywhere within it, the whole thing gets selected. Arrow keys move over text variables in one press. To select inside one, hold down Ctrl and click with the mouse in it.

You can also force Source Insight to process text variables in any selected text by using Expand Text Vari­ables. See: Expand Text Variables.

Text variables can also be used in Clips (see Clips).

Predefined Text Variables

There are many predefined text variables that are automatically replaced with values when a snippet is inserted.

Table 3.5: Predefined Text Variables

Name

Value

$current_function$

name of the current enclosing function

$current_namespace$

name of the current enclosing namespace

$current_package$

name of the current enclosing package

$current_symbol$

name of symbol declared or enclosing the cursor position

$current_type$

name of the current enclosing struct, class or data type

$current_word$

the current word at the cursor position before the snippet is inserted

$date$

the current local date

$dollar$

the dollar sign ($)

$enclosing_symbol$

name of the function, class, or symbol that encloses the cursor

$file$

current file name with path

$file_in_project$

current file name relative to project source directory (root)

$file_no_path$

current file name without path

$next_function$

name of the function declared after the current line

$next_namespace$

name of the namespace declared after the current line

$next_package$

name of package declared after the current line

$next_symbol$

name of the symbol declared after the current line

$next_type$

name of the type declared after the current line

$prev_function$

name of the function declared before the current line

$prev_namespace$

name of the namespace declared before the current line

$prev_package$

name of the package declared before the current line

$prev_symbol$

name of the symbol declared before the current line

$prev_type$

name of the type declared before the current line

$return_type$

return type of the current function

$selection$

the currently selected text before the snippet is inserted

$time$

the current local time

$username$

the system user name that is logged in

$UTC_date$

the current UTC date

$UTC_time$

the current UTC time

$UTC_year$

the current UTC year

$year$

the current local year

Placeholder Text Variables

You can also use your own text variables to represent placeholders that behave similar to form fields after the snippet is inserted.

For example, this snippet insert a "for" statement and has placeholders for the iterator variable, the limit value, and the final cursor position.

for ($i$ = 0; $i$ < $lim$; ++$i$)

   {

   $end$

   }

When this snippet is inserted, the first text variable is automatically selected. In this example, that would be the $i$ variable. You can simply type the name of the variable to replace $i$.

Then, you can use the Select Next Snippet Placeholder command (Ctrl+Shift+;) to select the next place­holder. Alternatively, you can use any other navigation command to move the cursor.

Automatic Text Variable Replacements

When you edit a placeholder, all instances of the same text variable placeholder get replaced automatically with what you typed. For example, if $myvar$ appears in 4 places, and you select it and type over it, then all 4 places are replaced with what you typed. This is useful for snippets that refer to the same identifier in multi­ple places.

In the above example, let's say you type ioffset to replace the value of $i$. The other instances of $i$ will get replaced with ioffset.

To select the next text variable in a file, use the Select Next Snippet Placeholder command (CTRL+SHIFT+;). This selects the next text variable placeholder in the file. If you have the "Smart Tab" feature enabled, then you can just type Tab in many cases to move to the next placeholder.

In the above example, the $lim$ placeholder will get selected. Now you can type the limit value of this for loop.

Pressing Ctrl+Shift+; again will select the $end$ placeholder. Now you can edit the body of the for loop.