# Surround code fragments

IntelliJ IDEA provides standard templates for surrounding code fragments with various constructs based on the language of the source code. This includes `if...else` conditional statements,  `do...while` and `for` loops,  combinations of `try...catch...finally`, XML tags,  folding regions, and other constructs.

Procedure: Surround a code block with language constructs

1. Select a code fragment.

2. Press `Ctrl+Alt+T` (Windows), `⌘ ⌥ T` (macOS), `⌘ ⌥ T` (IntelliJ IDEA Classic (macOS)), `⌘ ⌥ T` (macOS System Shortcuts), `Ctrl+Alt+Shift+B` (XWin), `Ctrl+Alt+Shift+B` (GNOME), `Ctrl+Alt+Shift+B` (KDE), `Ctrl+Alt+T` (Emacs), `Ctrl+Alt+T` (Sublime Text), `⌘ ⌥ T` (Sublime Text (macOS)), `Ctrl+Alt+T` (NetBeans), `Ctrl+Alt+T` (Visual Studio), `Ctrl+Alt+T` (Visual Studio (macOS)), `Alt+Shift+Z` (Eclipse), `⌘ ⌥ Z` (Eclipse (macOS)) or go to `Code | Surround With` in the main menu.

Alternatively, on the [toolbar](working-with-source-code.html#floating_toolbar) that appears, click Surround.

3. Select the necessary surround statement from the list. When you hover over an option, you will see a preview of the change that will be applied if you select that statement.

![Preview for surround with options](https://resources.jetbrains.com/help/img/idea/2026.2/surround_with_preview.png)

You can edit code templates that are used in the surround statements.

In the Settings dialog (`Ctrl+Alt+S` (Windows), `⌘ Comma` (macOS), `⌘ Comma` (IntelliJ IDEA Classic (macOS)), `⌘ Comma` (macOS System Shortcuts), `Ctrl+Alt+S` (XWin), `Ctrl+Alt+S` (GNOME), `Ctrl+Alt+S` (KDE), `Ctrl+Alt+S` (Emacs), `Ctrl+Alt+S` (Sublime Text), `⌘ Comma` (Sublime Text (macOS)), `Ctrl+Alt+S` (NetBeans), `Ctrl+Alt+S` (Visual Studio), `⌘ Comma` (Visual Studio (macOS)), `Ctrl+Alt+S` (Eclipse), `⌘ Comma` (Eclipse (macOS))), go to `Editor | File and Code Templates`.

For example, you can configure final modifier, name or even a type of the exception for the `try...catch` conditional statement.

![Code template settings](https://resources.jetbrains.com/help/img/idea/2026.2/code_template.png)

> **Note:**
> If live templates contain the [$SELECTION$ variable](template-variables.html#pdtv), they can be used as surround statements.
>
>
>
> To create your own surround statement, [create a live template](creating-and-editing-live-templates.html)  and use the `$SELECTION$` variable in the template text.

## Example

In the following example, we select the `int number = scanner.nextInt();` Java statement and apply the try / catch surround statement to it.

Before:

```JAVA
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.print("Enter a number: ");
        int number = scanner.nextInt();
        System.out.println("You entered: " + number);
    }
}
```

After:

```JAVA
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.print("Enter a number: ");
        int number = 0;
        try {
            number = scanner.nextInt();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        System.out.println("You entered: " + number);
    }
}
```

This feature applies to the following languages (contexts):

| Context | Surround with |
| --- | --- |
| Java statements |      * `if`    * `if/else`    * `while`    * `do/while`    * `for`    * `try/catch`    * `try/finally`    * `try/catch/finally`    * `synchronized`    * `Runnable`    * `{}`    |
| Java expressions |      * `(expr)`    * `((Type)expr)`    * `!(expr instanceof Type)`    |
|  XML/HTML /JSP/JSPX   tags  |      * Tag    * CDATA section    * <% ... %>    * [Emmet](using-zen-coding-support.html)    |
| JavaScript statements |      * `(expr)`    * `!(expr)`    * `if`    * `if / else`    * `while`    * `do / while`    * `for`    * `try / catch`    * `try / finally`    * `try / catch / finally`    * `with`    * `function`    * `{ }`    * `function expression`    |
| PHP statements |      * `if`    * `if / else`    * `while`    * `do / while`    * `for`    * `foreach`    * `try / catch`    * `function`    |
|   Custom folding region comments  | Any fragment of code, where Surround With is applicable. |

