Last active
January 3, 2016 12:29
-
-
Save elfenlaid/8463366 to your computer and use it in GitHub Desktop.
Move region up\down
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;;; Move region | |
(defun selected-text () | |
(let ((text (if (region-active-p) | |
(vector (buffer-substring-no-properties (region-beginning) (region-end)) | |
(region-beginning) (region-end)) | |
nil))) | |
(if (null text) (unit-at-cursor 'line) text))) | |
(defun move-line (start end n) | |
(let ((text-meta (selected-text))) | |
(delete-and-extract-region (elt text-meta 1) (elt text-meta 2)) | |
(delete-char 1) | |
(forward-line n) | |
(insert (elt text-meta 0)) | |
(insert "\n") | |
(forward-line -1))) | |
(defun move-line-up (start end n) | |
"Move current region or line up by N lines" | |
(interactive "r\np") | |
(move-line start end (if (null n) -1 (- n)))) | |
(defun move-line-down (start end n) | |
"Move current region or line down by N lines" | |
(interactive "r\np") | |
(move-line start end (if (null n) 1 n))) | |
(global-set-key (kbd "M-s-<up>") 'move-line-up) | |
(global-set-key (kbd "M-s-<down>") 'move-line-down) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment