2010-09-14 54 views
13

我正在从事大型项目,其中有大约100位工程师在处理许多文件。我想看看是否可以在emacs中添加自定义功能来删除尾随空格,并清除我正在编辑的行。在与我的更改无关的大文件中消除和删除空格不是一个好主意。 (我同意,大家在团队应该遵循一些基本的规则做什么,有时它不会以这种方式工作:(。)unabify和删除emacs中的尾随空格

目前我已经启用。

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

这些选项的问题,如果文件的所有者没有修复他的标签和尾随空格,它会使所有文件变成黄色或白色

如果你能指向我的emacs选项,这将使我“删除我正在编辑的行中的空格和制表符“(不是全部文件)。

+0

您能澄清吗?这听起来像你只是想将一个函数映射到所有打开的缓冲区而不是一堆文件,对吗? – pheaver 2010-09-14 00:39:10

回答

5

自从我开始使用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,不幸的是它安装了一个post-command-hook,有时会产生错误并且完全是borks org-mode。例如,当我从我的议程缓冲区中点击'z'(org-agenda-add-note)时,'* Org Note *'缓冲区不会出现。然后,如果我尝试使用'C-x b'切换缓冲区,则小缓冲区只有一个字符宽度:每次键入一个新字符时,它都会覆盖最后一个字符。 – 2011-09-27 14:37:28

+1

我可能对ws-trim不公平;我认为这可能是另一个软件包的问题。我正在尝试ws-trim – 2011-10-15 06:33:59

+0

如何在执行查询替换时禁用它?它修改多行查询中的空白 - 替换目标,以使它们不匹配任何内容。 – EoghanM 2015-03-25 16:19:00

15

这不是你的问题的答案。但我怀疑你会有兴趣去了解它:

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

它保持的文件是否是“干净”(无结尾的空白和标签),当你打开它,跟踪,并会自动将其删除当你保存它,当且仅当它在你开始时是干净的。这意味着如果文件开始清理干净,它将保持文件干净,并且会留下任何肮脏的文件(即其他人不遵守规则)。

+0

做这份工作,出色的建议。谢谢! – ptrn 2012-01-04 03:44:17

+0

我刚刚遇到了ethan-wspace,它规则了 – Noah 2013-04-09 13:03:52

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设置,用空格替换制表符。然后我使用c-indent-region当我给与奇怪的tab-with值(8,4,3和2常见)的文件。

remove trailing whitespace in emacs 21+在整个缓冲区使用delete-trailing-whitespace否则regexp-replace如上工作。

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

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

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