Skip to content

Instantly share code, notes, and snippets.

@dtuite
Last active October 12, 2023 16:46
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save dtuite/6314519 to your computer and use it in GitHub Desktop.
Save dtuite/6314519 to your computer and use it in GitHub Desktop.
Recursively remove trailing whitepace from files.
# Remove trailing whitespace from files in the src directory.
# This is useful because (for example) trailing whitespace hinders Vim's
# paragraph movement.
# INFO: http://stackoverflow.com/a/4438318/574190
# INFO: http://stackoverflow.com/q/1583219/574190
# The directory under which we want to search for files.
ROOT_DIRECTORY="$1"
# Get a list of files in the src directory (this is specific to this project)
# which match the passed in matcher.
function find_src_files_of_name {
find "$ROOT_DIRECTORY" -type f -name "$1" -print0
}
# Given a list of files, remove trailing whitespace from them.
function remove_trailing_whitespace {
# This is specific to OSX. The linux version would not need the single quotes
# after the -i flag.
# The e flag indicates extended regular expressions.
xargs -0 sed -i '' -e 's/[[:space:]]*$//'
}
# Usage Examples
find_src_files_of_name '*.scss' | remove_trailing_whitespace
find_src_files_of_name '*.js' | remove_trailing_whitespace
find_src_files_of_name '*.html' | remove_trailing_whitespace
find_src_files_of_name '*.rb' | remove_trailing_whitespace
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment