Tuesday, December 9, 2008

Coda Plug-in: Remove Trailing Whitespace

Edit: Erik Hinterbichler has written a much better native plugin called White Out. I highly recommend using that one instead of this one.

I've been using Coda to do all of my programming for quite some time now. One thing I wish it had, though, is the ability to remove trailing white space from lines when the file is saved.

With Coda 1.6 they added the ability to create plug-ins, and with the release of 1.6.1 a few days ago they have added the ability to manipulate the whole document with plug-ins. After hearing this I decided to write my own to take care of that pesky trailing white space issue. Coda doesn't have the ability to have plug-ins run upon file save, but it's the best I could do with what they provide and what I know how to do.

You can download the plug-in here:

Remove Trailing Whitespace v1.0

Here is the code I used to write it. The $$IP$$ stuff is so that the insertion point remains where it was before the code is run. If I leave it out, the insertion point goes to the end of the file. You can also visit its github repository.


ip_line = ENV['CODA_LINE_NUMBER'].to_i
ip_index = ENV['CODA_LINE_INDEX'].to_i

$stdin.each_line do |line|

  if $stdin.lineno == ip_line
    ip_index = line.length if (line.length) < ip_index
    line = line.insert(ip_index, "$$IP$$")

  print line