Skip to content

Instantly share code, notes, and snippets.

@dusdanig
Created June 11, 2014 18:40
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 dusdanig/b79c15a83368958feb96 to your computer and use it in GitHub Desktop.
Save dusdanig/b79c15a83368958feb96 to your computer and use it in GitHub Desktop.
Transpose lines for emacs that makes sense to me
;; Dusdanig transpose lines
;;
;; The transpose lines (C-x C-t) really anoyed me. I kept squinting my
;; eyes to see what lines would be transposed. Also the point was
;; never near the line which should have been transposed. It made no
;; sense to me.
;;
;; The solution this listing implements will swap the previous and
;; current line when the point is aproximatly at the start of the line
;; and will swap the current and next line if the point is aproximatly
;; at the end of the line.
;;
;; This enables me to move the lines contextually without introducing
;; separate keybinding for each action.
;;
(defun dusdanig-aprox-begin-of-line ()
"Check whether I am on the begin or end of a line"
(< (- (point) (line-beginning-position))
(- (line-end-position) (point)))
)
(defun dusdanig-move-line-up ()
"Move up the current line."
(interactive)
(transpose-lines 1)
(forward-line -2)
(indent-according-to-mode)
(beginning-of-line))
(defun dusdanig-move-line-down ()
"Move down the current line."
(interactive)
(forward-line 1)
(transpose-lines 1)
(forward-line -1)
(indent-according-to-mode)
(end-of-line))
(defun dusdanig-transpose-lines ()
(interactive)
(if (dusdanig-aprox-begin-of-line)
(dusdanig-move-line-up) (dusdanig-move-line-down))
)
(global-set-key (kbd "C-x C-t") 'dusdanig-transpose-lines)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment