Move, copy and delete lines with a single keystroke

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.

Shortcut Action
Ctrl+Alt+Down Copy line(s)
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…

Related Tips

Here are some other tips to help with line editing:

Share This Tip

Support Eclipse On E

8 thoughts on “Move, copy and delete lines with a single keystroke

    • No problem. There’s also Ctrl+Alt+Up which copies the line and inserts it above the current line, but I prefer to use Ctrl+Alt+Down and then Alt+Up/Down if I have to.

  1. Thanks – been looking for these keystokes and gave up. One thing that I missed from NetBeans….

  2. Pingback: Tweets that mention Move, copy and delete lines with a single keystroke « Eclipse on E --

  3. Pingback: Java bookmark links, Dec 2009 – Week 1 | bookmark-link

  4. Is there a eclipse command to copy a line to the clipboard to past it elsewhere. I don;t always want to copy it to the line above or below. I can do the key bindings but I cant find an easy way to do the copy of a line in one command (key stroke)

    • Don, I haven’t seen a native command that does precisely what you want, but there are some that come close:

      1) You can cut a line to the clipboard with a single key then undo the change immediately with Ctrl+Z. The line will still be in the clipboard. Cut line isn’t mapped by default, so go to Window > Preferences > General > Keys. Search for Cut Line and map it to any key you want, eg. Ctrl+Alt+X. Now you can do Ctrl+Alt+X, Ctrl+Z.

      2) Check out this post I did on selecting code using Alt+Shift+Up. You’ll probably have to press it 3 times and then do a Ctrl+C.

      3) Use the default editing shortcuts: Press Home, Shift+End, Ctrl+C.

      I’d go with (1), but whatever you choose (if you’re using Windows) you can use the excellent AutoHotkey to send any of the multiple keys above by just pressing a single key (similar to running a macro). I can publish a script for you if you’re interested.

      Hope it helps.

  5. Pingback: Plantillas de Eclipse « Java Mania