在emacs中向上或向下移动选定区域或线条(如果没有选择)的最简单方法是什么?我正在寻找与日食相同的功能(仅限于M-up,M-down)。
在emacs中向上或向下移动选定区域或线条(如果没有选择)的最简单方法是什么?我正在寻找与日食相同的功能(仅限于M-up,M-down)。
Answers:
可以使用绑定到的转置线移动一条线C-x C-t
。邓诺虽然地区。
我发现这个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)
更新:move-text
从Marmalade或MELPA安装软件包以获取以下代码。
这是我使用的方法,可在区域和单独的行上使用:
(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)
你应该尝试drag-stuff
!
它与eclipse Alt+ Up/完全一样,适用Down于单行以及选定的区域线!
除此之外,它还允许您使用Alt+ Left/移动单词,Right
这正是您要寻找的!它甚至可以从ELPA仓库中获得!
其他解决方案对我没用。其中一些是越野车(在更改顺序时换行,wtf?),而另一些则在精确选择的区域内移动,从而使未选择的部分保留在其位置上。但drag-stuff
工作原理完全类似于日食!
甚至更多!您可以尝试选择一个地区并使用Alt+ Left/ Right!这会将所选区域的位置向左或向右移一个字符。惊人!
要全局启用它,只需运行以下命令:
(drag-stuff-global-mode)
(drag-stuff-define-keys)
键绑定开始起作用之前,需要在init文件中进行操作。在此github中对此进行了解释:github.com/rejeep/drag-stuff.el
我编写了一些交互式函数来上下移动行:
;; 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样式,但是您可以使用任何所需的东西。我可能还应该实现一些在区域上运行的功能。
这是我的代码段,用于移动当前行或活动区域所跨越的行。它尊重光标位置和突出显示的区域。当区域不在线条边界处开始/结束时,它不会中断线条。(它受到日食的启发;我发现日食比“转置线”更方便。)
;; 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)))
没有内置的。您可以使用转置线(Cx Ct),但不能重复使用。查看http://www.schuerig.de/michael/blog/index.php/2009/01/16/line-movement-for-emacs/上的功能。
也应该容易地使其适应区域。