开发者的实用 Vim 插件（二）
插件 Syntastic Vim
毫无疑问，Vim 是一个开箱即用并能够胜任编程任务的编辑器，但实际上是该编辑器中的插件帮你实现这些方便的功能。在 [开发者的实用 Vim 插件（一）](/article-7901-1.html)，我们已经讨论两个编程相关的 Vim 插件——标签侧边栏（Tagbar）和定界符自动补齐（delimitMate）。作为相同系列，我们在本文讨论另一个非常有用、专门为软件开发正定制的插件——语法高亮插件。


![](/data/attachment/album/201610/28/163904kpp139uefxxm1xbx.jpg)


请注意：本教程中列举的所有例示、命令和说明都是在 Ubuntu 16.04 环境下进行测试的，并且，我们使用的 Vim 版本是 7.4。


### 语法高亮（Syntastic）插件


假如你的软件开发工作涉及到 C/C++ 语言，毫无疑问的说，遇到编译错误也是你每天工作中的一部分。很多时候，编译错误是由源代码之中的语法不正确造成的，因为开发者在浏览源码的时候很少能够一眼就看出所有这些错误。


那么 Vim 中是否存在一种插件可以让你不经编译源码就可以显示出语法错误呢？当然是有这样一种插件的，其名字就是 Syntastic。


“Syntastic 是 Vim 用来检验语法的插件，通过外部语法校验器校验文件并将错误呈现给用户。该过程可以在需要时进行，或者在文件保存的时候自动进行。”该插件 [官方文档](https://github.com/scrooloose/syntastic) 如是说。“如果检测到语法错误就会提示用户，因为不用编译代码或者执行脚本就可以知道语法错误，用户也就乐享与此了。”


安装过程和第一部分提到的方法类似，你只需要运行下列命令即可：



```
cd ~/.vim/bundle/

git clone https://github.com/scrooloose/syntastic.git

```

一旦你成功安装这个插件（即上述命令执行成功），你就不需要进行任何配置了——当 Vim 启动时会自动加载这个插件。


现在，打开一个源码文件并用 `:w` Vim 命令保存即可使用这个插件了。等待片刻之后，如果在源码中有语法错误的好，就会高亮显示出来。比如，看看一下截图你就会明白该插件是如何高亮显示语法错误的：


![Vim Syntax error highlighting](/data/attachment/album/201610/28/163933wdt2t35qoq50l2r2.png)


在每行之前的 `>>` 表示该行中有语法错误。了解确切的错误或者想知道是什么东西错了，将光标移到该行——错误描述就会展示在 Vim 窗口的最底下。


![View Syntax errors in Vim](/data/attachment/album/201610/28/163934o9gu0urhpgulxfi0.png)


这样，不用进行编译你就能够修复大多数语法相关的错误。


再往下，如果你运行 `:Errors` 命令，就会展现当前源文件中所有语法相关错误的描述。比如，我运行 `:Errors` 命令就是下图的效果：


![Syntastic :Errors command](/data/attachment/album/201610/28/163934ci5vvn3vomh9nnzb.png)


请记住，`:Errors` 展现的语法错误是不会自动更新的，这意味着在你修复错误之后，你需要重新运行 `:Errors` 命令，编辑器底部的错误描述才会消除。


值得一提的是，还有 [许多配置选项](https://github.com/scrooloose/syntastic/blob/master/doc/syntastic.txt) 能够使得 Syntastic 插件使用起来更加友好。比如，你可以在你的 `.vimrc` 中添加下列内容，然后 `:Errors` 就可以在修复错误之后自动更新它的底部描述。



```
let g:syntastic_always_populate_loc_list = 1

```

添加以下内容，以确保在你打开文件时 Syntastic 插件自动高亮显示错误。



```
let g:syntastic_check_on_open = 1

```

类似的，你也可以在保存或打开文件时让光标跳转到检测到的第一个问题处，将下列行放到你的 `.vimrc` 文件之中：



```
let g:syntastic_auto_jump = 1

```

这个值也可以指定为其它两个值： 2 和 3，其官方文档的解释如下：


“如果设置为 2 的话，光标就会跳到检测到的第一个问题，当然，只有这个问题是一个错误的时候才跳转；设置为 3 的话，如果存在错误，则会跳到第一个错误。所有检测到的问题都会有警告，但光标不会跳转。”


以下信息可能对你有帮助：


“使用 `:SyntasticCheck` 来手动检测错误。使用 `:Errors` 打开错误位置列表并使用 `:lclose` 来关闭。使用 `:SyntasticReset` 可以清除掉错误列表，使用 `:SyntasticToggleMode` 来切换激活（在写到 buffer 时检测）和被动（即手动检测）检测错误。”


注意：Syntastic 并不局限于 C/C++ 所写的代码，它同时也支持很多的编程语言——点击 [此处](https://github.com/scrooloose/syntastic) 了解更多相关信息。


### 结论


毫无疑问的，Syntastic 是一个非常有用的 Vim 插件，因为在出现语法相关错误时候，它至少能够让免去频繁编译的麻烦，而且不用说，同时也节约了你不少的时间。


正如你所看到的一样，配置好几个主要选项之后，Syntastic 变得非常好用了。为了帮助你了解这些设置，官方文档中包含了一份“推荐设置”——跟着文档进行设置即可。加入你遇到一些错误、有些疑问或者问题，你也可以查询一下 FAQ。




---


via: <https://www.howtoforge.com/tutorial/vim-editor-plugins-for-software-developers-2-syntastic/>


作者：[Ansh](https://www.howtoforge.com/tutorial/vim-editor-plugins-for-software-developers-2-syntastic/) 译者：[GHLandy](https://github.com/GHLandy) 校对：[wxy](https://github.com/wxy)


本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译，[Linux中国](https://linux.cn/) 荣誉推出


OK
# Useful Vim editor plugins for software developers - part 2: Syntastic

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](https://www.howtoforge.com/tutorial/vim-editor-plugins-for-software-developers/) 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.

## Syntastic

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](http://www.vim.org/) 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](https://github.com/scrooloose/syntastic) 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:

cd ~/.vim/bundle/

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](https://github.com/scrooloose/syntastic/blob/master/doc/syntastic.txt) 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](https://github.com/scrooloose/syntastic) to learn more about it.

## Conclusion


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.