Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hidden features of Eclipse [closed]

Tags:

java

eclipse

ide

People also ask

How do I reopen a closed project in Eclipse?

To reopen a closed project, in the Package Explorer view, select the closed project and click on the Project menu and select Open Project. Once the project is open its content can be edited using the Eclipse user interface.

How do I open a hidden project in Eclipse?

Eclipse shows hidden files in the "Navigator" view. You can add that via Window->Show View->Navigator. With Eclipse 4.14 for Mac OS X: Window > Navigation > Show View Menu > Filters ans Customization, then after just uncheck .

How do I close a view in Eclipse?

Press Alt + - to open the system menu, and then press C to close the view.

How do I close a folder in Eclipse?

To close a project, select the project select then go to Project menu and select close project.


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!


ctrl-shift-r and its buddy, ctrl-shift-t, to open a resource or type, respectively. Resources includes all files in your open projects (including non-java files), and types includes java types either in your projects, or in a library included in the projects.


Crtl+1 is my favorite. The quick fixes for the red-squiggles.

It is also located in the Edit Menu -> Quick Fix.


Ctrl+Shift+O to organize imports, which will format them nicely, remove unneeded imports, and add missing imports.