Skip to content

Instantly share code, notes, and snippets.

@elfenlaid
Last active January 3, 2016 12:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save elfenlaid/8463366 to your computer and use it in GitHub Desktop.
Save elfenlaid/8463366 to your computer and use it in GitHub Desktop.
Move region up\down
;;; 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