7

Emacs (post v21) 包含一个从文件中删除尾随空格的功能。我将如何delete-trailing-whitespace在 Magit 暂存区域(来自magit-status)中提供,以便我可以从单个大块或整个文件中删除尾随空格?

4

2 回答 2

3

这是 Sean 的片段,针对 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))
于 2015-10-21T02:03:56.603 回答
0

感谢@tarsius 的绝招!我设法对其进行了调整,以仅删除该行的尾随空格。这需要安装“ws-trim”包。

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

我正在绑定删除键,因为它不用于任何有用的magit-status-mode.

于 2017-10-16T11:35:48.680 回答