Skip to content

Instantly share code, notes, and snippets.

@jasonaowen
Last active February 25, 2019 17:15
Show Gist options
  • Save jasonaowen/8b33b8501a9358e26083b1dcf6535a83 to your computer and use it in GitHub Desktop.
Save jasonaowen/8b33b8501a9358e26083b1dcf6535a83 to your computer and use it in GitHub Desktop.
Removing trailing whitespace

Remove trailing whitespace from text files in a git repo.

From git help grep:

-l, --files-with-matches, --name-only, -L, --files-without-match
    Instead of showing every matched line, show only the names of files that contain
    (or do not contain) matches. For better compatibility with git diff, --name-only
    is a synonym for --files-with-matches.
-I
    Don’t match the pattern in binary files.
-E, --extended-regexp, -G, --basic-regexp
    Use POSIX extended/basic regexp for patterns. Default is to use basic regexp.

And, from man sed:

-i[SUFFIX], --in-place[=SUFFIX]
    edit files in place (makes backup if SUFFIX supplied)
#!/usr/bin/env bash
for f in $(git grep --files-with-matches -I --extended-regexp '^.+[ ]+$')
do
sed --in-place 's/[[:space:]]*$//' "$f"
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment