在Emacs中去除制表符和行末空格

15

我正在处理一个大型项目,其中约有100名工程师在许多文件上工作。 我想知道是否可以在Emacs中添加自定义功能来删除我正在编辑的行末空格并取消制表符缩进。 在与我的更改无关的大型文件中取消制表符和删除空格不是个好主意。(我同意,团队中的每个人都应该遵循一些基本规则,但有时事情并不总是按照我们想的那样。 :( )。

目前我已启用:

(show-ws-toggle-show-trailing-whitespace)
(show-ws-toggle-show-tabs)

使用这些选项存在问题,如果文件的所有者没有修复其制表符和尾随空格,则会使所有文件变为黄色或白色。

如果您能指出emacs选项,使我能够“删除我正在编辑的行中的空格和制表符”(而不是整个文件),那将非常好。


你能澄清一下吗?听起来你只是想在所有打开的缓冲区上映射一个函数,而不是一堆文件,对吗? - pheaver
5个回答

16

这不是对你问题的回答,但我猜你会对它感兴趣:

http://github.com/glasserc/ethan-wspace

它能够跟踪文件是否“干净”(没有尾随空格和制表符),并且在你打开文件时自动删除它们,但只有当文件在开始时是干净的情况下,保存时才会清除它们。这意味着它可以保持一个文件的干净状态,并且将不遵循规则的文件保持原样。


我刚刚发现了ethan-wspace,它真的很棒。 - Noah

8

从我的老旧.emacs文件中:

(defun clean-whitespace-region (start end)
  "Untabifies, removes trailing whitespace, and re-indents the region"
  (interactive "r")
  (save-excursion
    (untabify start end)
    (c-indent-region start end)
    (replace-regexp "[  ]+$" "" nil start end))) ;// uses literal space and tab chars

在选择或标记区域后,请调用M-x clean-whitespace-region

untabify会根据当前的tab-width设置将制表符替换为空格。当我遇到具有奇怪的tab-with值(8、4、3和2似乎很常见)的文件时,我使用c-indent-region

要在整个缓冲区中删除Emacs 21+中的尾随空格,请使用delete-trailing-whitespace,否则regexp-replace与上述相同。


6

自从我开始使用Emacs以来,我一直在使用ws-trim.el插件。它非常好用,只需配置一次,就可以遗忘它。

;;;; ************************************************************************
;;;; *** strip trailing whitespace on write
;;;; ************************************************************************
;;;; ------------------------------------------------------------------------
;;;; --- ws-trim.el - [1.3] ftp://ftp.lysator.liu.se/pub/emacs/ws-trim.el
;;;; ------------------------------------------------------------------------
(require 'ws-trim)
(global-ws-trim-mode t)
(set-default 'ws-trim-level 2)
(setq ws-trim-global-modes '(guess (not message-mode eshell-mode)))
(add-hook 'ws-trim-method-hook 'joc-no-tabs-in-java-hook)

(defun joc-no-tabs-in-java-hook ()
  "WS-TRIM Hook to strip all tabs in Java mode only"
  (interactive)
  (if (string= major-mode "jde-mode")
      (ws-trim-tabs)))

1
我尝试了ws-trim,但不幸的是它安装了一个后置命令钩子,有时会生成错误并完全破坏org-mode。例如,当我从我的日程表缓冲区中按下z'(org-agenda-add-note)时,Org Note'缓冲区不会出现。然后,如果我尝试使用`C-x b'切换缓冲区,迷你缓冲区只有一个字符宽:每次我键入一个新字符时,它都会覆盖上一个。 - Dave Abrahams
1
我可能有些冤枉了ws-trim;我认为这可能是另一个包的问题。我正在尝试重新使用ws-trim。 - Dave Abrahams
当执行查询替换时,如何禁用它?它会修改多行查询替换目标中的空格,使其不匹配任何内容。 - EoghanM

2

我曾经使用这个方法来删除整个文档中的尾随空格。这是我多年前写的...

;;
;; RM-Trailing-Spaces
;;
(defun rm-trailing-spaces ()
  "Remove spaces at ends of all lines"
  (interactive)
  (save-excursion
    (let ((current (point)))
      (goto-char 0)
      (while (re-search-forward "[ \t]+$" nil t)
        (replace-match "" nil nil))
      (goto-char current))))

移动换行符应该这样做吗 (replace-match "\n" nil nil)) - Shubham Pawar

1
如果您想删除当前行的尾随空格,请使用以下命令:

(defun delete-trailing-whitespace-of-current-line ()
  "Delete all the trailing whitespace on the current line.
All whitespace after the last non-whitespace character in a line is deleted.
This respects narrowing, created by \\[narrow-to-region] and friends."
  (interactive "*")
  (save-match-data
    (save-excursion
      (move-to-column 0)
      (if (re-search-forward "\\s-$" nil t)
        (progn
          (skip-syntax-backward "-" (save-excursion (forward-line 0) (point)))
          (delete-region (point) (match-end 0)))))))

将其绑定到您想要的任何键。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接