I’m an Ubuntu user and I love it. But: I don’t have TextMate so good old gedit has to suffice. One particular thing annoyed me, though. Say I have a controller that looks something like this:
class ForumsController < ApplicationController
def new
@forum = Forum.new
end
end
What do I do when I want to add, e.g., a create action? I put my cursor after the end keyword for the new action, press enter, delete the two extra spaces (that gedit creates because auto-indentation is turned on) with Shift + Tab, press enter again and then Tab.
I do that a lot during a day. In my spec files things are even worse. Because of the nesting I have to tab in and out maybe eight times. That's genuine waste of time. If I had an awesome editor, things would be taken care of for me, but I don't. So what I did was to write a pre-commit hook for Git instead:
#!/usr/bin/env ruby
Dir["{app,config,features,lib,spec}/**/*"].each do |filename|
unless File.directory?(filename)
with_trailing_whitespace = File.read(filename)
without_trailing_whitespace = with_trailing_whitespace.gsub(/ +$/, '')
unless with_trailing_whitespace == without_trailing_whitespace
File.open(filename, 'w') do |file|
file.puts(without_trailing_whitespace)
end
`git add #{filename}`
end
end
end
Basically, what it does is to recursively loop through all files in the app, config, features, lib, and spec directories of my project. If there's any difference between the original version of a file with trailing whitespace and the one without, the script will update the file and add it to Git's index.
To install the hook, simply copy and paste the script into .git/hooks/pre-commit in any Git repository, chmod it to 0755 and you're off.
I still haven't found a way to maintain this behaviour throughout all my projects; adding a hook to one repository has no effect on others. Once that's done I have conquered the trailing whitespace!