views:

1364

answers:

3

Is there an Eclipse command to surround the current selection with parentheses?

Creating a template is a decent workaround; it doesn't work with the "Surround With" functionality, because I want to parenthesize an expression, not an entire line, and that requires ${word_selection} rather than ${line_selection}.

Is there a way that I can bind a keyboard shortcut to this particular template? Ctrl-space Ctrl-space arrow arrow arrow isn't as slick as I'd hoped for.

A: 

Easy, Window->Prefs, then select Java->Editor->Templates

Create a new template with : (${line_selection}${cursor})

The "line_selection" means you have to select more than one line.

You can try creating another one with "word_selection", too.

Then, select text, right click, Surround With... and choose your new template.

Dustin
Surround with only works with line_selection, and line_selection doesn't fit my use case.
erickson
+9  A: 

Maybe not the correct answer, but at least a workaround:

  1. define a Java template with the name "parenthesis" (or "pa") with the following :

    (${word_selection})${cursor}

  2. once the word is selected, ctrl-space + p + use the arrow keys to select the template

I used this technique for boxing primary types in JDK 1.4.2 and it saves quite a lot of typing.

Vladimir
For me, it's the same except that for step 2 I type ctrl-space twice to bring up the template list. (ctrl-space + p doesn't work for me)
Matt Passell
I am using Eclipse for Python development, and this *definitely* doesn't work for that. Bringing up the template list doesn't seem to work consistently, and when it does come up, sometimes it puts "(...)" around the selection but sometimes it just replaces the whole selection with "()". Is there a way to do this *without* using templates?
Glyph
A: 

I find that this leaves the originally selected text with the template appended afterward. For example, if I select the word "parenthetic" and run the above template the editor will show:

parenthetic(parenthetic)

instead of the desired:

(parenthetic)

Anyone else come across this?