ChatGPT解决这个技术问题 Extra ChatGPT

Move line/region up and down in emacs

What is the easiest way to move selected region or line (if there is no selection) up or down in emacs? I'm looking for the same functionality as is in eclipse (bounded to M-up, M-down).

I'm pretty sure the idiom is to just kill what you want to move, use the usual navigation functions (including isearch, etc.) to move the point where you want the code to be, and then yank. There is a stack of kills, remember, so you don't even have to find the location immediately; you can gather up other bits to yank later. Personally, as a heavy Emacs user, I can't imagine any situation where I'd rather manually move a single line up or down. It just wouldn't be useful.
@jrockway: thanks for your comment. I think everyone has his own way of doing things. I work a lot with eclipse and I'm very used to moving lines/regions. One of the great thing of having emacs very extensible is that this functionality can be easily added.
agreed. I frequently want to move lines up and down as you describe, whether or not I'm in an editor that supports it. Or, for that matter, whether or not I'm working on code. Word supports this (by paragraph) with Alt+Shift+Up and Down, and I map that in my editors whenever I can.
@Drew and jrockway : I think settle for less is hardly the emacs way. Moving lines is just quicker when you want to move them than kill/yankk is. And org-mode ofcourse has it implemented too for a reason: indeed there are many cases you want to move a line, moving a wrong placed statement in / out a block scope is one, alter statement order, alter document order (see org-mode), .. A lot of people use it in eclipse. What you don't use , you often don't miss, but the usefullness of moving lines is a fact for a whole lot of programmers
This is useful when editing git-rebase files, containing lists of commits that you may want to reorder.

s
sanityinc

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

Here's what I use, which works on both regions and individual 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 [M-S-up] 'move-text-up)
(global-set-key [M-S-down] 'move-text-down)

Ive added this to EmacsWiki for you, it's here: emacswiki.org/emacs/MoveText
Thanks! I think that code was already on there (emacswiki.org/emacs/basic-edit-toolkit.el) but this new page will be easier for people to find.
Ahh, cool I've not seen that before, looks like basic-edit-toolkit could use a cleanup.
@mcb Yes, any ELPA package can be installed via el-get. (Note that even the el-get author is moving towards package.el these days.) To select full lines, I would suggest writing a separate function which quickly selects either the current line, or extends the current region to include full lines. Then just invoke that function before the move-text functions.
@sanityinc Thank you for this. I just had a mini Emacs 'O' the first time I used it. Good stuff!
p
peterh

A line can be moved using transpose-lines bound to C-x C-t. I don't know about regions, though.

I found this elisp snippet that does what you want, except 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)

Thanks for the snippet. This is exactly what I was looking for!
There's a little problem with both this solution, and the almost identical one posted by @sanityinc: if I use the defined shortcut to move a region up and/or down, then immediately use the shortcut for undo (C-_ by default), the region simply disappears, and the message "Undo in region!" is displayed in the echo area. Another undo command yields the message "No further undo information for region". However, if I move the cursor, then command undo, the undo starts working again. A relatively minor irritation, but all other Emacs commands can be undone immediately without resorting to a trick.
This is exactly the right way to do it. Default transpose line in Emacs is NOT correct functionality since the cursor moves to next line after the transpose making it difficult to repeat it step by step.
A
Aleks-Daniel Jakimenko-A.

You should try drag-stuff !

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

In addition to that it allows you to move words with Alt+Left/Right
This is exactly what you're looking for! And it is even available from the ELPA repos!

Other solutions never worked for me. Some of them were buggy(transposing lines while changing their order, wtf?) and some of them were moving exactly selected region, leaving unselected parts of the lines on their positions. But drag-stuff works exactly like in eclipse!

And even more! You can try selecting a region and using Alt+Left/Right ! This will transpose selected region by one character to the left or right. Amazing!

To enable it globally simply run this:

(drag-stuff-global-mode)

It really needs to have left and right movement optional, as it's overriding the default emacs keymap.
@Slomojo perhaps! Why don't you suggest that on emacswiki? :)
I used to use this, I'm using smart-shift now. I like that it DWIM for horizontal movement.
Needs (drag-stuff-define-keys) in the init file before the keybindings start to work. This is explained in this github: github.com/rejeep/drag-stuff.el
B
Bozhidar Batsov

I have written 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 keybindings are IntelliJ IDEA style, but you can use anything you want. I should probably implement some functions that operate on regions as well.


J
Ji Han

Here is my snippet to move the current line or the lines spanned by the active region. It respects cursor position and highlighted region. And it won't break lines when the region doesn't begin/end at line border(s). (It is inspired by eclipse; I found the eclipse way more convenient than 'transpose-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)))

For me the the move-text package was broken. Your solution works perfectly on my system. Thanks for that!
M
Manuel

There is an entry in the emacs wiki just for this:

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

For moving regions:

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


F
Florian Thiel

There's no built-in. You can use transpose-lines (C-x C-t) but you cannot use it repeatedly. Look at the functions on http://www.schuerig.de/michael/blog/index.php/2009/01/16/line-movement-for-emacs/.

It should be easy to adapt that to regions, too.


Btw, if you do decide to use the linked snippet, you might want to change the let statement to (let ((col (current-column)) (line-move-visual nil)), otherwise you get funny behavior with wrapped lines.
R
Roberto Aloi

The transpose-paragraph function could help you.

You might also want to have a look to the transpose section in the Emacs manual. Essentially:

C-t
Transpose two characters (transpose-chars).
M-t
Transpose two words (transpose-words).
C-M-t
Transpose two balanced expressions (transpose-sexps).
C-x C-t
Transpose two lines (transpose-lines).

j
jpkotta

I use the smart-shift package (in Melpa) for this. By default it rebinds C-C <arrow> to move a line or region. It moves horizontally by a major-mode-specific amount (e.g. c-basic-offset or python-indent-offset). Works on regions also.

;; binds C-C <arrows>
(when (require 'smart-shift nil 'noerror)
  (global-smart-shift-mode 1))