Move lines / areas up and down in emacs

What is the easiest way to move a selected area or line (if there is no choice) up or down in emacs? I am looking for the same functionality as in eclipse (limited to M-up, M-down).

+74
emacs
Mar 11
source share
9 answers

The line can be moved using transposed lines linked to Cx Ct . However, about the regions.

I found this elisp snippet that does what you want, except that you need to change the bindings.

 (defun move-text-internal (arg) (cond ((and mark-active transient-mark-mode) (if (> (point) (mark)) (exchange-point-and-mark)) (let ((column (current-column)) (text (delete-and-extract-region (point) (mark)))) (forward-line arg) (move-to-column column t) (set-mark (point)) (insert text) (exchange-point-and-mark) (setq deactivate-mark nil))) (t (beginning-of-line) (when (or (> arg 0) (not (bobp))) (forward-line) (when (or (< arg 0) (not (eobp))) (transpose-lines arg)) (forward-line -1))))) (defun move-text-down (arg) "Move region (transient-mark-mode active) or current line arg lines down." (interactive "*p") (move-text-internal arg)) (defun move-text-up (arg) "Move region (transient-mark-mode active) or current line arg lines up." (interactive "*p") (move-text-internal (- arg))) (global-set-key [\M-\S-up] 'move-text-up) (global-set-key [\M-\S-down] 'move-text-down) 
+35
Mar 11 2018-10-11T00:
source share

Update: Install the move-text package from Marmalade or MELPA to get the following code.

Here is what I use that works in both areas and on separate lines:

 (defun move-text-internal (arg) (cond ((and mark-active transient-mark-mode) (if (> (point) (mark)) (exchange-point-and-mark)) (let ((column (current-column)) (text (delete-and-extract-region (point) (mark)))) (forward-line arg) (move-to-column column t) (set-mark (point)) (insert text) (exchange-point-and-mark) (setq deactivate-mark nil))) (t (let ((column (current-column))) (beginning-of-line) (when (or (> arg 0) (not (bobp))) (forward-line) (when (or (< arg 0) (not (eobp))) (transpose-lines arg) (when (and (eval-when-compile '(and (>= emacs-major-version 24) (>= emacs-minor-version 3))) (< arg 0)) (forward-line -1))) (forward-line -1)) (move-to-column column t))))) (defun move-text-down (arg) "Move region (transient-mark-mode active) or current line arg lines down." (interactive "*p") (move-text-internal arg)) (defun move-text-up (arg) "Move region (transient-mark-mode active) or current line arg lines up." (interactive "*p") (move-text-internal (- arg))) (global-set-key [MS-up] 'move-text-up) (global-set-key [MS-down] 'move-text-down) 
+43
Mar 11 '10 at 15:43
source share

You should try drag-stuff !

It works exactly like eclipse Alt + Up / Down for individual lines, as well as for selected lines of a region!

In addition to this, it allows you to move words using Alt + Left / Right
This is exactly what you are looking for! And it is even available from ELPA repositories !

Other solutions have never worked for me. Some of them were erroneous (transposing lines when changing their order, wtf?), And some of them moved a precisely selected area, leaving the unselected parts of the lines in their positions. But drag-stuff works exactly the same as in eclipse!

And even more! You can try to select a region and use Alt + Left / Right ! This transposes the selected area one character left or right. Awesome!

To enable globally just run this:

 (drag-stuff-global-mode) 
+21
Oct 15 '13 at 10:02
source share

I wrote a couple of interactive functions for moving lines up / down:

 ;; move line up (defun move-line-up () (interactive) (transpose-lines 1) (previous-line 2)) (global-set-key [(control shift up)] 'move-line-up) ;; move line down (defun move-line-down () (interactive) (next-line 1) (transpose-lines 1) (previous-line 1)) (global-set-key [(control shift down)] 'move-line-down) 

The key elements are the IntelliJ IDEA style, but you can use whatever you want. I should probably implement some features that work with regions too.

+8
Mar 11 '10 at 14:35
source share

Here is my snippet to move the current line or lines stretched by the active area. It takes into account the position of the cursor and the selected area. And he will not break the lines when the region does not start / end on the border. (He is inspired by eclipse, I found the eclipse method more convenient than the "transposed lines".)

 ;; move the line(s) spanned by the active region up/down (line transposing) ;; {{{ (defun move-lines (n) (let ((beg) (end) (keep)) (if mark-active (save-excursion (setq keep t) (setq beg (region-beginning) end (region-end)) (goto-char beg) (setq beg (line-beginning-position)) (goto-char end) (setq end (line-beginning-position 2))) (setq beg (line-beginning-position) end (line-beginning-position 2))) (let ((offset (if (and (mark t) (and (>= (mark t) beg) (< (mark t) end))) (- (point) (mark t)))) (rewind (- end (point)))) (goto-char (if (< n 0) beg end)) (forward-line n) (insert (delete-and-extract-region beg end)) (backward-char rewind) (if offset (set-mark (- (point) offset)))) (if keep (setq mark-active t deactivate-mark nil)))) (defun move-lines-up (n) "move the line(s) spanned by the active region up by N lines." (interactive "*p") (move-lines (- (or n 1)))) (defun move-lines-down (n) "move the line(s) spanned by the active region down by N lines." (interactive "*p") (move-lines (or n 1))) 
+4
Sep 20
source share

The emacs wiki has an entry just for this:

http://www.emacswiki.org/emacs/MoveLine

For moving areas:

http://www.emacswiki.org/emacs/MoveRegion

+3
Mar 18 '11 at 11:17
source share

There is no built-in. You can use transposition lines (Cx Ct), but you cannot reuse them. Take a look at the functions http://www.schuerig.de/michael/blog/index.php/2009/01/16/line-movement-for-emacs/ .

It is also easy to adapt this to areas.

+1
Mar 11 2018-10-11T00:
source share

The transpose-paragraph function can help you.

You can also take a look at the transpose section in the Emacs manual. Essentially:

 Ct Transpose two characters (transpose-chars). Mt Transpose two words (transpose-words). CMt Transpose two balanced expressions (transpose-sexps). Cx Ct Transpose two lines (transpose-lines). 
+1
Mar 11 '10 at 10:03
source share

For this, I use the smart-shift package (in Melpa). By default, it overloads CC <arrow> to move a row or region. It moves horizontally depending on the main mode (e.g. c-basic-offset or python-indent-offset). It also works with regions.

 ;; binds CC <arrows> (when (require 'smart-shift nil 'noerror) (global-smart-shift-mode 1)) 
0
Dec 10 '15 at 19:42
source share



All Articles