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.

vvvvv
  • 25,404
  • 19
  • 49
  • 81
ben lemasurier
  • 2,582
  • 4
  • 22
  • 37

6 Answers6

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! :)

Glorfindel
  • 21,988
  • 13
  • 81
  • 109
Ory Band
  • 14,716
  • 14
  • 59
  • 66
  • 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.. – Claes Mogren May 27 '11 at 22:08
  • 2
    Ok, noticed that Syntastic requires ballon_eval, wich isn't supported in MacVim. Too bad.. – Claes Mogren May 28 '11 at 19:43
  • @Claes Mogren: I guess somebody should get involved in the [Syntatic GitHub repo](https://github.com/sjl/syntastic) and add automatic `jshint` detection, in order to skip the step where you have to overwrite Syntastic's `javascript.vim` with our own custom one. – Ory Band May 30 '11 at 00:34
  • I agree, might give it a try myself. :-) – Claes Mogren May 30 '11 at 09:30
  • 1
    Tried adding it, hope it (or a prettier version of it) gets pulled https://github.com/scrooloose/syntastic/pull/47 – Claes Mogren May 30 '11 at 20:02
  • @Claes Mogren: Wow, that was quick. Way to go man! :) Now this tool is even more helpful. – Ory Band May 31 '11 at 18:41
  • My patch was pulled by scrooloose yesterday so you might want to update the answer. (And please try that it works :-)) – Claes Mogren Jun 03 '11 at 12:30
  • Just found this; I've followed the instructions, and can verify that Syntastic is installed, but nothing happens on my .js files... Can someone explain how to enable it? – Matt Feifarek Nov 16 '11 at 02:02
  • @MattFeifarek - You're welcome to consult my .vimrc file for help. There're more than one cause for this to not work on your machine: https://github.com/oryband/dotvim/blob/master/vimrc – Ory Band Dec 03 '11 at 18:42
  • do you have any advice on debugging this configuration. I've installed all these things, but I'm not sure what is wrong or how to use. – jrwren Mar 13 '12 at 19:46
  • 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 '12 at 20:03
  • @ThePants Heh, guess I should update it. :) Thanks for the notice. Guess we can never evade typos. – Ory Band Apr 28 '12 at 14:15
  • @ThePants Just edited the answer and added the developer's repository updated configuration example. They pulled from me originally, but have kept updating it since then. Enjoy! – Ory Band Apr 28 '12 at 14:22
  • 1
    jsl for Windows: http://www.javascriptlint.com/download.htm also, watch out in the jsl.vim file for this issue: http://vim.1045645.n5.nabble.com/shellescape-doesn-t-work-in-Windows-with-shellslash-set-td1211618.html – Andrew Jun 05 '12 at 22:08
  • Already had syntastic and node/npm installed, and this worked perfectly first try. Should be the accepted answer. – Matthew Turner Dec 19 '13 at 19:33
  • 1
    Syntasic is no longer supported :( – Joe Sep 21 '22 at 15:07
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.

f3lix
  • 29,500
  • 10
  • 66
  • 86
  • Why are you catting the buffer and passing it as an argument to mylintrun? – jamessan Feb 11 '10 at 02: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 '10 at 10:37
  • what is 'the path to SpiderMonkey' on your machine - I've got a few on mine – Dr. Frankenstein May 18 '10 at 11:57
  • I can't get at mylintrun.js, is there an alternative location. it appears to be dead – bluekeys Mar 20 '12 at 15:45
  • @dsjbirch: try this https://github.com/anatoliychakkaev/.vim/blob/master/mylintrun.js – f3lix Mar 22 '12 at 15:38
  • I've opted for another approach using node I'll outline in a response – bluekeys Mar 24 '12 at 06:10
  • Now there is `jslint.vim` available at [github.com/hallettj/jslint.vim](https://github.com/hallettj/jslint.vim/). Even though it's in alpha, it's pretty stable for me. – gregoltsov Jul 20 '12 at 09:10
  • 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 '12 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!

Styx
  • 9,863
  • 8
  • 43
  • 53
Alex Kahn
  • 537
  • 5
  • 10
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.

bluekeys
  • 2,217
  • 1
  • 22
  • 30
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!

Glorfindel
  • 21,988
  • 13
  • 81
  • 109
Shaun Dychko
  • 825
  • 1
  • 9
  • 11
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

mde
  • 196
  • 1
  • 4