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

2

7

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?

Pa answered 21/11, 2013 at 17:3 Comment(11)
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.Balikpapan
Magit maintainer here. Magit does not support this.Finnic
@Balikpapan 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.Pa
@Finnic Not yet ;) I've got a working function; I just can't seem to hook it onto the statues-mode-map.Pa
Well, from the currently-selected file, at least: gist.github.com/vermiculus/8177389Pa
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.Finnic
Why don't you remove trailing whitespace on save, with (add-hook 'before-save-hook 'delete-trailing-whitespace)?Judijudicable
@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.Pa
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.Judijudicable
@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!Pa
I haven't tried it myself, but github.com/lewang/ws-butler might help in such cases.Finnic
F
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))
Finnic answered 21/10, 2015 at 2:3 Comment(2)
Thanks 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.Pa
For a hunk section (magit-section-value (magit-current-section)) would return something like ("(defun module-rebuild ()" "-73,6" "+73,8").Finnic
V
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.

Vesuvius answered 16/10, 2017 at 11:35 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.