Published: 2011-10-20
You can configure Vim to automatically compile CoffeeScript files when you save them from within Vim, it’s easy, just add an autocmd to run the CoffeeScript compiler to your ~/.vimrc file:
autocmd BufWritePost,FileWritePost *.coffee :silent !coffee -c <afile> |
I prefer this to the CoffeeScript compiler --watch option because you get to see any compiler errors in the editor when you save the CoffeeScript source file.
|
Tip |
Use Ctrl+L to clear any compiler errors and redraw the screen. |
This technique works, but what happens if you want to selectively include CoffeeScript compiler options with some files and not others? I came up against this problem writing QUnit tests using CoffeeScript which must be compiled without the top-level function wrapper using the --bare command-line option.
My solution was to take a cue from Vim’s mode line feature and write a CoffeeScript compiler wrapper shell script which scans the end of the CoffeeScript source file for a comment line starting with # coffee:, it then passes the remainder of the line to the CoffeeScript compiler as command-line options. Here’s a basic CoffeeScript QUnit test file specifying the --bare option in the trailing mode line:
module 'Test'
test 'addition', ->
equals 1+1, 2, 'one and one is two'
# coffee: --bare
|
And here’s the coffee.sh wrapper script:
#!/bin/sh # # 'Mode line' wrapper for CoffeeScript 'coffee' compiler command. # # Executes CoffeeScript compiler and includes compiler options from line at the # end of the CoffeeScript source file starting with '# coffee:' (c.f. Vim mode # lines). # # Last argument is the source file name. for srcfile; do true; done # Search the last 5 lines in the source for a mode line and extract the # trailing coffee command options. pat='^#\s*coffee:' opts=`tail -n5 $srcfile | grep "$pat" | sed "/$pat/s/$pat//"` coffee $opts $@ |
You also need to change coffee to coffee.sh in your ~/.vimrc autocmd plus you need to ensure both coffee to coffee.sh are in your shell search PATH:
autocmd BufWritePost,FileWritePost *.coffee :silent !coffee.sh -c <afile> |
October 27, 2011 at 9:30 pm |
What about :CoffeeMake that comes with Vim CoffeeScript plugin? :)
https://github.com/vim-scripts/vim-coffee-script