When coding new features or making big changes to existing functions, I end up moving around a lot of code by typing something, deleting it, copying another line to modify it slightly, moving another line up or down, etc. Deleting a line is also more common than you think.
Instead of selecting lines and using standard copy, paste and delete to copy, move or delete lines, I’d rather use only single keystrokes. Why? Because selecting a line is slow since you have to move the beginning to fully select it. Then you have to copy, move to the new location and paste. Also, it impresses any colleagues watching me code.
Shortcuts to move, copy and delete lines
Eclipse has single keystroke commands to do this type of editing. BTW, this works in most editors (Text, XML, HTML, etc), not just Java editors.
|Alt+Down||Move line(s) down|
|Alt+Up||Move line(s) up|
|Ctrl+D||Delete a line|
Tip: You can also selected a range of lines and copy, move or delete them. Be careful of moving blocks of lines from one method into another or across large distances – Eclipse tends to mess up the formatting and get confused. For this scenario I use the normal cut and paste (Ctrl+X, Ctrl+V).
Not convinced? Watch the following video to get an idea of how smoothly it works. Just makes me want to edit some code…
Here are some other tips to help with line editing:
- Select entire strings and methods in Eclipse with a single keystroke
- Add comments and Javadocs in Eclipse with a single keystroke
- The easiest ways to navigate methods in a class using Eclipse keyboard shortcuts
- Navigate and fix errors and warnings in a class with Eclipse keyboard shortcuts
- How to manage keyboard shortcuts in Eclipse and why you should