Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 326
Sean Allred Avatar asked Nov 21 '13 17:11

Sean Allred


1 Answers

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))
like image 162
tarsius Avatar answered Oct 20 '22 20:10

tarsius