Vim Tips Wiki
No edit summary
No edit summary
Line 2: Line 2:
   
 
<pre>
 
<pre>
  +
if executable('yuicompressor')
  +
 
function Yuic ()
 
function Yuic ()
 
let cwd = expand('<afile>:p:h')
 
let cwd = expand('<afile>:p:h')
Line 18: Line 20:
 
autocmd FileWritePost,BufWritePost *.js :call Yuic()
 
autocmd FileWritePost,BufWritePost *.js :call Yuic()
 
autocmd FileWritePost,BufWritePost *.css :call Yuic()
 
autocmd FileWritePost,BufWritePost *.css :call Yuic()
  +
  +
endif
 
</pre>
 
</pre>
   

Revision as of 10:04, 11 September 2009

If you use yuicompressor to minimize your css and js files. It's necessary to compress the files everytime you save the file. So I wrote a function to do this automatically. Just copy the codes into your vimrc file.

if executable('yuicompressor')

function Yuic ()
    let cwd = expand('<afile>:p:h')
    let nam = expand('<afile>:t:r')
    let ext = expand('<afile>:e')
    if -1 == match(nam, "[\._]src$")
        let minfname = nam.".min.".ext
    else
        let minfname = substitute(nam, "[\._]src$", "", "g").".".ext
    endif
    if filewritable(cwd.'/'.minfname)
        execute '!yuicompressor '.cwd.'/'.nam.'.'.ext.' > '.cwd.'/'.minfname
    endif
endfunction
 
autocmd FileWritePost,BufWritePost *.js :call Yuic()
autocmd FileWritePost,BufWritePost *.css :call Yuic()

endif

Notice

  1. You most install yuicompressor first. And make yuicompressor executable anywhere.
  2. The code executes yuicompressor automatically, only when the compressed file exists. (This means you should execute yuicompressor yourself first time.)