There’s no doubt that Vim is a capable programming editor out-of-the-box, but it’s the editor’s plugins that help you make the most out of it. In the first part of this article series, we discussed a couple of programing-related Vim plugins (Tagbar and delimitMate). Continuing on the same path, in this article, we will discuss another useful Vim plugin aimed at software developers – Syntastic.
Please note that all the examples, commands, and instructions mentioned in this tutorial have been tested on Ubuntu 16.04, and the Vim version we’ve used is 7.4.
If your software development work involves working with languages like C and C++, it’s needless to say that resolving compile time errors would be part of your daily work. Many a times, compilation errors arise due to incorrect syntax used in source code, as developers fail to observe them while just looking at the code.
What if I tell you there exists a Vim plugin that provides information about syntax errors without you having to compile your code? Yes, such a plugin exists, and it’s name is Syntastic.
“Syntastic is a syntax checking plugin for Vim that runs files through external syntax checkers and displays any resulting errors to the user. This can be done on demand, or automatically as files are saved,” the official documentation of the plugin says. “If syntax errors are detected, the user is notified and is happy because they didn’t have to compile their code or execute their script to find them.”
Installation of this plugin is similar to the way we installed the ones that were discussed in the first part – all you need to do is to run the following commands:
git clone https://github.com/scrooloose/syntastic.git
Once the plugin is installed successfully (meaning the above commands are successful), you don’t have to do anything else – it loads automatically when the Vim editor is launched.
Now, to use the plugin, open a source code file and save it using the :w Vim command. After a very brief pause, you’ll observe that syntax errors – if any – in your code will be highlighted. For example, the following screenshot should give you an idea about how the plugin highlights the errors:
The ‘>>’ sign in the beginning of a line indicates that there’s some problem with the code written in that line. To know what exactly the error is, or at-least get a rough idea about it, bring the cursor to that line – the error description will be displayed at the bottom of the Vim window.
So this way, you can resolve most of the syntax-related errors without even compiling the code.
Moving on, if you run the :Errors command, description of all the syntax-related errors in the current source file will be displayed. For example, running the :Errors command in my case brought up the following information:
Keep in mind that the information the :Errors command displays isn’t auto-refreshed, meaning even after an error is resolved, it’s description will be there at the bottom area until you run the :Errors command again.
It’s worth mentioning that there are many configuration options that make Syntastic even more user-friendly. For example, you can put the following line in your .vimrc file if you want the :Errors command to automatically update its output each time an error is resolved:
let g:syntastic_always_populate_loc_list = 1
Another major limitation that the default Syntastic behaviour has is that you have to run the :w command in order to get the plugin to indicate erroneous lines – this is ok while editing a file, but gets a bit weird when you’ve just opened a source file.
To make sure that Syntastic highlights the errors automatically when a file is opened, add the following line to your .vimrc file:
let g:syntastic_check_on_open = 1
Similarly, if you want the cursor to jump to the first detected issue when saving or opening a file, then put the following line in your .vimrc file:
let g:syntastic_auto_jump = 1
There are two other values that you can assign to the aforementioned variable: 2 and 3. Here is what the official documentation says about these two values:
“When set to 2 the cursor will jump to the first issue detected, but only if this issue is an error,” and “when set to 3 the cursor will jump to the first error detected, if any. If all issues detected are warnings, the cursor won’t jump.”
The following information should also be useful to you:
“Use :SyntasticCheck to manually check” for errors. “Use :Errors to open the location-list window, and :lclose to close it. You can clear the error list with :SyntasticReset, and you can use :SyntasticToggleMode to switch between active (checking on writing the buffer) and passive (manual) checking.”
Note: Syntastic isn’t just limited to code written in C and C++. It supports a huge list of other programming languages as well – head here to learn more about it.
Undoubtedly, Syntastic is a very useful Vim plugin as it saves you from the hassle of frequent compilation at-least when it comes to syntax-related errors. And not to mention that a lot of your time gets saved as well.
As you would have observed, Syntastic becomes even more useful once you’ve configured some of its main options. To help you get started with this, the official documentation contains a ‘Recommended settings’ section – do go through that. There is a nice little FAQ section as well in case you face some errors or have some doubts or queries.