Syntax checking: Syntastic
It’s built-in dummy!
A nice thing about an IDE is that it offers visual clues when editing source code files. It’s quite hard to not notice a syntax error in an IDE. The astute Vim adept will immediately tell you syntax highlighting is built-in into Vim. No need for a plugin. And he’s right, Vim comes with a built-in syntax highlighter and even indentation control. You can enable those by adding the following settings to your
1 2 3 4 5 6
More on indentation
Indentation can be controlled very granuallary. Here’s an overview of the most commonly used settings:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
If you want more information on indentation and how to control it, I recommend reading Secrets of tabs in Vim.
What we’re after is true language specific syntax checking. For this, we do need a plugin. Enter Syntastic, created by Martin Grenfell, author of NERDTree. What it does, is running your files through external syntax checkers. It then returns those errors and displays them to you. There are syntax checking plugins for a whole bunch of programming languages. Most important for me, is PHP support.
When you write a file and it has syntax errors in it, Syntastic notifies you in a couple of ways:
- A mark next to the line where your syntax error is.
- When hovering over that marker or line, a balloon with the error messages comes up.
- A notification in the status bar. (Never mind that fancy status bar in the screenshot, I’ll talk about it some other time.)
Quite easy with pathogen:
cd ~/.vim/bundle git clone https://github.com/scrooloose/syntastic.git
Restart Vim and you’re all set.
I’m sorry, everything works out-of-the-box. So there’s no configuration necessary.
I’m a PHP guy, and code quality is important to me. Luckily, Syntastic can assist me. There is support for tools like PHPCS and PHPMD. To add these to Syntastic, add this to your
~/.vimrc configuration file:
Syntastic will first use php lint. If no errors were found, the file is checked with PHPCS for coding standards fails. If there still are no errors, it uses PHPMD for mess detection. If you need help setting up these extra checkers, see
:help syntastic-checker-options. Basically, you can override all the arguments for each checker by specifying the arguments in this standardized way:
let g:syntastic_<filetype>_<subchecker>_args="<cli args>". Very neat to specify e.g. the coding standards you want your file to adhere to.
Syntastic comes well configured out-of-the-box. That’s what makes it so easy to use. Once it’s installed, you can just forget about it. If for some reason, you need a checker that’s not available, it should be fairly easy to create your own. Even without much of VimL knowledge. Just read the project wiki and write your missing checker. Don’t forget to share it with the rest of use, right?
Update 22/11/2013 – Corrected some factual errors; Added indentation stuff.
Vim For IDE-people
This article is part of the series “Vim For IDE-people”. The other articles in the list are: