开发者

untabify and remove trailing whitespaces in emacs

开发者 https://www.devze.com 2023-01-15 20:26 出处:网络
I\'m working on large scale project where in there are ~100 engineers working on many files. I would like to see if I could add customization in emacs to remove trailing white spaces and untabify the

I'm working on large scale project where in there are ~100 engineers working on many files. I would like to see if I could add customization in emacs to remove trailing white spaces and untabify the lines which i'm editing. It wont be good idea to untabify and remove whitespaces in large files which are not related my changes. (I agree, everyone in the team should be following some basic rules. What to do, sometime it doesn't work in that way. :( ).

Currently I've enabled:

(show-ws-toggle-show-trailin开发者_如何学运维g-whitespace)
(show-ws-toggle-show-tabs)

Issue with these options, it makes all over files either yellow or white if owner of the file hasn't fixed his tabs and trailing whitespaces.

It would be great if you could point me to emacs options which will enable me "remove whitespaces and tabs on lines which I'm editing" (not all over the files).


This is not an answer to your question. But I suspect you will be interested to know about it:

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

It keeps track of whether a file is "clean" (free of trailing whitespace and tabs) when you open it, and will automatically remove them when you save it if and only if it was clean when you started. This means it will keep a file clean if it started clean, and will leave alone any files that were dirty (i.e. someone else didn't follow the rules).


From my crufty old .emacs file:

(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

Invoke M-x clean-whitespace-region after selecting or marking a region.

untabify replaces tabs with spaces depending on your current tab-width settings. I then use c-indent-region for when I'm given files with strange tab-with values (8, 4, 3, and 2 seem common).

To remove trailing whitespace in emacs 21+ in the entire buffer use delete-trailing-whitespace otherwise regexp-replace works as above.


I've used the ws-trim.el package ever since I started using Emacs. It works like a charm; configure it and forget it.

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


I've use this to remove trailing white spaces from the whole document. I wrote it years ago...

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


If you want to delete trailing whitespaces on the current line, use the following command :

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

Bind it to whatever key you want.

0

精彩评论

暂无评论...
验证码 换一张
取 消