πŸ“œ ⬆️ ⬇️

Eclipse for Java Developers. Navigation and editing


Watching with my acquaintances who have recently started learning programming or switched to Eclipse from another IDE, I often notice that they almost don’t use auxiliary tools of the development environment. They either do not know about them at all, or do not understand what practical benefits they can bring.
This article is intended primarily for beginners in working with Eclipse. But in the process of preparing the article, I myself learned about several simple and convenient tools that I have never used in two years of active development.
This article focuses on navigation and editing.
Inside screenshots.

Since, the main use of these tools is manifested when using hot keys, I have compiled the following tables:

Navigation
Description
Ctrl + F
⌘ + F
Simple search / replace
Ctrl + H
Ctrl + H
Advanced Search / Replace
Ctrl + Shift + R
⌘ + ⇧ + R
Open resource
Ctrl + Shift + T
⌘ + ⇧ + T
Open logical structure (Class)
Ctrl + Shift + G
⌘ + ⇧ + G
Find occurrences of a variable, method, class
F4
F4
Open hierarchy menu
Ctrl + LeftClick
⌘ + LeftClick
Go to the implementation or definition of a class, function, variable
F3
F3
Go to definition
Ctrl + T
⌘ + T
Go to the implementation
Ctrl + L
⌘ + L
Go to the line with a certain number
Alt + (left right)
⌘ + ([])
Move to previous / next place in editor
Ctrl + Q
Ctrl + Q
Go to the last edit place
Ctrl + E
⌘ + E
Show the list of open files in the editor
Ctrl + F6 (Ctrl + Shift + F6)
⌘ + F6 (⌘ + ⇧ + F6)
Next / Previous Editor
Ctrl + O
⌘ + O
Show class fields and methods
Ctrl + T
⌘ + T
Show class hierarchy
F2
F2
Open javadoc
Ctrl + Shift + (up | down)
Ctrl + + (up | down)
Go to next / previous method
Editing
Description
Ctrl + Space
Ctrl + Space
Autocompletion
Ctrl + 1
⌘ +1
Possible actions
Ctrl + Shift + O
⌘ + ⇧ + O
Package Import Organization
Ctrl + Shift + F
⌘ + ⇧ + F
Auto Format Code
Shift + Alt + S
⇧ + βŒ₯ + S
Code generation menu
Shift + Alt + T
⇧ + βŒ₯ + T
Refactoring menu
Shift + Alt + R
⇧ + βŒ₯ + R
Rename variable, method, class
Shift + Alt + M
⌘ + βŒ₯ + M
Selecting part of the code in a separate method
Ctrl + Alt + (up | down)
⌘ + βŒ₯ + (up | down)
Duplicate string
Alt + (up | down)
βŒ₯ + (up | down)
Moving a string
Ctrl + / Ctrl + 7
⌘ + / ⌘ + 7
Single line comment
Ctrl + Shift + /
⌘ + ⇧ + /
Multiline comment
Ctrl + D
⌘ + D
Delete line or selection

Consider each tool in more detail:

Simple search / replace


Ctrl + F ⌘ + F
')


This tool is present in many text editors. Provides the ability to find the necessary text entries in the current document. And if necessary, quickly replaced by another text.
It is possible to search using regular expressions, with which you can create a complex search, but it is rarely used to search for a single class.
If you select text and apply this tool, the selected text will immediately appear in the search field.
Application:
- Quick transition to the desired part of the code;
- Replacing one word to another. If you need to write two functions that are identical in logical structure, but work with different collections, it is faster to write one function, copy and make a replacement.

Advanced Search / Replace


Ctrl + H Ctrl + H



This tool has the features that was discussed earlier.
Key Features:
- Search by all projects, not by one;
- both full-text search and typed is possible;
- with a large number of files full-text search takes a lot of time.
Application:
- Find the occurrences of an element or text in all projects.

Open resource


Ctrl + Shift + R ⌘ + ⇧ + R



Application:
Quickly open file with data or class.

Open logical structure (Class)


Ctrl + Shift + T ⌘ + ⇧ + T



You can enter only uppercase letters of the class. For example, to search for the AbstractDogFactory class, just enter the ADF.
You can also use *.

Application:
Quickly open a file with a class. Other files are ignored.

Find occurrences of a variable, method, class


Ctrl + Shift + G ⌘ + ⇧ + G



For the selected item is looking for its use in all projects.
Application:
Much faster than regular search. It is enough to select an element, apply the tool and on the screen a list of places where this element is used.

Open hierarchy menu


F4 F4



Go to the implementation or definition of a class, function, variable


Ctrl + LeftClick ⌘ + LeftClick

When the Ctrl key is held down, left-clicking with the mouse on the element will proceed to its definition or implementation.
Application:
- it is easy to find out where the variable was declared;
- quick transition to the implementation of a class or method.

Go to definition


F3 F3

Go to the definition of the element on which the cursor is.

Go to the implementation


Ctrl + T ⌘ + T

Transition to the implementation of the element on which the cursor is.

Go to the line with a certain number


Ctrl + L ⌘ + L

Go to the line with the specified number.
Application:
In the interaction programmers in order to specify the place in the code - called the file and string.

Move to previous / next place in editor


Alt + (left right) ⌘ + ([])

Returns to the previous / next place where the source code was viewed.
Application:
When deepening in the implementation of one of the functions or class, and return back.

Go to the last edit place


Ctrl + Q Ctrl + Q

Returns to the previous place where the source code was edited.
Application:
Quick return to work.

Show the list of open files in the editor


Ctrl + E ⌘ + E



Application:
Quick transition between files.

Next / Previous Editor


Ctrl + F6 (Ctrl + Shift + F6) ⌘ + F6 (⌘ + ⇧ + F6)



These shortcuts are not always convenient, so it makes sense to reassign them for example to Ctrl + Tab / Ctrl + Shift + Tab
For quick switching, use: Ctrl + Page Up / Page Down - Linux / Windows. Ctrl + Fn + Up / Down - OS X

Show class fields and methods


Ctrl + O ⌘ + O



When the hotkeys are pressed again, the methods from the parent classes are added to the list.
Application:
- quick assessment of the class structure;
- quick transition to the desired method.

Show class hierarchy


Ctrl + T ⌘ + T



When pressed again: expands the hierarchy in the opposite direction and shows the implemented interfaces.

Application:
Quick transition to parent classes.

Open javadoc


F2 F2



Go to next / previous method


Ctrl + Shift + (up | down) Ctrl + + (up | down)

Autocompletion


Ctrl + Space Ctrl + Space (conflicts with Spotlight)

The tool makes it possible not to write the full names of functions, classes and variables, but only a part of them, the programmer chooses a completion from the proposed ones.
However, many do not know some features of autocompletion in eclipse:
- if, when selecting auto-completion, press Ctrl + Enter instead of Enter, the word that stands to the right of the cursor will be replaced with auto-completed.
- autocompletion works for all classes, even for those for which import has not yet been made. If you autocomplete such a class, the import will be added automatically.
- autocompletion works for new variables:
if you wrote

Point2D 

and call autocompletion, you get

  Point2D point2d 

- in eclipse there are templates for quick writing:

  syso 

will get

  System.out.println(); 

Most popular templates:
- syso
- for
- while
- switch
- catch
You can view all the templates, as well as add your own ones in the settings in the Β«TemplatesΒ» tab.

Possible actions


Ctrl + 1 ⌘ + 1

In my opinion the most useful tool in the process of writing code.
Based on which line the cursor is on and whether there is text selection, eclipse suggests possible options for changes and additions.
Examples of the tool:
- creation of stubs for implementing methods of an interface or an abstract class

 public class Run implements Runnable { } 

actions on the line with the name of the class

 public class Run implements Runnable { @Override public void run() { // TODO Auto-generated method stub } } 


- creation of new methods
If the current part of the code requires a method, but it does not exist yet.

  boolean success = isSuccess(); 

method is created

 private boolean isSuccess() { // TODO Auto-generated method stub return false; } 


- variable declaration
If you need to use the result of the function

  isSuccess(); 

variable with the correct name is declared

  boolean success = isSuccess(); 


- selection of a constant
If inside the code there is a number or text that needs to be put into a constant, select it and apply the tool:

  String success = "success"; 


  private static final String SUCCESS = "success"; 


Package Import Organization


Ctrl + Shift + O ⌘ + ⇧ + O

Remove imports of unused packages and add missing ones.

Auto Format Code


Ctrl + Shift + F ⌘ + ⇧ + F

Bringing the code into a readable form:
- makes the correct tabs;
- adds / removes spaces;
- removes unnecessary indents;
- limits the length of the line - transfers the text to the next other line.
It works for the whole selected section of code, or for the whole class.
Application:
Sometimes it is faster to write a poorly formatted function and press hotkeys than to write at once beautifully.

Code generation menu


Shift + Alt + S ⇧ + βŒ₯ + S

Automatic generation of - - get and set methods;
- blanks redefined and implemented functions;
- hashcode and equals;
- toString;
- constructors.
Application:
Do not waste time on standard methods if their implementation is not specific.

Refactoring menu


Shift + Alt + T ⇧ + βŒ₯ + T



Rename variable, method, class


Shift + Alt + R ⇧ + βŒ₯ + R

Rename the selected method. Thanks to refactoring, the method will be renamed throughout the project.

Selecting part of the code in a separate method


Shift + Alt + M ⇧ + βŒ₯ + M



Selection of the selected part in the new method. All possible duplicates of this part of the code in this class will automatically be replaced with a call to the new method.

Duplicate string


Ctrl + Alt + (up | down) ⌘ + βŒ₯ + (up | down)

Application:
If the next line is similar to the current one - it is faster to copy the current one and change it than to write again.

Moving a string


Alt + (up | down) βŒ₯ + (up | down)

Application:
Fast transfer of part of the code, without having to copy and paste.

Single line comment


Ctrl + / Ctrl + 7 ⌘ + / ⌘ + 7

Multiline comment


Ctrl + Shift + / ⌘ + ⇧ + / (conflicts with system hotkeys to call Help)

When used on commented code - comments are removed.

Delete line or selection


Ctrl + D ⌘ + D

UPDATE: Corrected and added to the article.
Many thanks to arturphoenix , AgentSIB , mishadoff , mdcool , Lihonosov , Aivean , anoshenko , lany , abusalimov , bimeg , netslow , majus for additions and comments.
Some tools are indirectly related to navigation and editing, so I did not include them in the article, but I will try to include them in the following.

Source: https://habr.com/ru/post/168223/


All Articles