Using Vim with JavaScript, JSLint and Taglist
Thursday, June 16, 2011
Two awesome tools, an awesome language, and JSlint... ;) Unfortuantely, it's not straightforward to get them to work together. This is my configuration. I'm going to assume you've installed taglist in VIM.
In ~/.vimrc, put the following:
let g:tlist_javascript_settings = 'javascript;r:var;s:string;a:array;o:object;u:function'
In ~/.ctags, put the following:
--regex-JavaScript=/^var[ \t]+([a-zA-Z0-9_$]+) = \[/\1/a,array/ --regex-JavaScript=/^var[ \t]+([a-zA-Z0-9_$]+) = \{/\1/o,object/ --regex-JavaScript=/^var[ \t]+([a-zA-Z0-9_$]+) = (^{^[)+/\1/r,var/ --regex-JavaScript=/^[ \t]*(this\.)?([A-Za-z0-9_$()]+)[ \t]*[:=][ \t]*function[ \t]*\(\)/\2/u,function/ --regex-JavaScript=/^[ \t]*function ([a-z0-9]+[A-Za-z0-9_]*)/\1/u,function/ --regex-JavaScript=/^[ \t]*([A-Za-z0-9]+)\.prototype\.([a-z0-9]+[A-Za-z0-9_]*)/\1 : \2/u,function/ --regex-JavaScript=/^[ \t]*function ([A-Z]+[A-Za-z0-9_]*)/\1/o,object/Note: This does a pretty good job but still misses out on some things. Javascript has surprisingly complicated declarations. I'm working to improve it.
For JSLint, I assume you downloaded some version of jslint that you can run with rhino or whatever. In ~/.vimrc, put the following:
:map <F7> :w<CR>:!rm OUT_TEMP<CR>:!rhino jslint.js > OUT_TEMP<CR>:split OUT_TEMP<CR>When you press F7, this runs jslint, dumps it into a temporary file, and then opens it up in a new buffer. Then you can scroll through and have your feelings hurt. You can tweak the !rhino line to use node or whatever you cool kids are doing these days.
A cute extra: Running taglist on startup.
You can't put :TlistToggle
in your .vimrc, because Taglist hasn't loaded yet, so the command is unrecognized. The fix is to do this instead:
autocmd VimEnter *.js TlistToggle
You should follow me on twitter here.