0

In order to be able to chain the following command, I need the yanked text to be marked. That is because the end goal is to call (pop kill-ring) so that I can still use whatever was at the top of the kill-ring before duplicating.

I think all of that could be avoided if there was a way to duplicate region without using the kill-ring.

(defun me/duplicate-downward (begin end)
  (interactive "r")
  (kill-region begin end)
  (yank)
  (let (deactivate-mark)
    (push-mark)
    (yank)
    (setq mark-active t)))

Results

Region is correctly duplicated, but region isn't active.

Expected behavior

[] is region, | is point.

Before:

[Lorem
 ipsum]
|

After:

 Lorem
 ipsum
[Lorem
 ipsum]
|
0

I think this will do what you want:

(defun j/dd (begin end)
  (interactive "r")
  (let (deactivate-mark)
    (insert (buffer-substring begin end))
    (save-excursion
      (goto-char end)
      (set-mark-command nil))))
0

Went with this method because @JohnKitchin's leaves the region open. ie. Moving point de/increases the region.

(defun me/duplicate-downward (begin end)
  (interactive "r")
  (let (deactivate-mark (point (point)))
    (insert (buffer-substring begin end))
    (push-mark point)))

(defun me/duplicate-upward (begin end)
  (interactive "r")
  (let (deactivate-mark)
    (save-excursion
      (insert (buffer-substring begin end)))))

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.