Here is my second “Call For Help” needing help for an Eclipse Tip/Trick. Obviously, being a truly rookie with Eclipse these are basic questions now, but I’m sure they’ll be more challenging questions down the road. I guess this category could be helpful to people like myself, that are trying to get productive with Eclipse.
How can I quickly get a pre-defined code block/line/snippet into a Java class in Eclipse? With Vim I could define a “bit” of code in my .vimrc file and link it to a string. When I type the string it would get replaced with the code from my .vimrc.
So for a simple System.out.println( “” ); statement I would simply type jso+Spacebar and the “jso” would be replaced with “System.out.println( “” );”. The best part was I could control where my cursor would be and if I would be in edit mode or not.
I know this can be done easily in Eclipse. The problem is me finding out how to do it? Finding out what is already defined for me and how to write my own custom code “snippets”.
I’ll report back when I’ve “cracked the code”…… That was bad, but it got a smirk out of me. Sorry.
It’s all in the templates.
Window -> Preferences -> Java -> Editor -> Templates.
Define a name ‘jso’ for context of ‘java’ and a pattern of …
System.out.println(“${cursor}”);
Now, when in a java editor, type jso and hit Ctrl-Space (which is your Content Assist), at that point the jso will be expanded.
Incidentally, there is a vi plugin for eclipse available too. http://www.satokar.com/viplugin/index.php
Man… I’m glad I don’t have to face Joakim on Jeopardy.
Yes, Joakim is obviously an Eclipse guru. Joakim, I think I just need to give you an userid and password here on erik.weibust.net and just let you write my post updates instead of putting your stuff in the comments. THANKS!
Pingback: Erik's... Hmm...
Your solution is *templates* and they are really easy to set up. see it in action (and graphics 😉 at http://firstclassthoughts.co.uk/java/eclipse_tip_templates_public_static_final.html