Surround with Quotes in IntelliJ IDEA

Dave Townsend
unbounded.io
Published in
2 min readOct 11, 2016

--

Original post date Aug 12th, 2012

There is a nice feature in IntelliJ IDEA called “Surround With” (activated with cmd+option+t) that will wrap the current section with something. There are a few default surround with commands already, but the one I wanted was the ability to wrap the selection in quotes (double and single). Here’s how you can quickly add a live template command to do just that.

Adding the new command in Live Templates

You access the surround with feature in the Live Templates section of settings. Go to settings panel and type in Live Templates to access all the Live Templates. Find the one called “surround” and branch it out to see the default ones.

Click on the “+” sign on the right to add a new surround with command. Give your new command an Abbreviation and Description I’ve chosen “q” for my abbreviation and “Surround with quotes” for my description. Add this: “$SELECTION$” in the section labeled “Template text:” and click on Define where it says “No applicable contexts yet: Define”. Choose the context of your choice or just select “everywhere” for all contexts.

Surrounding with Quotes

Now when you are coding you can quickly wrap text in quotes with a quick cmd+option+t and when the “surround with” dialog pops up just hit “q” (or whatever abbreviation you’ve chosen) and the selection will be wrapped with quotes.

Further On

You can follow this same pattern to create one for single quotes using ‘$SELECTION$’ or whatever else you may want to surround. Live Templates in general are a really powerful way to increase the speed of your workflow. You can create all kinds of key activated snippets to cut down on the keystrokes and more importantly keeping your hands off the mouse.

--

--