4

Eclipse has a useful hotkey to assign current line to a local variable - when I type for instance:

Math.random()

and press ALT + SHIFT + L (Extract local variable), I can quickly change the line to

double random = Math.random();

I would like to use the same trick for printing it to std out, so that the Math.random() is being changed to:

System.out.println(Math.random());

Currently the fastet way to to this is to type syso and use content assist to use a template, but that requires manual copy pasting. Anyone knows a better way to do this?

Ernest Friedman-Hill
  • 80,601
  • 10
  • 150
  • 186
dstronczak
  • 2,406
  • 4
  • 28
  • 41

2 Answers2

1

Two options come to my mind to achieve your goal, but both of them require the selection of statement first.

After you select statement, press CTRL+SPACE, then type syso and hit Enter. Selected statement will be placed inside System.out block:

System.out.println(statement);

Also you can prepare eclipse template (Window->Preference->Java->Editor->Content Assist->Templates), and give it some name:

System.out.println(${line_selection});${cursor}

After you select statement, press ALT+SHIFT+Z or select menu option Source->Surround With (also in context menu). Template you have created should be there so select it. Selected statement will be wrapped inside desired block of code.

lukk
  • 3,164
  • 1
  • 30
  • 36
0

As far as I am aware there is no shortcut available from the keys section of preferences. Is content assist really not fast enough for you in this case?

Chucky
  • 1,701
  • 7
  • 28
  • 62
  • 1
    Well, I'm just doing this out of curosity. There are some possible custom templates configurations - I wonder if the thing I would like to do is achievable at all. – dstronczak Jul 03 '13 at 07:16
  • Could this be what you're looking for? http://wiki.eclipse.org/FAQ_How_do_I_provide_a_keyboard_shortcut_for_my_action%3F – Chucky Jul 05 '13 at 10:31