Skip to content

Commit

Permalink
Add duplicate-line-or-region to layer better-defaults
Browse files Browse the repository at this point in the history
from https://www.emacswiki.org/emacs/CopyingWholeLines

Duplicate current line, or region if active.
With argument N, make N copies.
With negative N, comment out original line and use the absolute value.

keybinding: `SPC x l d` (text - line - duplicate)
  • Loading branch information
deb0ch committed Oct 11, 2016
1 parent 72d87de commit c011cef
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 0 deletions.
1 change: 1 addition & 0 deletions doc/DOCUMENTATION.org
Original file line number Diff line number Diff line change
Expand Up @@ -2562,6 +2562,7 @@ Text related commands (start with ~x~):
| ~SPC x j r~ | set the justification to right |
| ~SPC x J~ | move down a line of text (enter transient state) |
| ~SPC x K~ | move up a line of text (enter transient state) |
| ~SPC x l d~ | duplicate line or region |
| ~SPC x l s~ | sort lines |
| ~SPC x l u~ | uniquify lines |
| ~SPC x o~ | use avy to select a link in the frame and open it |
Expand Down
23 changes: 23 additions & 0 deletions layers/+distributions/spacemacs-base/funcs.el
Original file line number Diff line number Diff line change
Expand Up @@ -710,6 +710,29 @@ the right."
(interactive)
(call-interactively 'write-file))

;; from https://www.emacswiki.org/emacs/CopyingWholeLines
(defun spacemacs/duplicate-line-or-region (&optional n)
"Duplicate current line, or region if active.
With argument N, make N copies.
With negative N, comment out original line and use the absolute value."
(interactive "*p")
(let ((use-region (use-region-p)))
(save-excursion
(let ((text (if use-region ; Get region if active, otherwise line
(buffer-substring (region-beginning) (region-end))
(prog1 (thing-at-point 'line)
(end-of-line)
(if (< 0 (forward-line 1)) ; Go to beginning of next line, or make a new one
(newline))))))
(dotimes (i (abs (or n 1))) ; Insert N times, or once if not specified
(insert text))))
(if use-region nil ; Only if we're working with a line (not a region)
(let ((pos (- (point) (line-beginning-position)))) ; Save column
(if (> 0 n) ; Comment out original with negative arg
(comment-region (line-beginning-position) (line-end-position)))
(forward-line 1)
(forward-char pos)))))

(defun spacemacs/uniquify-lines ()
"Remove duplicate adjacent lines in region or current buffer"
(interactive)
Expand Down
1 change: 1 addition & 0 deletions layers/+distributions/spacemacs-base/keybindings.el
Original file line number Diff line number Diff line change
Expand Up @@ -418,6 +418,7 @@
"xjl" 'set-justification-left
"xjn" 'set-justification-none
"xjr" 'set-justification-right
"xld" 'spacemacs/duplicate-line-or-region
"xls" 'spacemacs/sort-lines
"xlu" 'spacemacs/uniquify-lines
"xtc" 'transpose-chars
Expand Down

0 comments on commit c011cef

Please sign in to comment.