Skip to content

Instantly share code, notes, and snippets.

@davazp
Created January 27, 2016 14:37
Show Gist options
  • Save davazp/39d31aa596284f575294 to your computer and use it in GitHub Desktop.
Save davazp/39d31aa596284f575294 to your computer and use it in GitHub Desktop.
Cleanup whitespaces in a magit diff hunk
(defun davazp/magit-cleanup-hunk-whitespace ()
"Cleanup the whitespaces in the diff hunk under the cursor."
(interactive)
(let ((current (magit-current-section)))
(when (eq 'hunk (magit-section-type current))
(let ((file (magit-file-at-point))
(context (caddr (magit-section-value current))))
(cl-destructuring-bind (first-line count)
(mapcar #'string-to-number (split-string context ","))
(save-excursion
(with-current-buffer (find-file-noselect file)
(let (start end)
(goto-char (point-min))
(forward-line (1- first-line))
(setq start (point))
(forward-line (1- count))
(setq end (point))
(whitespace-cleanup-region start end)
(magit-refresh)))))))))
@davazp
Copy link
Author

davazp commented Jan 27, 2016

Bind it with

  (define-key magit-hunk-section-map (kbd "w")  'davazp/magit-cleanup-hunk-whitespace)

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