2010-09-14 55 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))))