Eclipse Auto Insert

by Jeff Langr

June 09, 2009

In Eclipse, I use Ctrl-1 and Ctrl-Space so much that the wear on those three keys is noticeably more than the others.

By default, adding a new (JUnit 4 style) test is a matter of typing “test,” then hitting ctrl-space to bring up the appropriate code template. Problem is, Eclipse brings up a number of choices, and hitting enter requires ensuring the right thing is selected (it’s not always in first position) and then pressing enter. I’ve lived with this so far and didn’t think much about it until last night, when I created over 50 tests in a 90-minute session, and really resented the overhead.

I edited the template and figured checking the “Auto Insert” box would solve the problem. No dice–Eclipse presents other choices when I type “test,” such as one that just inserts the lone @Test annotation. What if I rename the “test” code template to a unique name, like “tst?” No, I get a list with two things–my template, and an option to create a method stub whose name is “tst.” No auto insert! I tried a couple other things, such as introducing a special character so that Eclipse would think it couldn’t possibly create a method with that name. That didn’t work (I don’t recall specifically what happened).

I know auto insert works in Eclipse, because I can go into the body of a method, type “sysout” and then press Ctrl-space, and Eclipse auto-completes to “System.out.println()” immediately. It requires no selection on my part. This tells me that auto insert works only if there’s one and only one possible selection. Unfortunately, it looks like that will never be the case outside a method but within the class (i.e. where one can create a “Java type member”)–any template name could also represent the name for a new method stub.

Keystrokes, then, what about assigning keystrokes? Aha. In the General->Keys preference page, I filtered for “content assist.” The choice named simply Content Assist is the one triggered by Ctrl-Space, but another choice is Content Assist (type: Template Proposals), by default assigned no keyboard shortcut. I assigned Ctrl-\ to content assist for template proposals. Now I just type “test” Ctrl-\ and I have my test method stub.

I didn’t really want another key combination, mostly because I prefer to go by IDE defaults. My pairing sessions run me through a wide variety of development environments (at last count, within the past year or so, 10 different C++ editors and 3 different Java IDEs), so it’s easiest to not create dependencies on my own personal reworking of the key mappings.

Ah well, I suppose there are a few key mappings I’ll just have to remember to configure. This is the second one I will want universally for Eclipse; the other is Rerun JUnit Test.

My wish for Eclipse is improved pairing support. The simple idea: you make your key mappings available via a URI. The first time you pair with someone, you add this to Eclipse. From there, a universally defined keystroke allows you toggle through or select from the keymaps.


Igor June 15, 2009 at 05:54am

Type the template you would like to use and hit ctrl+space twice. Eclipse will bring the list with choices filtered down to templates. Auto insert works if the template you typed is the only choice possible. That may work if you delete the old style “test” template. However it still requires you to press ctrl+space twice.

Jeff Langr June 15, 2009 at 08:11am

Thanks Igor. I wasn’t aware of the ctrl-space double-press. You still have to hit enter! So, it’s ctrl-space-space-enter. I would have thought this wouldn’t be required since it’s configured as auto-insert. The double-key-press would be ok, but requiring a third sucks. I like my solution better except that it’s not default behavior.

igor 6/22/2009 at 09:00am

That’s why I used to live with it 😀

Mike Bria : 7/01/2009 at 10:32am

I don’t so much love it, but I’ve changed the the templates so that the one I want (JUnit 4 in my case) is


Works. 🙂


Jeff Langr July 1, 2009 at 08:10pm

Right, that was my first thought, and I’m just bugged by the need to look at an unnecessary popup.

Mike Bria July 2, 2009 at 06:32am

You’re right, bugs me too. Would love the “syso” behavior. I’ve been messing around with Eclipse plugins recently, maybe I’ll dig around a bit.

Cheers, MB

Share your comment

Jeff Langr

About the Author

Jeff Langr has been building software for 40 years and writing about it heavily for 20. You can find out more about Jeff, learn from the many helpful articles and books he's written, or read one of his 1000+ combined blog (including Agile in a Flash) and public posts.