sponsor Vim development Vim logo Vim Book Ad

JavaScript Indent : Javascript indenter (HTML indent is included)

 script karma  Rating 2601/694, Downloaded by 17640  Comments, bugs, improvements  Vim wiki

created by
Preston Koprivica
script type
Hosted at: http://code.google.com/p/web-indent/

*Update: (3/30/11) - Release Candidate 1.0 - Progress Started.
*Update: (5/14/10) - Beta 3.0 Released (See Below) (First Release Candidate)
*Update: (5/6/10) - Beta 2.2 Released (See Below)
*Update: (5/6/10) - Beta 2.1 Released (See Below)
*Update: (5/5/10) - Beta 2.0 Released (See Below)
*Update: (5/3/10) - Beta 1.0 Released (See Below)

I wrote this basically due to the lack of any good javascript indent plugin out there.  And due to javascripts lax syntactical structure cindent() just wont cut it.

Web-Indent handles all basic forms of add notes, bugs and multi-line and singe line objects:
var x = {
     y : {

var y = [

var z = {x : x, y : y, w: w};

t; // no indent

Handles all basic control structures (with or without leading '{'):
      // comment

Handles inline and nextline control structure block initializers ('{' in the vernacular):
if(x) {

Handles multi-line function declarations:
var x = function(
      y, // notice one tab over
      w) {

Handles multi-line control structure statements:
if( x == y ||
      y == z ||
      z == w)

This indenter is also fully compatible with Mootools and esp. its Class package.  I haven't tested it with any of the other frameworks, but due to their similar syntactical nature, I'm assuming they will work as will.

Included Files:
      ~test.js - A sample js file with various coding patterns.  Set tabstop=6 and shiftwidth=6 and comments should line up correctly.
      ~indent/javascript.vim - The core indenting file
      ~indent/html.vim - An updated version of the pre-packaged html indenter that utilizes javascript.vim (MUST BE LOCATED IN SAME DIRECTORY!!)

Release Notes:
~1.0 Released: (5/15/10)
      - Fixed: Continuation Lines
      - Removed: Logging

~Beta 3.0 Released: (5/14/10)
      - Fixed: Runaway Indentation.  
      - Fixed: indentkeys now correctly invoke indent.
      - Fixed: Comment lines with keywords are correctly indented.  Thanks to trojhlav for pointing it out.
      - Notes:
            \ First Release Candidate (v 1.0)
            \ Correctly indented entire jQuery source.
            \ Correctly indented entire MooTools source.
      - Concerns:
            \ Still no support for continuation lines.

PS: I'm also not sure if I'm allowed to repost the html.vim, since I couldn't find a license for it.  I'm pretty sure it was the one packaged with vim by default.  If anyone has any objections, I'll take it down immediately.
install details
Installation With Pathogen:
      1. Place in pathogen directory

Installation W/Out Pathogen:
      1. Place in .vim/indent directory, or anywhere in your runtime path.
      2. If placed outside of .vim/indent directory, set g:js_indent = /location/to/javascript.vim in your .vimrc file. (This tells the html.vim file where to find the javascript indent file)

To Disable Logging:
      1.  let g:js_indent_log = 0 in your .vimrc file.

rate this script Life Changing Helpful Unfulfilling 
script versions (upload new version)

Click on the package to download.

package script version date Vim version user release notes
web-indent.zip 1.0 2011-05-15 7.0 Preston Koprivica See: web-indent.tar.gz v 1.0 ~
web-indent.tar.gz 1.0 2011-05-15 7.0 Preston Koprivica Release 1.0!
- Removed indentKeys completely
- Removed Logging
- Continuation Lines Supported!
- Refactored Logic.
web-indent.tar.gz b3.0 2010-05-14 7.0 Preston Koprivica Fixed: Runaway Indentation
Fixed: indentkeys correctly invokes indent
Fixed: Comment lines with keywords.  (Tentative fix)
web-indent.tar.gz b2.2 2010-05-05 7.0 Preston Koprivica Added: Support for while() control block.
Added: Support for do {} while(); control block.
Added: Support for with() {} control block.
Added: Skeleton support for all MULTI-LINE cntrl blocks  SINGLE LINE cntrl blocks DO NOT HAVE SKELETON VERSIONS!!
Added: Skeleton support for function declarations.
Added: Skeleton support for basic objects.
Added: Tests for skeleton code.
web-indent.tar.gz b2.1 2010-05-05 6.0 Preston Koprivica Fixed: Object endings regex.  Now doesn't include preceding {, or [;
Fixed: Multi-Line invocation (Caveat: Must end in a ; or ,)
web-indent.tar.gz b2.0 2010-05-05 7.0 Preston Koprivica Completely Rewrote
Fixed: Determines comment lines by synIDAttr
Added: Anonymous function handling
ip used for rating:

If you have questions or remarks about this site, visit the vimonline development pages. Please use this site responsibly.
Questions about Vim should go to the maillist. Help Bram help Uganda.