Today I Learned
How to minify a css or js file in vim
Posted on January 26, 2018

I wanted a quick and easy way to minify a CSS file for this site, without adding any fancy build tools like webpack, using a quick command.

The ideal workflow was:

  • Open a regular CSS file.
  • Minify the current file.
  • Save over the file.

After some digging and a node library, I came up with this.

  1. Install Minify via NPM
    npm install -g minify
    
  2. Open css file in vim
    vim style.css
    
  3. Now that you are in vim, create a new line at the top of the file using Shift + O
  4. Read in the existing file, parse it through minify and paste it in place.
    :r !minify %
    
  5. Move down a line to the uncompressed css and delete the rest of the file using d + G

If you are looking to minify lots of files for an app there are clearly better options, but if you just want to shrink the size of a single file on a static site this is my new favorite.