25 Eclipse Shortcuts for Code Editing

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.

  1. Ctrl + D: Deletes current line.
  2. Ctrl + Delete: Deletes next word after the cursor.
  3. Ctrl + Shift + Delete: Deletes from the cursor until end of line.
  4. Ctrl + Backspace: Deletes previous word before the cursor.
  5. Shift + Ctrl + y: Changes a selection to lowercase.
  6. Shift + Ctrl + x: Changes a selection to uppercase.
  7. Alt + Up Arrow: Moves up current line (or a selected code block) by one line:Move up a line
  8. Alt + Down Arrow: Moves down current line (or a selected code block) by one line:Move down a line
  9. Ctrl + Alt + Up Arrow: Copies and moves up current line (or a selected code block) by one line:Copy and move up a line
  10. Ctrl + Alt + Down Arrow: Copies and moves down current line (or a selected code block) by one line:Copy and move down a line
  11. 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):insert a blank line
  12. Ctrl + Shift + Enter: works similar to the Shift + Enter, but inserts a blank line just before the current line.
  13. Ctrl + Shift + O: Organizes import statements by removing unused imports and sorts the used ones alphabetically. This shortcut also adds missing imports.organize imports
  14. 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:add single import
  15. 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:

    format code

  16. 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:correct indentation
  17. 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:single line comment code
  18. Ctrl + Shift + /: Adds block comment to a selection.add block comment
  19. Ctrl + Shift + \: Removes block comment.
  20. Alt + Shift + S: Shows context menu that lists possible actions for editing code:context menu for code
    From this context menu, you can press another letter (according to the underscore letters in the names) to access the desired functions.

  21. 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:Generate getters and setters
  22. 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:generare constructor using fields
  23. 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 Exceptionsuperclass. This shortcut brings the Generate Constructors from Superclass dialog which allows us to choose the constructors to be implemented in the subclass:generate constructors from superclass
  24. 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:generate hashCode and equalsSelect the fields to be used in hashCode() and equals() method, and then click OK. We got the following result (example):
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    @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;
    }
  25. 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:Generate toString methodAnd here’s sample of a generated toString() method:
    1
    2
    3
    4
    5
    @Override
    public String toString() {
      return "User [id=" + id + ", username=" + username + ", password="
      + password + "]";
    }

Comments

comments

25 Eclipse Shortcuts for Code Editing

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.

  1. Ctrl + D: Deletes current line.
  2. Ctrl + Delete: Deletes next word after the cursor.
  3. Ctrl + Shift + Delete: Deletes from the cursor until end of line.
  4. Ctrl + Backspace: Deletes previous word before the cursor.
  5. Shift + Ctrl + y: Changes a selection to lowercase.
  6. Shift + Ctrl + x: Changes a selection to uppercase.
  7. Alt + Up Arrow: Moves up current line (or a selected code block) by one line:Move up a line
  8. Alt + Down Arrow: Moves down current line (or a selected code block) by one line:Move down a line
  9. Ctrl + Alt + Up Arrow: Copies and moves up current line (or a selected code block) by one line:Copy and move up a line
  10. Ctrl + Alt + Down Arrow: Copies and moves down current line (or a selected code block) by one line:Copy and move down a line
  11. 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):insert a blank line
  12. Ctrl + Shift + Enter: works similar to the Shift + Enter, but inserts a blank line just before the current line.
  13. Ctrl + Shift + O: Organizes import statements by removing unused imports and sorts the used ones alphabetically. This shortcut also adds missing imports.organize imports
  14. 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:add single import
  15. 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:

    format code

  16. 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:correct indentation
  17. 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:single line comment code
  18. Ctrl + Shift + /: Adds block comment to a selection.add block comment
  19. Ctrl + Shift + \: Removes block comment.
  20. Alt + Shift + S: Shows context menu that lists possible actions for editing code:context menu for code
    From this context menu, you can press another letter (according to the underscore letters in the names) to access the desired functions.

  21. 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:Generate getters and setters
  22. 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:generare constructor using fields
  23. 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 Exceptionsuperclass. This shortcut brings the Generate Constructors from Superclass dialog which allows us to choose the constructors to be implemented in the subclass:generate constructors from superclass
  24. 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:generate hashCode and equalsSelect the fields to be used in hashCode() and equals() method, and then click OK. We got the following result (example):
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    @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;
    }
  25. 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:Generate toString methodAnd here’s sample of a generated toString() method:
    1
    2
    3
    4
    5
    @Override
    public String toString() {
      return "User [id=" + id + ", username=" + username + ", password="
      + password + "]";
    }

Comments

comments