Skip to content

Instantly share code, notes, and snippets.

@kch
Created January 1, 2010 23:57
Show Gist options
  • Save kch/267312 to your computer and use it in GitHub Desktop.
Save kch/267312 to your computer and use it in GitHub Desktop.
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