Search code examples
emacswhitespacemagit

How can I remove trailing whitespace from a hunk in Magit?


Emacs (post v21) includes a function to delete trailing whitespace from a file. How would I make delete-trailing-whitespace available in the Magit staging area (from magit-status) so that I can remove trailing whitespace from individual hunks or entire files?


Solution

  • This is Sean's snippet, adjusted for Magit v2:

    (defun my-magit-delete-trailing-whitespace-from-file ()
      "Remove whitespace from the current file."
      (interactive)
      (save-excursion
        (magit-diff-visit-file-worktree (magit-file-at-point))
        (delete-trailing-whitespace)
        (save-buffer)
        (kill-buffer))
      (magit-refresh))