Thanks @tarsius for your trick! I was able to tweak it to remove only trailing spaces for a line in a dot. To do this, install the ws-trim package.
(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 bind the delete key, as this is not used for anything useful in magit-status-mode .
source share