08.14.07

semi-robobait: macros in Eclipse

Posted in Eclipse, Hacking, programmer productivity, robobait at 3:26 pm by ducky

Cool! Eclipse has keyboard macros! Eclipse calls them “Editor Templates”, and has a bunch of them predefined. For example, if you type

sysout Cntl-SPACE

it converts that into

System.out.println();

with the cursor in the middle of the parens. Similarly, if you type

for Cntl-SPACE

you’ll get a short menu of options; selecting the first one (“iterate over array”) magically converts into

for (int i = 0; i < array.length; i++) {

}

with the first “i” selected If you type in “fooIndex”, then all the “i”s in the line change to “fooIndex”. Pressing tab takes you to the “array”, where you can then type in the name of the array. Press return, and then you are sent to the middle of the curly braces.

Whoa!

My old housemate, Chris Beekhuis, once set up macros like this and said that it made his coding significantly faster. I’m very much looking forward to using Editor Templates, and am a little puzzled as to why their existence isn’t being shouted from the rooftops. (Or was it? Did I have my headphones in at the time?)

(To see all of the pre-defined macros — or to define your own — go to Window->Preferences->Java->Editor->Templates. Or press Control-3, type “template”, and Editor Templates will be one of the options.)

2 Comments

  1. Ak said,

    October 31, 2009 at 7:14 am

    o thx sysout Cntl-SPACE – pretty usefull thing, was looking exactly the same

  2. Magnus said,

    July 26, 2012 at 4:40 am

    No, Eclipse still doesn’t have macros. While what you explain above is nice, it just isn’t *macros*. Please have a look at Vim and Emacs to find out what Eclipse is missing.