Surround With…

Posted on by etienne

IntelliJ IDEA makes it very easy to surround a code block with if, while, for and other statements or to make a code block become part of such statments as try/catch or synchronized. Simply select the code block to surround (don’t forget to use Ctrl + W to increase the current selection) and then press Ctrl + Alt + T (or right-click the selection and select Surround with… from the menu). IntelliJ IDEA will show a list of options to choose from.

Once you have made your choice, IntelliJ IDEA will add the necessary code and put the caret to the position that allows you to complete the surrounding statement, if needed.

After that, you’re done. As you can see, adding such a surrounding if statement does not break the flow of coding at all and there is no need in any additional key stroke or mouse move.

Furthemore, IntelliJ IDEA not simply surrounds the code, but makes it as smart as possible. For example, if you choose to surround a code block with a try/catch statement, IntelliJ IDEA will add all necessary catch statements for all thrown exception types that it is able to determine within the selected code block.
Another useful scenario of using ‘Surround With…’ is when inspecting objects in the debugger’s Expression Evaluation window. Let’s say you want to evaluate a method call on the inspected object where the method is a part of the runtime type but not a part of the declared super type. IntelliJ IDEA helps you with casting the object to the runtime type: just press Ctrl + Alt + T in the expression box, and select Surround With ((RuntimeType) expr) from the list.

You can then invoke any method that is a part of the runtime type of the inspected object. Again, the described operation of casting to the runtime type does not break the flow of coding due to IntelliJ IDEA’s intelligent and unobtrusive assistance.
By the way, the Expression Evaluation view supports code completion, JavaDoc popup, etc. just like the normal Java editor.

Technorati tags: , ,
Comments below can no longer be edited.

4 Responses to Surround With…

  1. Esko Luontola says:

    July 25, 2006

    It’s also possible to create your own custom surround operations in Settings > Live Templates (invoked with “Surround with Live Template”, Ctrl+Alt+J). For example I’ve create the following template for quickly entering {@code} tags in javadocs (Context=Java comments):
    {@code $SELECTION$}

    And the Java 1.4 version:
    $SELECTION$

  2. Esko Luontola says:

    July 25, 2006

    (comment system ate my code tags – a bug?)

    And the Java 1.4 version:
    <code>$SELECTION$</code>

  3. anon says:

    December 31, 2019

    Ctrl + Alt + T is standard for “launch terminal” linux distros, so yeah.

  4. Luka says:

    February 22, 2020

    Hi,

    I googled a lot, and I couldn’t find an answer…

    Is there a way to immediately surround selected text with live template without the intermediary step of Ctrl+Alt+T?

    I’d like to assign a shortcut for surrounding a text with try/catch or if/else block
    with SINGLE keystroke.

    something like Ctrl+$ would result in applying try/catch live template around the code.

    Thank you!

Subscribe

Subscribe for updates