Vim Tips Wiki
(Insert TipProposed template)
Line 1: Line 1:
  +
{{TipProposed
  +
|id=0
  +
|previous=0
  +
|next=0
  +
|created=September 11, 2009
  +
|complexity=basic
  +
|author=Othree
  +
|version=7.0
  +
|subpage=/200909
  +
|category1=
  +
|category2=
  +
}}
 
If you use [http://developer.yahoo.com/yui/compressor/ yuicompressor] to minimize your css and js files it is necessary to compress each file whenever you save it. The following function does this automatically; just copy the code into your [[vimrc]]:
 
If you use [http://developer.yahoo.com/yui/compressor/ yuicompressor] to minimize your css and js files it is necessary to compress each file whenever you save it. The following function does this automatically; just copy the code into your [[vimrc]]:
 
<source lang="vim">
 
<source lang="vim">
Line 31: Line 43:
 
yuicompressor example.js > example.min.js
 
yuicompressor example.js > example.min.js
 
</source>
 
</source>
After example.min.js is created. Everytime you edit and save example.js. Vim will auto execute the command on the above to compress the js file. This function can handle both js and css. And three kind of file name pairs:
+
After example.min.js is created. Everytime you edit and save example.js. Vim will auto execute the command on the above to compress the js file. This function can handle both js and css. And three kind of file name pairs:
 
* blah.js > blah.min.js
 
* blah.js > blah.min.js
 
* blah.src.js > blah.js
 
* blah.src.js > blah.js

Revision as of 10:44, 15 September 2009

Proposed tip Please edit this page to improve it, or add your comments below (do not use the discussion page).

Please use new tips to discuss whether this page should be a permanent tip, or whether it should be merged to an existing tip.
created September 11, 2009 · complexity basic · author Othree · version 7.0

If you use yuicompressor to minimize your css and js files it is necessary to compress each file whenever you save it. The following function does this automatically; just copy the code into your vimrc:

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

Notes:

  • You most install yuicompressor and make yuicompressor executable from any directory.
  • The code executes yuicompressor automatically, but only when the compressed file exists, so you need to execute yuicompressor manually the first time.

Comments

Thanks for the tip, but we need a little more info to avoid having each reader spend time working out what actually happens. I install yuicompressor and put the above code in vimrc. I edit example.js. I make some changes and save the file. What happens then? JohnBeckett 10:25, September 11, 2009 (UTC)

Thanks for reformatting. At first, You must use yuicompressor to compress example.js youself. In most case, the compressed file will name as example.min.js. So you might execute the following command:

yuicompressor example.js > example.min.js

After example.min.js is created. Everytime you edit and save example.js. Vim will auto execute the command on the above to compress the js file. This function can handle both js and css. And three kind of file name pairs:

  • blah.js > blah.min.js
  • blah.src.js > blah.js
  • blah_src.js > blah.js

Othree 03:52, September 12, 2009 (UTC)