10个提高Eclipse开发效率的小技巧
1. Text File Encoding
The default setting for saving files in your Eclipse workspace is to use the file encoding of the underlying operating system. However, you usually want to create a platform independent application. To accomodate this, set the file encoding to UTF-8
. Using UTF-8 will prevent problems with special characters if you deploy to a system that runs a different OS, run your build on a UNIX machine or if somebody else works on the code using a different OS. You’ll find the settings in: Preferences > General > Workspace
or in your project specific settings.
2. Save automatically before build
One thing I noticed is that people (including me) are constantly hitting Ctrl+S
to save or even worse — grabbing the mouse to hit the Save action in the toolbar. This behavior seems to originate from experiences where an application was started from the workspace without saving and of course, changes were then lost. You can save some keystrokes and mouse clicks by enabling Save automatically before build. This is in: Preferences > General > Workspace
3. Type Filters (for PDE / RCP developers)
Type Filters remove suggestions for certain types (e.g. classes, interfaces) from the “Open Type” dialog, content assist and quick fix proposals. Especially as an RCP / SWT developer you most likely don’t want suggestions for Swing components like Button, Label, etc.
You’ll find this setting in: Preferences > Java > Appearance > Type Filters
4. Use “Step Filtering” for Debugging
With “Step Filters” you can filter out types that you do not wish to see, or step through while debugging. This helps you to stay focused on the actual code and does not expose you to the inner workings where you might not even have the source. Have a look at Holger’s Effective Mockito post about Step Filtering in conjunction with Mockito.
You’ll find this in: Preferences > Java > Debug > Step Filtering
5. Always launch the previously launched application
This option will ensure your last launched configuration is used. In the default setting, Eclipse tries to determine the most appropriate configuration for the currently selected resource — which can be quite annoying. Set this under: Preferences > Run/Debug > Launching
If you want to automatically stop running instances when you start a new one, have a look at Launch an OSGi app and automatically kill its running instance.
6. Store your Launch Configuration in a Project
You probably spend a good amount of time crafting your launch configuration. You can actually store it as a file in one of your projects so you can check it in. This way you can share it with colleagues and save them from reinventing the wheel.
You’ll find this Preference in: Menu: Run > Run Configurations... > Launch Configuration Tab: Common
Hint: You can access the Launch Configurations dialog by clicking the Launch/Debug action in the Toolbar while holding the ctrl
key or cmd
on a Mac.
7. Make a Launch Configuration always appear in the Menu
If you have multiple Launch Configurations, you can choose your favorite ones and stick them permanently to your Launch/Debug menu.
Go to: Menu: Run > Run Configurations... > Launch Configuration Tab: Common
8. Organize imports on save
Java developers really don’t need to worry about imports in Eclipse. The imports are automatically added to the class file. But, changing or removing code might make some imports obsolete. To fix that, you can Organize Imports by hitting shift+crtl+o
or shift+cmd+o
on a Mac and your imports will be cleaned up. Or you can enable Organize imports on save and Eclipse will take care of that automatically.
You’ll find this in: Preferences > Java > Editor > Save Actions
or in your project specific settings.
9. Format edited lines on save
The code formatter in Eclipse is a very useful thing. It ensures that the code looks consistent in your projects — no matter who wrote it — and can be easily read and understood by anyone else. But, formatting the whole file can seriously mess up a diff between two versions of the file. So ‘format only edited lines on save’ might be the right choice if you want the have your code formatted and still be able to see what was changed in your code later on.
You’ll find this in: Preferences > Java > Editor > Save Actions
or in your project specific settings.
You might also want to take a look at the Eclipse Checkstyle Plug-In. This will help you to enforce your coding guidelines.
10. Let Eclipse do the typing
A good IDE should support the developer in writing his code at nearly the speed of thought. And Eclipse comes quite close to this with Refactorings, Quick Fixes, Code Assist/Code completion and more. So don’t type too much yourself — you will, here and there, type something wrong, overlook something, scroll through code, which costs a lot of time. Force yourself to use shortcuts!
Here is an example. If you want to store a method parameter in a field, just set the cursor on the parameter name, hit the Quick Fix shortcut and select Assign parameter to new field. And poof, you’re done. If you need a getter, type get
somewhere in your class (outside of a method declaration) and use Content Assist to let Eclipse create the method for you. The same applies for creating setters, the main method, the constructor and more.
- Quick Fix:
ctrl+1
orcmd+1
on a Mac - Content Assist:
ctrl+space
(for Mac and PC) - Quick Access:
ctrl+3
orcmd+3
on a Mac - Open Type:
shift+ctrl+T
orshift+cmd+T
on a Mac
aka ctrl+awesome
™
Use the Mousefeed Plug-In, to learn keyboard shortcuts. It gives you a message showing the keyboard shortcut for the action you just performed with the mouse.