Code Templates

Code Templates allow you to insert boilerplate code sections instead of entering the code manually.

CodeRush is shipped with a large library of code templates. To expand a template, type the template name and press Space. For instance, to create a simple class, type in "c" and press Space. This creates a new class and prompts you to name it (the constructor is named automatically). You can also expand templates with the Tab key by binding the "TemplateExpand" command to it using the Setup Wizard or Shortcuts... items in the CodeRush menu.

Templates_Main

Important

The default CodeRush configuration does not interfere Visual Studio features such as IntelliSense. This prevents the Space and Tab keys from expanding templates because this key confirms the IntelliSense suggestion by default. The default configuration allows you to expand a template only if you close the IntelliSense suggestions window by pressing Esc before the Space key. You can also force the templates to expand in the IDE | IntelliSense options page.

Code templates are context-aware which means a shortcut expands different templates depending on the context. For instance, the "mb" template declares a Boolean method inside a class or generates a call to MessageBox.Show() inside a method.

Code templates are combinable, for example, you can combine the "r" (for return) template with the "t" (for true) templates and use the "rt" template to insert the return true statement. The same principle is used to declare typed members and variables: you can add a type mnemonic to each member or variable template to make it expand to the correct type.

Adding a Template

You can create a template from code using the code editor's context menu:

  • Select the code to create a template from.

    Note

    You can use the Multi-Select feature to mark parts that are used as Text Fields when a template is created. You can place an empty text field using a MultiSelectAddPoint command (the shortcut is disabled by default). Dropped Markers are also stored in the generated template.

  • Right-click the selection and choose Selection to Template...

    Templ_from_Sel1

  • In the Create Template From Selection dialog, specify the template options and choose for which identifiers the links should be created.

    Templ_From_Sel3

  • Enter the template name that corresponds to its mnemonics. You should type the template name in your code to expand the template.

    Templ_from_Sel2

  • Select the template's category. Right-click a category to add a new one.
  • Use OK to confirm the template's creation. This opens the new template in the Template Editor.
  • Make the required changes to your new template, and click OK to close the Template Editor.
  • Test the created template by expanding it as described above.

Refer to the Template Configuration article for details on how to edit existing templates and define the new ones manually.

See Also