开发者

How to autocmd BufWritePost with CoffeeScript vim up a directory?

开发者 https://www.devze.com 2023-03-22 14:31 出处:网络
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'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 s开发者_开发百科ilent 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.


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()).


If you have app with file structure like this (or any other structure with different directory name ):

app -- 
      coffee --
                module1 -- 
                           ..files
                module2 --
                           ..files
      js --
                module1 -- 
                           ..files
                module2 --
                           ..files

you might want to consider this as well:

autocmd BufWritePost,FileWritePost *.coffee silent execute 'CoffeeMake! -o '.expand('%:p:h:s?coffee?js?') 

the last bit :s?coffee?js? replaces first occurance of coffee with js in your save path

more info: :help filename-modifiers

0

精彩评论

暂无评论...
验证码 换一张
取 消