Skip to content

Instantly share code, notes, and snippets.

@kch kch/filetrim.rb
Created Jan 1, 2010

Embed
What would you like to do?
removes trailing spaces on every line, leading and trailing empty lines; ensures files end with a line feed
#!/usr/bin/env ruby -pi
#
# Copyright © 2010 Caio Chassot
# Licensed under the WTFPL <http://sam.zoy.org/wtfpl/COPYING>
#
# File Trim. (Vertical and Right trim.)
# Command receives file paths as arguments. Modifies them in-place.
# - removes trailing spaces on every line;
# - removes leading and trailing empty lines;
# - ensures files end with a line feed.
next !$<.eof? && $s && $s << "\n" if $_ !~ /\S/
$_ = $s.to_s << $_.sub(/\s*\z/, "\n")
$s = ("" unless $<.eof?)
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.