25 Eclipse Shortcut Keys for Code Editing
- Details
- Written by Nam Ha Minh
- Last Updated on 07 August 2019   |   Print Email
When using an IDE, you cannot be more productive without using its shortcut keys frequently as your habit. In this article, we summarize a list of shortcut keys which are useful for editing Java code in Eclipse IDE.
NOTE: Standard shortcuts are not covered, such as Ctrl + A (select all), Ctrl + Z (undo), etc.
- Ctrl + D: Deletes current line.
- Ctrl + Delete: Deletes next word after the cursor.
- Ctrl + Shift + Delete: Deletes from the cursor until end of line.
- Ctrl + Backspace: Deletes previous word before the cursor.
- Shift + Ctrl + y: Changes a selection to lowercase.
- Shift + Ctrl + x: Changes a selection to uppercase.
- Alt + Up Arrow: Moves up current line (or a selected code block) by one line:
- Alt + Down Arrow: Moves down current line (or a selected code block) by one line:
- Ctrl + Alt + Up Arrow: Copies and moves up current line (or a selected code block) by one line:
- Ctrl + Alt + Down Arrow: Copies and moves down current line (or a selected code block) by one line:
- Shift + Enter: Inserts a blank line after current line, regardless where the cursor is at the current line (very different from press Enter key alone):
- Ctrl + Shift + Enter: works similar to the Shift + Enter, but inserts a blank line just before the current line.
- Ctrl + Shift + O: Organizes import statements by removing unused imports and sorts the used ones alphabetically. This shortcut also adds missing imports.
- Ctrl + Shift + M: Adds a single import statement for the current error due to missing import. You need to place the cursor inside the error and press this shortcut:
- Ctrl + Shift + F: Formats a selected block of code or a whole source file. This shortcut is very useful when you want to format messy code to Java-standard code. Note that, if nothing is selected in the editor, Eclipse applies formatting for the whole file:
- Ctrl + I: Corrects indentation for current line or a selected code block. This is useful as it helps you avoid manually using Tab key to correct the indentation:
- Ctrl + /or Ctrl + 7: Toggle single line comment. This shortcut adds single-line comment to current line or a block of code. Press again to remove comment. For example:
- Ctrl + Shift + /: Adds block comment to a selection.
- Ctrl + Shift + \: Removes block comment.
- Alt + Shift + S: Shows context menu that lists possible actions for editing code:
From this context menu, you can press another letter (according to the underscore letters in the names) to access the desired functions. - Alt + Shift + S, R: Generates getters and setters for fields of a class. This is a very handy shortcut that helps us generate getter and setter methods quickly. The following dialog appears:
- Alt + Shift + S, O: Generates constructor using fields. This shortcut is very useful when you want to generate code for a constructor that takes class’ fields as its parameters. The following dialog appears:
- Alt + Shift + S, C: Generates Constructors from Superclass. A common example for using this shortcut is when creating a custom exception class. In this case, we need to write some constructors similar to the Exception superclass. This shortcut brings the Generate Constructors from Superclass dialog which allows us to choose the constructors to be implemented in the subclass:
- Alt + Shift + S, H: Generates hashCode() and equals() methods, typically for a JavaBean/POJO class. The class must have non-static fields. This shortcut brings the Generate hashCode() and equals() dialog as below:
Select the fields to be used in hashCode() and equals() method, and then click OK. We got the following result (example):
@Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + id; result = prime * result + ((password == null) ? 0 : password.hashCode()); result = prime * result + ((username == null) ? 0 : username.hashCode()); return result; } @Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; User other = (User) obj; if (id != other.id) return false; if (password == null) { if (other.password != null) return false; } else if (!password.equals(other.password)) return false; if (username == null) { if (other.username != null) return false; } else if (!username.equals(other.username)) return false; return true; }
- Alt + Shift + S, S: Generates toString() method. This shortcut comes in handy if we want to override toString() method that returns information of relevant fields of the class. This brings the Generate toString() dialogas below:
And here’s sample of a generated toString() method:
@Override public String toString() { return "User [id=" + id + ", username=" + username + ", password=" + password + "]"; }
Related Eclipse Shortcut Keys Tutorials:
Other Eclipse Tutorials:
- How to use Eclipse IDE for Java EE Developers
- How to create, build and run a Java Hello World program with Eclipse
- How to change font for Java code in Eclipse
- How to Add Copyright License Header for Java Source Files in Eclipse
- How to generate Javadoc in Eclipse
- How to create JAR file in Eclipse
- How to generate WAR file in Eclipse
- How to add custom Ant build script to Eclipse project
- How to pass arguments when running a Java program in Eclipse
- How to create Java web project with Maven in Eclipse
Comments