Function: magit-copy-diff-as-kill

magit-copy-diff-as-kill is an interactive and byte-compiled function defined in magit-apply.el.

Signature

(magit-copy-diff-as-kill)

Documentation

Save the diff at point to the kill ring.

This command can be used on staged and unstaged changes, and anywhere magit-apply can be used. Additionally, if there is no diff at point but magit-buffer-revision(var)/magit-buffer-revision(fun) is non-nil, or there is a commit at point, the diff for that commit is saved.

Key Bindings

Source Code

;; Defined in ~/.emacs.d/elpa/magit-20260822.1158/magit-apply.el
;;;; Copy

(defun magit-copy-diff-as-kill ()
  "Save the diff at point to the kill ring.
This command can be used on staged and unstaged changes, and anywhere
`magit-apply' can be used.  Additionally, if there is no diff at point
but `magit-buffer-revision' is non-nil, or there is a commit at point,
the diff for that commit is saved."
  (interactive)
  (cond-let
    ([$ (magit-apply--get-selection t)]
     (pcase (list (magit-diff-type) (magit-diff-scope))
       (`(untracked ,(or 'file 'files))
        (user-error "Cannot copy this as a diff"))
       (`(rebase-sequence file)
        (user-error "Cannot copy this as a diff"))
       (`(,_ region) (magit-apply-region $))
       (`(,_   hunk) (magit-apply-hunk   $))
       (`(,_  hunks) (magit-apply-hunks  $))
       (`(,_   file) (magit-apply-diff   $))
       (`(,_  files) (magit-apply-diffs  $))))
    ([rev (or (magit-commit-at-point) magit-buffer-revision)]
     (magit--with-temp-process-buffer
       (magit-git-insert "show" "-p" "--format=" rev)
       (kill-new (buffer-string))))
    ((user-error "Cannot copy this as a diff"))))