121

I spend my days in vim, currently writing a lot of JavaScript. I've been trying to find a way to integrate JSLint or something similar into vim to improve my coding. Has anyone managed to do something like this?

I tried this: Javascript Syntax Checking From Vim, unfortunately the output is very crude.

6 Answers 6

188

The best-practice way IMO is:

  1. Install Syntastic Vim plugin - Best syntax-checker around for plenty of languages, plus it integrates with Vim's location-list (==quickfix) window.
  1. Choose one of the two options below:

JSLint

  1. Install jsl (JSLint executable) using your favorite package manager (Ubuntu's apt-get, Mac's home brew, etc.).

Community-driven jshint.com (better than JSLint)

  1. Install node.js using your favorite package manager.
  2. Install Node Package Manager: 'curl https://npmjs.org/install.sh | sh' EDIT: npm IS PART OF node.js NOW
  3. Install jshint globally: 'npm install jshint -g'
  4. Put your jshint config file in your $HOME dir: '~/.jshintrc'
  5. Overwrite Syntastic's syntax_checkers/javascript.vim file with this one - EDIT: NO LONGER NECESSARY WITH NEWEST SYNTASTIC VERSION.

Enjoy! :)

16
  • 1
    I'm giving this a try right now. Tried cloning Syntastic and using the "rake install" command and then replaced the javascrip.vim file with yours. Still can't figure out how to use it, :SyntasticEnable doesn't do anything as far as I can see. I'll keep trying though.. May 27, 2011 at 22:08
  • 2
    Ok, noticed that Syntastic requires ballon_eval, wich isn't supported in MacVim. Too bad.. May 28, 2011 at 19:43
  • 1
    Tried adding it, hope it (or a prettier version of it) gets pulled github.com/scrooloose/syntastic/pull/47 May 30, 2011 at 20:02
  • 9
    I just spent a long time trying to figure out why nothing appeared to be happening... if you pulled Ory's .jshintrc be aware that there's a trailing comma after "google" in the "predef" object that causes jshint to fail to load and Syntastic to fail silently.
    – ThePants
    Apr 27, 2012 at 20:03
  • 1
    jsl for Windows: javascriptlint.com/download.htm also, watch out in the jsl.vim file for this issue: vim.1045645.n5.nabble.com/…
    – Andrew
    Jun 5, 2012 at 22:08
34

You can follow the intructions from JSLint web-service + VIM integration or do what I did:

Download http://jslint.webvm.net/mylintrun.js and http://www.jslint.com/fulljslint.js and put them in a directory of your choice.

Then add the following line to the beginning of mylintrun.js:

var filename= arguments[0];

and change last line of code in mylintrun.js ("print( ...)") to:

 print ( filename + ":" + (obj["line"] + 1) + ":" + (obj["character"] + 1) + ":" + obj["reason"] );

This makes in mylintrun.js output a error list that can be used with the VIM quickfix window (:copen).

Now set the following in VIM:

set makeprg=cat\ %\ \\\|\ /my/path/to/js\ /my/path/to/mylintrun.js\ %
set errorformat=%f:%l:%c:%m

where you have to change /my/path/to/js to the path to SpiderMonkey and /my/path/to/mylintrun.js to the path where you put the JS files.

Now, you can use :make in VIM and use the quickfix window (:he quickfix-window) to jump from error to error.

8
  • Why are you catting the buffer and passing it as an argument to mylintrun?
    – jamessan
    Feb 11, 2010 at 2:59
  • @jamessan: the mylintrun.js script reads the file from stdin. So that's what the catting is for. For the error output mylintrun.js needs also the file name, so it is given as an argument. You could modify the script to open the file for reading instead of reading from stdin (making the cat unnecessary). But I didn't want to bother with this... and I believe there is a problem reading files if you have a JavaScript engine compiled w/o the FileObject
    – f3lix
    Feb 11, 2010 at 10:37
  • what is 'the path to SpiderMonkey' on your machine - I've got a few on mine May 18, 2010 at 11:57
  • I can't get at mylintrun.js, is there an alternative location. it appears to be dead
    – bluekeys
    Mar 20, 2012 at 15:45
  • 1
    The links to mylintrun.js and fulljslint.js are broken which is rendering this answer less than useful because it relies heavily on them. Please can you update with working urls? Otherwise I'll need to delete this answer which is a bummer because it looks quite good and has helped a lot of folks. Thanks.
    – Kev
    Sep 20, 2012 at 23:50
17

Another option is jslint.vim from Jesse Hallet. It's available on GitHub and works with or without Vim's QuickFix window. It's a nice plugin!

3
  • 1
    +1 for this. Works great. Conveniently downloadable from vim.org/scripts/script.php?script_id=2729. Note, on Windows you'll need to install Ruby.
    – mm2001
    Mar 1, 2011 at 19:33
  • 3
    Also be sure to check out JSHint, mentioned below.
    – Alex Kahn
    May 9, 2011 at 21:27
  • Project is no longer under development.
    – OrangeTux
    Nov 7, 2014 at 13:29
6

I've been very happy using node-lint

sudo npm -g install jslint

Then whack this somewhere in your .vim

set makeprg=jslint\ %
set errorformat=%-P%f,
        \%E%>\ #%n\ %m,%Z%.%#Line\ %l\\,\ Pos\ %c,
        \%-G%f\ is\ OK.,%-Q

Now a :make will run jslint. Errors appear in the quickfix window.

3

Here are the Mac OS instructions updated for Nov. 2012. Assumes you have Homebrew installed in order to get Node.js, and that you've already installed Syntastic for Vim (I use https://github.com/carlhuda/janus which provides this automatically):

$ brew install node.js
$ npm install -g jshint

Then add '/usr/local/share/npm/bin' to your PATH (probably in ~/.bashrc). For example, add the line: export PATH="$PATH:/usr/local/share/npm/bin"

restart your Terminal and check that

$ jshint

is executable from the command line. Syntastic will discover jsHint automatically. Restart MacVim and enjoy!

2

Much better is to pipe the results through Lynx to deal with JSLint's unfortunate choice of HTML for output format. I have a blog post on how to do it here:

http://www.fleegix.org/articles/2008-09-06-jslint-in-vim-through-lynx

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.