public
Last active

Recursively remove trailing whitepace from files.

  • Download Gist
remove_trailing_whitespace.sh
Shell
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
# 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

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.