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?
Asked
Active
Viewed 701 times
7

Sean Allred
- 3,558
- 3
- 32
- 71
-
Now, I tried `magit`. My guess that you could edit the diffs does not look good. This sort of works with `ediff`-patching but not with git. So, I will delete my answer. I am very sorry about that. – Tobias Nov 22 '13 at 03:59
-
Magit maintainer here. Magit does not support this. – tarsius Dec 30 '13 at 01:47
-
@Tobias No worries—as I recall, you were still right in that I could go right to the file from within Magit. From there its a pretty simple function call. – Sean Allred Dec 30 '13 at 02:34
-
@tarsius Not yet ;) I've got a working function; I just can't seem to hook it onto the statues-mode-map. – Sean Allred Dec 30 '13 at 02:51
-
1Well, from the currently-selected file, at least: https://gist.github.com/vermiculus/8177389 – Sean Allred Dec 30 '13 at 03:27
-
You might wanna post your snippet as an answer. It's good enough, there are of course a few things that could be improved, i.e. make sure we actually are on a file and don't kill the buffer if it already existed before using this command. – tarsius Jul 28 '14 at 20:26
-
Why don't you remove trailing whitespace on save, with `(add-hook 'before-save-hook 'delete-trailing-whitespace)`? – Oct 21 '15 at 03:50
-
@torazaburo In this case, as I recall, it would've created an obscenely massive diff. People would've yelled at me. You can imagine how the government can be. – Sean Allred Oct 21 '15 at 04:24
-
Sure. I guess the govt would prefer smaller diffs to having its code meet trailing whitespace standards. And I guess it does not matter for them that any diffing tool can be told to ignore whitespace changes. Anyway, good luck. – Oct 21 '15 at 04:28
-
@torazaburo Well I tried changing the options to the difftool, but they got a little upset there as well :) I've long since left that job (this question is from 2013), but thanks! – Sean Allred Oct 21 '15 at 04:34
-
I haven't tried it myself, but https://github.com/lewang/ws-butler might help in such cases. – tarsius Oct 21 '15 at 14:21
2 Answers
3
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))

tarsius
- 8,147
- 5
- 32
- 46
-
1Thanks for the update. Is there any way to limit this to the hunk? Does the overlay in magit-status have any information about where the hunk comes from? If so, you could use that information to narrow before deleting the whitespace. – Sean Allred Oct 21 '15 at 04:27
-
1For a hunk section `(magit-section-value (magit-current-section))` would return something like `("(defun module-rebuild ()" "-73,6" "+73,8")`. – tarsius Oct 21 '15 at 14:19
0
Thanks @tarsius's for your great trick! I managed to tweak it to remove just trailing whitespaces for the line at point. This requires the 'ws-trim' package installed.
(defun my-magit-delete-trailing-whitespace ()
"Remove whitespace from the current file."
(interactive)
(save-excursion
(magit-diff-visit-file-worktree (magit-file-at-point))
(ws-trim-line nil)
(save-buffer)
(kill-buffer))
(magit-refresh))
(add-hook 'magit-status-mode-hook
(lambda ()
(local-set-key [deletechar] 'my-magit-delete-trailing-whitespace)))
I'm binding the delete key since that isn't used for anything useful in magit-status-mode
.

kristianlm
- 1,071
- 1
- 10
- 14