Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to autocmd BufWritePost with CoffeeScript vim up a directory?

I'm using this vim plugin for coffeescript and I want to keep all my source files in /src/ even though the compiled files may live elsewhere, like / or /public/js.

I know I can have vim autosave to the current directory using the following (in .vimrc):

autocmd BufWritePost *.coffee silent CoffeeMake!

But I can't figure out how to change that on a per-file basis. I'm trying to do something like this:

:autocmd BufWritePost server.coffee silent CoffeeMake! -o ../server.js

But nothing happens when I do that (when I save my coffee file): no new files are created, and no errors are thrown.

I'd also love it if I could specify a placeholder for the file name, like this (no clue if this is even close to being right):

:autocmd BufWritePost @%.coffee silent CoffeeMake! -o ../@%.js

Can anyone help me make this work the way I want?


EDIT: Coffee expects the -o param to just be the directory (not the file), so the final solution should be something like

:autocmd BufWritePost *.coffee silent execute 'CoffeeMake! -o '.expand('<afile>:p:h').'/../'

... if you wanted it in the root of your web app.

like image 314
neezer Avatar asked Dec 12 '25 00:12

neezer


1 Answers

Try the following command.

:autocmd BufWritePost *.coffee
\   silent execute 'CoffeeMake! -o ' .
\   expand('<afile>:p:h') . '/../' . expand('<afile>:t:r') . 'js'

The name of the file that matched an auto-command can be determined using the <afile> cmdline-special variable. To extract its value in an expression, use the expand() function. Additional modifiers allow to extract the full path or remove the file extension (see :help expand()).

like image 179
ib. Avatar answered Dec 14 '25 15:12

ib.



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!