drag-stuff
Check out the drag-stuff package (also available on Melpa).
You can then select a region and use drag-stuff-up/drag-stuff-down to move that region up/down.
Alternative behavior when dragging lines
By default, the drag-stuff commands will also drag the line the point is on (even if the point is on the very first column). If you want to select, let's say 2 whole lines by doing C-a C-SPC C-n C-n, the selection will look something like this
line 1
▯line 2
line 3
▮line 4
line 5
Note that here I intend to move just lines 2 and 3, not line 4. But drag-stuff will move that 3rd line as well by default.
That was my pet-peeve (and probably doesn't apply to anyone else) and so I requested the package dev for a solution. Here's a hack you can put in your emacs config after requiring drag-stuff if you do not want this default behavior. The hack will not move the current line IF the point is on column 0 (first column).
;; https://github.com/kaushalmodi/.emacs.d/blob/master/setup-files/setup-drag-stuff.el
;; https://github.com/rejeep/drag-stuff.el/issues/4
(defvar modi/drag-stuff--point-adjusted nil)
(defvar modi/drag-stuff--point-mark-exchanged nil)
(defun modi/drag-stuff--adj-pt-pre-drag ()
"If a region is selected AND the `point' is in the first column, move
back the point by one char so that it ends up on the previous line. If the
point is above the mark, exchange the point and mark temporarily."
(when (region-active-p)
(when (< (point) (mark)) ; selection is done starting from bottom to up
(exchange-point-and-mark)
(setq modi/drag-stuff--point-mark-exchanged t))
(if (zerop (current-column))
(progn
(backward-char 1)
(setq modi/drag-stuff--point-adjusted t))
;; If point did not end up being on the first column after the
;; point/mark exchange, revert that exchange.
(when modi/drag-stuff--point-mark-exchanged
(exchange-point-and-mark) ; restore the original point and mark loc
(setq modi/drag-stuff--point-mark-exchanged nil)))))
(defun modi/drag-stuff--rst-pt-post-drag ()
"Restore the `point' to where it was by forwarding it by one char after
the vertical drag is done."
(when modi/drag-stuff--point-adjusted
(forward-char 1)
(setq modi/drag-stuff--point-adjusted nil))
(when modi/drag-stuff--point-mark-exchanged
(exchange-point-and-mark) ; restore the original point and mark loc
(setq modi/drag-stuff--point-mark-exchanged nil)))
(add-hook 'drag-stuff-before-drag-hook #'modi/drag-stuff--adj-pt-pre-drag)
(add-hook 'drag-stuff-after-drag-hook #'modi/drag-stuff--rst-pt-post-drag)
Demonstration of how dragging lines works before and after the above hack
Before hack
line 1 line 1
▯line 2 line 5
line 3 --(M-x drag-stuff-down)--> ▯line 2 MOVED LINE
▮line 4 line 3 MOVED LINE
line 5 ▮line 4 MOVED LINE
After hack
line 1 line 1
▯line 2 line 4
line 3 --(M-x drag-stuff-down)--> ▯line 2 MOVED LINE
▮line 4 line 3 MOVED LINE
line 5 ▮line 5
Keybindings
To achieve the eclipse-like behavior just add appropriate key bindings:
(global-set-key (kbd "M-<up>") #'drag-stuff-up)
(global-set-key (kbd "M-<down>") #'drag-stuff-down)