VIM for IDE-people - part 4

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 ~/.vimrc file:

1
2
3
4
5
6
" Takes care of highlight
filetype plugin on
syntax on

" Takes care of indentation
filetype indent on

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
" Set an indent to 4 columns
set tabstop=4

" When reindenting, each indentation is 4 columns
set shiftwidth=4

" Don't replace an indentation by 4 spaces
set noexpandtab

" Do replace an indentation by 4 spaces
"set expandtab

" Use 4 columns when hitting Tab in insert mode
set softtabstop=4

If you want more information on indentation and how to control it, I recommend reading Secrets of tabs in Vim.

Syntastic

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:

  1. A mark next to the line where your syntax error is.
  2. When hovering over that marker or line, a balloon with the error messages comes up.
  3. A notification in the status bar. (Never mind that fancy status bar in the screenshot, I’ll talk about it some other time.)

Installation

Quite easy with pathogen:

cd ~/.vim/bundle
git clone https://github.com/scrooloose/syntastic.git

Restart Vim and you’re all set.

Configuration

I’m sorry, everything works out-of-the-box. So there’s no configuration necessary.

PHP Checkers

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:

1
let g:syntastic_php_checkers=['php', 'phpcs', 'phpmd']

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.

Final words

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:

  1. Basic Vim configuration and plugins
  2. Solarized color scheme
  3. File system explorer: NERDTree
  4. Syntax checking: Syntastic
  5. Lightweight status line: vim-airline

Comments