Hidden features of Eclipse [closed]

Don't forget Ctrl+Shift+L, which displays a list of all the keyboard shortcut combinations (just in case you forget any of those listed here).


Ctrl-2 something

Seems that nobody mentioned Ctrl-2 L (assign to new local variable) and Ctrl-2 F (assign to a new field), these ones have changed how I write code.

Previously, I was typing, say (| is cursor location):

Display display = new |

and then I pushed Ctrl-Space to complete the constructor call. Now I type:

new Display()|

and press Ctrl-2 L, which results in:

Display display = new Display()|

This really speeds things up. (Ctrl-2 F does the same, but assigns to a new field rather than a new variable.)

Another good shortcut is Ctrl-2 R: rename in file. It is much faster than rename refactoring (Alt-Shift-R) when renaming things like local variables.

Actually I went to Keys customization preference page and assigned all sorts of additional quick fixes to Ctrl-2-something. For example I now press Ctrl-2 J to split/join variable declaration, Ctrl-2 C to extract an inner class into top-level, Ctrl-2 T to add throws declaration to the function, etc. There are tons of assignable quick fixes, go pick your favourite ones and assign them to Ctrl-2 shortcuts.

Templates

Another favourite of mine in my “npe” template, defined as:

if (${arg:localVar} == null)
    throw new ${exception:link(NullPointerException,IllegalArgumentException)}("${arg:localVar} is null");

This allows me to quickly add null argument checks at the start of every function (especially ones that merely save the argument into a field or add it into a collection, especially constructors), which is great for detecting bugs early.

See more useful templates at www.tarantsov.com/eclipse/templates/. I won't list them all here because there are many, and because I often add new ones.

Completion

A few code completion tricks:

  • camel case support mentioned in another answer: type cTM, get currentTimeMillis
  • default constructor: in the class declaration with no default constructor push Ctrl-Space, the first choice will be to create one
  • overloading: in the class declaration start typing name of a method you can overload, Ctrl-Space, pick one
  • getter/setter creation: type “get”, Ctrl-Space, choose a getter to create; same with “is” and “set”

Assign To A New Field

This is how I add fields.

  1. If you have no constructors yet, add one. (Ctrl-Space anywhere in a class declaration, pick the first proposal.)

  2. Add an argument (| is cursor position):

    public class MyClass {
        public MyClass(int something|) {
        }
    }
    
  3. Press Ctrl-1, choose “assign to a new field”. You get:

    public class MyClass {
        private final Object something;
        public MyClass(Object something) {
            this.something = something;
        }
    }
    
  4. Add a null-pointer check if appropriate (see “npe” template above):

    public class MyClass {
        private final Object something;
        public MyClass(Object something) {
            npe|
            this.something = something;
        }
    }
    

    Hit Ctrl-Space, get:

    public class MyClass {
        private final Object something;
        public MyClass(Object something) {
            if (something == null)
                throw new NullPointerException("something is null");
            this.something = something;
        }
    }
    

A great time saver!