Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
(defun *magit-delete-trailing-whitespace-from-file ()
"Removes whitespace from the current item."
(interactive)
(save-excursion
(magit-visit-item)
(delete-trailing-whitespace)
(save-buffer)
(kill-buffer))
(magit-refresh))
@Xparx
Copy link

Xparx commented Sep 17, 2015

This code is obsolete with the latest version of magit. My fix was editing line 5 to:

(magit-diff-visit-file-worktree (magit-file-at-point))

@vermiculus
Copy link
Author

vermiculus commented May 1, 2017

Yes; Magit's maintainer provided his own version on my SO question:

(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))

If a reader is seeing this in the far future, I'd check the SO post for any possible updates for new Magit versions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment