Last Updated: March 02, 2016
·
2.382K
· bkimminich

Surrounding marked text with HTML tag in IntelliJ IDEA

Problem

It can be very frustrating to manually surround an existing piece text, e.g. click me, with an HTML tag such as <a> in IntelliJ IDEA.

When you open a tag in front of the text you want to enclose, IDEA typically closes that tag right away, which is not what you want:

<a></a>click me

This leaves you to either Ctrl-C,Ctrl-V the closing tag to its intended location or delete it and manually add it after the text again.

Solution

Select the text click me in your editor and hit Ctrl-Alt-T which opens the Surround With Live Template selection, where Surround With <tag></tag> in HTML/JSP should be preselected. Hit Enter and you will get <>click me</> with the cursor inside the opening <>. As you type the tag name the closing tag will be added as well:

<a>click me</a>

https://www.jetbrains.com/idea/help/wrapping-a-tag-example-of-applying-surround-live-templates.html

2 Responses
Add your response

Very good tip, did not know this.

over 1 year ago ·

Yup! You can also define a Live Template as a surroundable (my word) and use it from the popup. I use it when I need to surround some HTML with a PHP block

over 1 year ago ·