Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Whitespaaaaaaaace! WHITESPAAAAAAAACE!
# requires BSD sed
namespace :whitespace do
desc 'Removes trailing whitespace'
task :cleanup do
sh %{for f in `find . -type f | grep -v .git | grep -v ./vendor | grep -v ./tmp | egrep ".(rb|js|haml|html|css|sass)"`;
do sed -i '' 's/ *$//g' "$f";
done}, {:verbose => false}
puts "Task cleanup done"
end
desc 'Converts hard-tabs into two-space soft-tabs'
task :retab do
sh %{for f in `find . -type f | grep -v .git | grep -v ./vendor | grep -v ./tmp | egrep ".(rb|js|haml|html|css|sass)"`;
do sed -i '' 's/\t/ /g' "$f";
done}, {:verbose => false}
puts "Task retab done"
end
desc 'Remove consecutive blank lines'
task :scrub_gratuitous_newlines do
sh %{for f in `find . -type f | grep -v .git | grep -v ./vendor | grep -v ./tmp | egrep ".(rb|js|haml|html|css|sass)"`;
do sed -i '' '/./,/^$/!d' "$f";
done}, {:verbose => false}
puts "Task scrub_gratuitous_newlines done"
end
desc 'Execute all WHITESPACE tasks'
task :all do
Rake::Task['whitespace:cleanup'].execute
Rake::Task['whitespace:retab'].execute
Rake::Task['whitespace:scrub_gratuitous_newlines'].execute
end
end
# requires GNU sed
namespace :whitespace do
desc 'Removes trailing whitespace'
task :cleanup do
sh %{for f in `find . -type f | grep -v .git | grep -v ./vendor | grep -v ./tmp | egrep ".(rb|js|haml|html|css|sass)"`;
do sed -i 's/[ \t]*$//' $f;
done}, {:verbose => false}
puts "Task cleanup done"
end
desc 'Converts hard-tabs into two-space soft-tabs'
task :retab do
sh %{for f in `find . -type f | grep -v .git | grep -v ./vendor | grep -v ./tmp | egrep ".(rb|js|haml|html|css|sass)"`;
do sed -i 's/\t/ /g' $f;
done}, {:verbose => false}
puts "Task retab done"
end
desc 'Remove consecutive blank lines'
task :scrub_gratuitous_newlines do
sh %{for f in `find . -type f | grep -v .git | grep -v ./vendor | grep -v ./tmp | egrep ".(rb|js|haml|html|css|sass)"`;
do sed -i '/./,/^$/!d' $f;
done}, {:verbose => false}
puts "Task scrub_gratuitous_newlines done"
end
desc 'Execute all WHITESPACE tasks'
task :all do
Rake::Task['whitespace:cleanup'].execute
Rake::Task['whitespace:retab'].execute
Rake::Task['whitespace:scrub_gratuitous_newlines'].execute
end
end
@jteneycke

This comment has been minimized.

Copy link

jteneycke commented Sep 18, 2013

Dude, this is fantastic! Thanks!

@raulgobr

This comment has been minimized.

Copy link

raulgobr commented Sep 13, 2014

Thanks mate. Saved my day!

@marvindanig

This comment has been minimized.

Copy link

marvindanig commented Oct 10, 2014

I'm getting a lot of errors when I run $ rake whitespace:all task:
Like this:

sed: RE error: illegal byte sequence
sed: RE error: illegal byte sequence
sed: RE error: illegal byte sequence
sed: RE error: illegal byte sequence
sed: RE error: illegal byte sequence
sed: RE error: illegal byte sequence
sed: RE error: illegal byte sequence
sed: RE error: illegal byte sequence
sed: RE error: illegal byte sequence
sed: RE error: illegal byte sequence
sed: RE error: illegal byte sequence
sed: RE error: illegal byte sequence
sed: RE error: illegal byte sequence
sed: RE error: illegal byte sequence

I'm on Mac. Is this normal?

@anthonyto

This comment has been minimized.

Copy link

anthonyto commented Apr 4, 2017

Still incredibly useful. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.