fastcodejava
fastcodejava

Reputation: 41097

Auto complete a statement in eclipse

Is there a way to auto complete a statement in eclipse? If I type

if (condition) 

it should, after pressing some key, become

if (condition) {
    // putting cursor here
}

Upvotes: 1

Views: 5505

Answers (5)

marcosbeirigo
marcosbeirigo

Reputation: 11328

Ctrl + Space is used to auto complete words, variable names, classes etc, if you want eclipse to complete your if statement, do the following:
- write your if condition along with the first bracket

if(condition){

- hit enter, then eclipse will do its job.

Upvotes: 1

Thilo
Thilo

Reputation: 262504

You can just continue to type the { and enter, and the closing bracket will be completed automatically without any special command:

if (  condition) { /* cursor here , enter */


if (  condition) { 
  /* cursor here */
}

There is also autocompletion (Crtl-Space), which can do the following

if /* cursor here , Ctrl-Space */

becomes

if ( /* cursor here */ condition) {

}

or

if ( /* cursor here */ condition) {

} else {

}

Upvotes: 5

Padmarag
Padmarag

Reputation: 7214

Ctrl + Space is default for code assist.
For more customization you need templates.

Upvotes: 2

user8681
user8681

Reputation:

This kind of thing cannot be autocompleted the conventional way, since you're not adding to a class or variable name. However, this should be possible using eclipse templates (Window->Preferences->Java->Editor->Templates)

if (${condition:var(boolean)}) {
 ${cursor}
}

You can then access the template by typing its name (most likely just 'if') and pressing Ctrl-space

Upvotes: 3

Andy
Andy

Reputation: 17771

Try Ctrl-Space

Upvotes: 1

Related Questions