7

Windows 10, Emacs 25.1, Magit

I select some text (Shift + Arrow Down) from the Unstaged changes section. Here is a screenshot:

Select_text

Then I paste into another buffer (*scratch*).

This is the result:

Result_paste

Is it possible to strip the leading +s when killing lines from diff hunks?

8

Magit doesn't support that currently, but it should be doable. I'll probably do that soon. I sometimes could use that feature too.

Meanwhile use a macro to replace the +s after yanking: <f3> C-d C-n <f4>....

  • 1
    After yank I do next: 1) Set cursor over first "+", 2) Start command <C-return> (cua-set-rectangle-mark) , 3) Select only "+" (one column) and press Del for delete all symbols "+" – Alex Oct 11 '17 at 9:56
  • @Alex Could you not use cua-rectangle-mark-mode from within the magit buffer? – 0x5453 Oct 11 '17 at 12:49
  • I use cua-rectangle-mark-mode in another buffer (not magit) after yank text. – Alex Oct 11 '17 at 13:18
  • Without CUA, after yank: C-a C-f C-x C-x C-x r d. – Alan Third Oct 12 '17 at 13:20
4

Until Magit supports something like this, the behavior is easy to get with a custom command. I have something similar to the following command in my configuration. If you call it with a prefix argument, it will remove the leading diff markers.

(defun km/magit-copy-region-hunk (&optional no-column)
  (interactive "P")
  (when (magit-section-internal-region-p)
    (magit-section-when hunk
      (deactivate-mark)
      (let ((text (buffer-substring-no-properties
                   (region-beginning) (region-end))))
        (kill-new (if no-column
                      (replace-regexp-in-string "^[ \\+\\-]" "" text)
                    text))))))

Edit: With the developmental version of Magit (what will be v2.91.0), you can strip the diff markers by marking a region of a hunk and calling magit-copy-section-value with a prefix argument.

  • No docstrings in your configuration?! For shame. :) Works well though +1. – Basil Oct 12 '17 at 15:38
1

Going off of Kyle Meyer's answer, if you want to copy the diff without needing to hit the prefix key, you can use the following:

(defun copy-diff-region ()
  "Copy diff region without + or - markers."
  (interactive)
  (deactivate-mark)
  (let ((text (buffer-substring-no-properties
               (region-beginning) (region-end))))
    (kill-new (replace-regexp-in-string "^[\\+\\-]" "" text))))

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.