diff --git a/helpers.el b/helpers.el index 0384a72..f5a98df 100644 --- a/helpers.el +++ b/helpers.el @@ -29,13 +29,26 @@ ;; Duplicate Line ;; -(defun duplicate-line() - (interactive) - (move-beginning-of-line 1) - (kill-line) - (yank) - (newline) - (yank)) +;; From: http://tuxicity.se/emacs/elisp/2010/03/11/duplicate-current-line-or-region-in-emacs.html +(defun duplicate-current-line-or-region (arg) + "Duplicates the current line or region ARG times. +If there's no region, the current line will be duplicated. However, if +there's a region, all lines that region covers will be duplicated." + (interactive "p") + (let (beg end (origin (point))) + (if (and mark-active (> (point) (mark))) + (exchange-point-and-mark)) + (setq beg (line-beginning-position)) + (if mark-active + (exchange-point-and-mark)) + (setq end (line-end-position)) + (let ((region (buffer-substring-no-properties beg end))) + (dotimes (i arg) + (goto-char end) + (newline) + (insert region) + (setq end (point))) + (goto-char (+ origin (* (length region) arg) arg))))) ;; diff --git a/keybindings.el b/keybindings.el index 1706456..69b2a7f 100644 --- a/keybindings.el +++ b/keybindings.el @@ -62,7 +62,7 @@ (global-set-key (kbd "M-n") 'textmate-column-down)) ;; Duplicate line (via helpers.el) -(global-set-key (kbd "C-x C-d") 'duplicate-line) +(global-set-key (kbd "C-x C-d") 'duplicate-current-line-or-region) ;; Align to equal signs (global-set-key (kbd "C-x a =") 'align-to-equals)