views:

4881

answers:

2

I'm a recent semi-convert to Eclipse after 20 years of using vi and gvim. One of the things I miss about gvim is that I could cut a bunch of different snippets of code into named buffers, and paste them at will when doing something like repeating a common idiom. For instance I'd have it so "ap would paste

DatabaseHandle handle = null;
try
{
  handle = DatabaseConnectionPool.newHandle();

and then "bp would paste

  handle.commit();
}
finally
{
  handle.rollback();
  DatabaseConnectionPool.returnHandle(handle);
}

And I could repeat both of them over and over in the course of a day. In an answer to another question, somebody mentioned that you could "manage code snippets" in Eclipse, but didn't mention how. So now I'm asking: how do you manage code snippets in Eclipse?

+10  A: 

You might want to store those two snippets into a code template, as explained in this tutorial

And do not forget about the possibility to quickly execute any kind of java code snippets in a scrapbook (not exactly what you want, but it can come in handy at times)

Newtopian adds (in the comments)

In fact templates become much more powerful by adding variables and tabstops within, so your example above would become dbHandle ctrl+space. It would copy snipets from both parts and place your cursor right in the middle.

VonC
That would have been my advice too, in fact templates become much more powerful by adding variables and tabstops within, so your example above would become dbHandle ctrl+space. It would copy snipets from both parts and place your cursor right in the middle.
Newtopian
+1  A: 

The question is old but the link of the answere is older ;)

Here is a nice tutorial: http://www.dansshorts.com/post/creating-snippets-in-eclipse

David Bruchmann