在emacs中上下移动行/区域

时间:2010-03-11 09:31:03

标签: emacs

在emacs中向上或向下移动选定区域或行(如果没有选择)的最简单方法是什么?我正在寻找与eclipse相同的功能(限制为M-up,M-down)。

9 个答案:

答案 0 :(得分:47)

更新:从Marmalade或MELPA安装move-text软件包以获取以下代码。

以下是我使用的内容,适用于区域和个别行:

(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)

答案 1 :(得分:41)

可以使用绑定到C-x C-t的{​​{3}}移动一行。关于地区的Dunno虽然。

我找到了transpose-lines elisp片段,除了你需要更改绑定外,它可以做你想要的。

(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)

答案 2 :(得分:27)

您应该尝试drag-stuff

对于单行以及选定的区域行,它的工作原理与eclipse Alt + Up / Down 完全相同!

除此之外,它允许您使用 Alt + / 移动单词 这正是你要找的!它甚至可以从ELPA repos获得!

其他解决方案从未对我有用。它们中的一些是有缺陷的(在改变它们的顺序时转换线,wtf?)并且其中一些正在移动精确选择的区域,在它们的位置留下未选择的线部分。但drag-stuff与日食完全一样!

甚至更多!您可以尝试选择一个区域并使用 Alt + Left / Right !这会将所选区域向左或向右移调一个字符。惊人!

要全局启用它,只需运行:

(drag-stuff-global-mode)

答案 3 :(得分:8)

我已经编写了一些用于上下移动线的交互式功能:

;; 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)

键绑定是IntelliJ IDEA样式,但您可以使用任何您想要的。我应该实现一些在区域上运行的函数。

答案 4 :(得分:4)

emacs wiki中只有一个条目:

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

对于移动区域:

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

答案 5 :(得分:4)

以下是移动当前行或活动区域跨越的行的代码段。它尊重光标位置和突出显示的区域。当区域不在线边界处开始/结束时,它不会断开线。 (它受到日食的启发;我发现日食方式比'转置线'更方便。)

;; 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)))

答案 6 :(得分:1)

没有内置功能。您可以使用转置线(C-x C-t),但不能重复使用它。查看http://www.schuerig.de/michael/blog/index.php/2009/01/16/line-movement-for-emacs/上的函数。

也应该很容易适应地区。

答案 7 :(得分:1)

transpose-paragraph功能可以帮助您。

您可能还想查看Emacs手册中的转置部分。 基本上:

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).

答案 8 :(得分:0)

我使用智能移位包(在Melpa中)。默认情况下,重新绑定C-C <arrow>以移动一条线或区域。它以特定于主模式的量水平移动(例如,c-basic-offset或python-indent-offset)。也适用于地区。

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