在emacs中上下移动行/区域

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

一条线可以使用绑定到Cx Ct 转置线移动。 不知道地区虽然。

我发现这个 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) 

更新:从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 [MS-up] 'move-text-up) (global-set-key [MS-down] 'move-text-down) 

你应该尝试drag-stuff

它的工作原理与单行的eclipse Alt + Up / Down ,以及选定的区域行一样!

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

其他解决scheme从未为我工作。 其中有些是越野车(改变顺序换位线,wtf?),其中一些正在移动精确选定的区域,留下未选定的部分线。 但是drag-stuff就像在eclipse中一样!

甚至更多! 你可以尝试select一个地区,并使用Alt + / ! 这会将所选区域左移一个字符。 惊人!

要全局启用它,只需运行以下命令:

 (drag-stuff-global-mode) 

我已经写了几个交互function来移动上/下行:

 ;; 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的风格,但你可以使用任何你想要的。 我也许应该实现一些在地区上运行的function。

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

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

在emacs维基上只有这样一个条目:

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

移动地区:

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

没有内置的 您可以使用转置线(Cx Ct),但不能反复使用。 查看http://www.schuerig.de/michael/blog/index.php/2009/01/16/line-movement-for-emacs/上的function。;

应该很容易适应这个地区。

transpose-paragraphfunction可以帮助你。

您可能还想看看Emacs手册中的转置部分。 主要有:

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

我使用智能换档软件包(在Melpa)。 默认情况下,它重新绑定CC <arrow>来移动一个线或区域。 它通过特定于主模式的量进行水平移动(例如,c-basic-offset或python-indent-offset)。 也在地区工作。

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