Storing code snippets in eclipse
Asked Answered
G

7

66

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?

Gilli answered 26/10, 2008 at 13:3 Comment(0)
C
76

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 snippets from both parts and place your cursor right in the middle.

Cringle answered 26/10, 2008 at 14:3 Comment(1)
I found a good video tutorial on how to make a snippet in Eclipse, its actually called a template there : youtube.com/watch?v=JqMxaETAMaA&feature=youtu.beJueta
P
24

Eclipse also offers something very similar to the templates feature described by VonC called (would you believe) snippets. Window > Show view > Snippets.

To add a new snippet category: Right click in the Snippets window and click Customize... Click New > New Category. Enter a category name if necessary (e.g. "Java"). Click Apply. With your chosen category selected, click New > New Item. Enter your snippet.

To use a snippet, put the cursor where you want to insert the snippet, then double click on a snippet in the Snippets window.

Pietism answered 12/6, 2011 at 11:31 Comment(3)
is there a keyboard shortcut to use the snippet?Lowgrade
Does your variable substitution work - on my end the 'Insert Variable Placeholder...' button is disabled.Galley
Ctrl-space is the shortcut.Chariness
A
7

I ran into the Snip2Code plugin recently. It did the job, and I can collect and search snippets in a quick way.

Armstead answered 7/11, 2012 at 21:31 Comment(2)
I installed Snip2code but not sure how to use it. How do I access it? I can't see it anywhere.Intima
try to signup at snip2code.com/Access/Signup. Then, inside Eclipse, you would see a menu item "Snip2Code" to add new snippets to your account or search themVagal
O
7

Well a picture worths a thousand words, what about this one?

enter image description here

Overlap answered 2/11, 2015 at 19:14 Comment(1)
In the Eclipse 2019-12 version there's a "Context" button when you edit the template. Select Java to enable the template while editing Java code.Paludal
F
2

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

Ferroconcrete answered 25/9, 2010 at 12:29 Comment(0)
N
1

I have used snippets in some IDEs, like Dreamweaver and Homesite, an old Coldfusion IDE. I also use a lot of snippets in MySQL Workbench - where i type a lot of SQL, very handy there.

I am now using Eclipse Java EE IDE for Web Developers Version Indigo Release and found the snippets panel in Window|Show View|Other...|General|Snippets. I was able to manipulate it and figure out how to add the code I wanted as snippets and how to use it efficiently.

Neff answered 16/9, 2011 at 14:49 Comment(0)
D
1

Use Eclipse Snipmatch (Part of Eclipse for Java Developers Package).

  • Works very well for Java code snippets but also works for any other language like HTML, ABABP, PHP etc.
  • You can convert any code fragment from your editor directly to a code template. Highlight the code you'd like to convert to a snippet, context menu "create snippet", complete the form and done.
  • snippets can be shared via Git repositories with your team members

Manual: https://www.eclipse.org/recommenders/manual/#snipmatch

Installation: https://marketplace.eclipse.org/content/snipmatch

Dissepiment answered 30/3, 2017 at 14:1 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.