JetBrains Rider
 
Get JetBrains Rider
Get your hands on the new features ahead of the release by joining the Early Access Program for Rider 2025.1! Learn more

Surround code fragments with templates

Last modified: 25 September 2024

Surround templates are used to quickly enclose an expression, a single statement, or an arbitrary block of code with if...else, try...catch or other code constructs. As the template expands, JetBrains Rider intelligently reformats the code, adjusts the selection, and/or repositions the caret at the end so that you can continue typing from a handy position.

Out of the box, JetBrains Rider provides lots of surround templates in all supported languages, but you can also create surround templates for your specific cases.

Consider the following code fragment:

We would like to enclose the statement in a using directive. JetBrains Rider conveniently offers the corresponding predefined template to streamline this operation. Pressing after selecting this code block opens a list with available templates:

JetBrains Rider: Surrounding statments with 'using'

To choose the using template, you can do one of the following:

  • Click it with the mouse.

  • Go down to the corresponding menu item using arrow keys and press Enter.

  • Start typing usi... when the popup is open.

  • Press a key corresponding to the template mnemonic digit, which is shown next to the template.

JetBrains Rider will insert the template code and deploy a Hot spot session in the editor so that you could provide values for the parameters of the template:

JetBrains Rider: Surrounding statments with 'using'

We supply the desired disposable resource at the first parameter and press Tab or Enter to complete the hot spot session. The caret and selection are moved to the positions specified by the template designer, so that you can continue typing inside the created using statement.