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.

#!/usr/bin/ruby

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

$stdin.each_line do |line|
  line.rstrip!

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

  print line
  print ENV['CODA_LINE_ENDING']
end

14 comments:

  1. Thank you for creating this plugin, it's much appreciated!

    ReplyDelete
  2. Doesn't seem to work for me. Trailing whitespace is not removed, the cursor moves to the end of the file and actually adds more whitespace; a new line.

    ReplyDelete
  3. If your file doesn't end with a newline character, then this script will add one. The rest of the behaviour you described is unexpected though. Are you using coda 1.6.1? Does your computer have ruby installed? At the command line if you type "ruby -v" it can help me fix the issue.

    ReplyDelete
  4. Thanks for your dedication. My Coda version is 1.6.2. and the Ruby version is 1.8.6 (2008-03-03 patchlevel 114). Don't sweat it if this doesn't help, though, not that big of a deal to me.

    ReplyDelete
  5. That is the exact configuration I have. I wonder why it isn't working.

    If you type '/usr/bin/ruby -v' do you get the same version? Feel free to email me a test file so I can figure out what is going on.

    glowacz at gmail

    ReplyDelete
  6. Hi Kevin

    I wonder how the plugin scales. I just tried applying it on a ~1000 lines .css file and Coda crashed several times (not responding).

    Any thoughts?

    ReplyDelete
  7. Yeah, I'm not sure if it is the fault of the plugin or Coda itself. The plugin just reads from STDIN and writes to STDOUT and coda does the rest. I'll look in to this though.

    ReplyDelete
  8. Yes, I am having the same issue. The plugin makes Coda hang/not respond. Have to force quit Coda.

    ReplyDelete
  9. I have the crashing issue also, sometimes it happens and sometimes it doesn't, I haven't been able to figure out why.

    ReplyDelete
  10. It would be really nice if before you actually changed each line, you checked if the rstripped line length was less than the original line length.

    That way, in Coda, the file wouldn't be marked as modified unless a line actually changed.

    ReplyDelete
  11. Any luck with the crashing issue?

    ReplyDelete
  12. Since the crashing issue was not resolved, I wrote my own Coda plugin to remove trailing whitespace. It's native Objective-C and can handle documents with tens of thousands of lines. It also doesn't mark files as modified unless there was a change. You can download it here:

    http://erikhinterbichler.com/software/white-out/

    ReplyDelete
  13. awesome. I'm sure a native version will work much better. Thanks!

    ReplyDelete
  14. Thanks a lot for this plug-inn. I've been trying to figure out how to remove the trailing spaces within Coda forever.

    ReplyDelete