How to remove trailing whitespace of all files recursively?

前端 未结 15 1991
难免孤独
难免孤独 2020-12-07 06:58

How can you remove all of the trailing whitespace of an entire project? Starting at a root directory, and removing the trailing whitespace from all files in all folders.

15条回答
  •  悲哀的现实
    2020-12-07 08:05

    This is what works for me (Mac OS X 10.8, GNU sed installed by Homebrew):

    find . -path ./vendor -prune -o \
      \( -name '*.java' -o -name '*.xml' -o -name '*.css' \) \
      -exec gsed -i -E 's/\t/    /' \{} \; \
      -exec gsed -i -E 's/[[:space:]]*$//' \{} \; \
      -exec gsed -i -E 's/\r\n/\n/' \{} \;
    

    Removed trailing spaces, replaces tabs with spaces, replaces Windows CRLF with Unix \n.

    What's interesting is that I have to run this 3-4 times before all files get fixed, by all cleaning gsed instructions.

提交回复
热议问题