Vim Tips Wiki
No edit summary
No edit summary
Line 22: Line 22:
 
Notice
 
Notice
 
# You most install yuicompressor first. And make yuicompressor executable anywhere.
 
# You most install yuicompressor first. And make yuicompressor executable anywhere.
# Only when the compressed file exists, this code will do yuicompressor automatically. (This means you must do compress yourself at first time.)
+
# Only when the compressed file exists, this code will do yuicompressor automatically. (This means you should execute yuicompressor yourself first time.)

Revision as of 09:32, 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.

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

Notice

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