Eclipse Surround with option

Eclipse IDE provides a one of finest tip for all Developer to make their coding easy and make their life easy.
In this post we will try to understand “surround with” option, in this option mainly we focus on customizing the template with respective code.

Direction towards surround with

When we right click on selected code we will get an option called surround with there we will find following options.

Try /catch Block
  1. do (do while statement)
  2. for (iterate over array )
  3. if (if statement)
  4. runnable (runnable)
  5. synchronized (synchronized block)
  6. try (try catch block)
  7. while (while loop with condition)

Configure templates .. // we will see in second part

I hope every one of us knows more details about these feature but we will try to see how it looks when we select each of them.

Try / catch Block.

Once we select try/catch option whole selected code will be under try catch block let see next image.

Similarly all other options will be implemented except last option configure templates.

Lets now focus towards configuring custom template.
We can select option without selecting any code but we simple shortcut select windows>> preference>>Java >> Editor >> Templates
Here is the following image

Now we will see one of the default template, I mostly use main as one of shortcut once type main and press  Ctrl + Space to show the template proposal please check below image.

So here are few examples for default template names

// main    Ctrl+Spaces gives public static void main (String args[]) {}
// sysout  Ctrl+Spaces gives System.out.println();
// syserr Ctrl+Spaces gives System.err.println()
//lazy Ctrl+Spaces gives if (args == null) { args = new type(arguments); } return args;

Now we will drive with some internal of adding templates.


  1. Press Windows >> Preference >>  Java >> Editor >> Templates
  2. It will open the Template wizard and now press New  for more check below image 
  3. Provide some template name shortcut name under Name
  4. Select any one of the context type they are
    1. Java
    2. Java statements
    3. Java type members
    4. Javadoc
    5. SWT
    6. SWT statements
    7. SWT type Members
  5. Provide description of the templates
  6. Now under Pattern we need to define our own template based on context lets see a very simple example.
  7. I create simple baseFunction with template name as bone provide javadoc also please check the images.

Once we type bone and Press Ctrl+Space then it will give complete template.

Hope this post will help every Developers to build their own templates.

No comments:

Post a Comment