How to use the "sysout" snippet in Eclipse with selected text?
Asked Answered
P

6

25

I am aware of the Eclipse snippet "sysout" which is neatly replaced with System.out.println(); when tab-completed.

This is very useful but sometimes, I need to wrap some existing code in a System.out.println();

In Eclipse internals, the template is defined as being able to wrap a "selected word". But how can I use the snippet with selected text since typing "sysout" obviously removes the selected text.

Any idea?

Pest answered 19/6, 2009 at 21:7 Comment(0)
V
70

The sysout template acts upon entire Java statements.

  1. Highlight a statement in the editor.
  2. Hit CTRL-SPACE (or whatever you have set up for content assist.)
  3. Type sysout then hit enter. Note that when you're typing sysout it will temporarily overwrite your statement (but it will come back wrapped by System.out.println when you hit enter.)
Voltmeter answered 19/6, 2009 at 21:26 Comment(3)
Awesome trick, +1. Do you know if there is a way to modify the macro to have eclipse automatically enclose everything from the cursor to the EOL inside the quotes? I always seem to want that and instead eclipse does really nasty/seemingly random stuff with text after the insertion point.Mcdaniel
hi, I'm following this pattern (copying from java sysout template) in PHP in Eclipse PDT, but template keeps replacing selected word instead of wrapping it.. any ideas how to have it wrap what was selected?Belldas
As an answer to @BillK, I just achieved that by editing the sysout template (to get to there, use as @sadhasivam pointed out SHIFT+ALT+Z -> Configure templates -> sysout -> Edit) to System.out.println("${word_selection} " + ${word_selection}${});${cursor}Guv
O
3

Eclipse has "Surround Width" Option which can do this for you.

SHIFT + ALT + Z should get you that to see how that templates meta-data layout.

Overdevelop answered 19/6, 2009 at 21:35 Comment(2)
Surround with does not offer the option to surround with System.out.printlnPest
Sysout has surround width feature and its combined with macro completion too. as jimr commented CTRL+SPACE includes but surrounds with and insta complete. if you want specific sysout completion then u can create another template say sy to do this. for our logger statements i used to have custom template like lod --> log.debug kindOverdevelop
I
0

I don't think you can do it in one go, but what about cutting the selected text and then: tab+space, ctrl+v it's just one key combination more.

Instil answered 19/6, 2009 at 21:25 Comment(0)
A
0

if you use content assist (ctrl-space on Windows), at the end of the list will be the sysout option. you might want to augment the template with quotes around the word selection so you dont need to type them in.

Alban answered 19/6, 2009 at 21:27 Comment(0)
B
0

Preference>General>Keys. then search "content assist" or "content" in "type filter text". don't press Enter. choice "content assist". type Binding and set your own shortcuts. such as "control+space".

Brother answered 7/9, 2014 at 15:26 Comment(0)
T
0

The main part is into:

Java->Editor->Templates

This can accessed by:

  • Preferences window: into Java->Editor->Templates.
  • Surrounding a text and press:

    Windows: SHIFT + ALT + Z (as sadhasivam said)
    Mac: CMD + OPT + Z 
    

    to display the Preferences Window

Thedrick answered 6/4, 2018 at 13:28 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.