Instantly share code, notes, and snippets.

Embed
What would you like to do?

The Power of Vim

Today's episode: Compile/build/error check workflow. The Vim Way™

Introduction

The number one plugin that everyone and their relatives installs, without a second thought, if they want to check for errors in their script/language of choice, is syntastic. In my opinion, syntastic is a completely useless plugin. "HUH WTF?! How dare you insult my favorite plugin?!" Yes, that's right. I said it. It's bloated, has way too many problems, not to mention, it causes problems to people who install it, and most importantly, it's superfluous. Think netrw vs NERDtree, only more simplistic. A lot of newcomers to Vim install NERDtree without a second thought, mostly because they don't know that netrw exists and comes built in to Vim.

So why is syntastic superfluous? Well because you can check for errors in Vim. You can run scripts/files in Vim. You can even kick off a build in Vim, all without installing a single plugin. "Whoa, what?" Yes, that's what I said. Welcome to Vim. Welcome to the Power of Vim!

Simply Making It

If your project has a Makefile, guess what? You can call it right inside Vim without installing anything. Simply execute :make and Vim will run the Makefile. The variable that controls this is called &makeprg. Simply set the makeprg to whatever you like. For instance, if you want to compile the current file with javac: set makeprg=javac\ %. :make will now execute javac on the current file. :set makeprg=ruby\ -c\ % will check the syntax of your ruby file. See :help 'makeprg' in Vim for more detailed explanations.

You can also set the appropriate makeprg in a compiler file such as ~/.vim/after/ftplugin/c.vim for a c filetype, for instance.

Want error checking on save like syntastic? Simple, add a BufWritePost hook. Here's an example that runs :make if you save a c, c++ or a python file:

autocmd BufWritePost *.c,*.cpp,*.py make

"But wait", you're thinking, "syntastic gives me errors! I can see all my errors in a little list! Vim with no syntastic is too primitive!!".

Quick, Fix it!

Introducing the QuickFix list. Where all your problems--er--errors can be found!

Look at it, it looks majestic. And guess what, all this is done without a single plugin.

qflist

The QuickFix list provides very handy mappings to jump to the current, next or previous error. :cc jumps to the current highlighted error, :cnext (or just :cn) jumps you to the next error in the list, :cprevious (or just :cN jumps you to the previous error. You can open or close the quickfix list with :copen or :cclose respectively.

You can also have the quickfix list come up automatically after a :make by putting the following in your ~/.vimrc:

autocmd QuickFixCmdPost * copen

"How am I getting this awesomely awesome sauce without a plugin?!" you surely ask.

Format My Error

Vim provides a option called &errorformat (:help 'errorformat'). It's a useful setting that controls the format of the error descriptions using a scanf-like format. You can format it to the language of choice, and the quickfix list will render it like you see in the screenshot above. You can choose to put it in a FileType autocmd in your ~/.vimrc, or better, put the setting in an appropriate compiler file. For instance, I put a python centric &errorformat in ~/.vim/ftplugin/python.vim. For a swift centric &errorformat, I set it in ~/.vim/ftplugin/swift.vim.

Typically, compiler files that ship with Vim will set an errorformat, but if not, you can easily create it using information from :help errorformat or, more likely, someone has already created one. The help file even has several examples for Java, ant, jade, perl. You could also use a compiler file found on vim.org. Compiler files typically set both the &makeprg and &errorformat.

And of course, Vim ships with compiler files for a lot of languages already, so it's likely that you won't have to do anything. It Just Works™.

Grand Central Dispatch

No, not that dispatch, this one by Tim Pope. You're a fan of plugins you say?! Well this is a great and useful one that actually complements Vim's native way rather than duplicating lot of existing functionality. It's the only plugin I use to kick off builds. Sometimes I like them to be asynchronous. The good news is, Dispatch can handle your &makeprg setting just fine or &errorformat, so :Dispatch will run your set &makeprg in the background and then put the results in the quickfix list after the command finished. It runs in the background so you can continue working in Vim. See :help vim-dispatch for more.

Finally

As proven above, Vim can do a lot without plugins. In fact, exploring what Vim has to offer goes a long way, and you further improve your Vim skills. We've only changed a couple of settings to get Vim to check for errors for us or build our file. Now when you look at syntastic and its hundreds of lines of code to acheive the same thing, you really have to ask, what is it really doing for you that Vim doesn't already (besides adding more than 10 ms to Vim's startup time)?

@Konfekt

This comment has been minimized.

Konfekt commented Feb 3, 2015

Thanks for sharing this!

What if someone uses already a makeprg to compile the source and wants to use a syntax checker ? The first thing that came to my mind was temporarily changing it by

  let oldEfm = &l:errorformat
  let oldMp = &l:makeprg

  setlocal errorformat="%f",\ line\ %l:\ %m
  setlocal makeprg=compile

  silent lmake %

  let &l:errorformat = oldEfm
  let &l:makeprg = oldMp

This is a hack. What's the right way to do this?

@dahu

This comment has been minimized.

@Konfekt

This comment has been minimized.

Konfekt commented Feb 9, 2015

Thank you!

I hope I got this right at lervag/vimtex#114

Now how to revert to the previous makeprg and erroformat settings after compiler lint say?

@mtglsk

This comment has been minimized.

mtglsk commented Mar 23, 2016

Syntastic does more than this. For example it can contantenate multiple checkers, gracefully adjust to available checkers, uses location list, has signs, has balloon tooltips, has statusline tooltips, works with a lot of different filetypes ootb. Also opening a tmux splits with dispatch on every write? ke?

Writing errorformats is painful and error prone, so it's a good idea to use a plugin as actively developed as syntastic.

@ohcibi

This comment has been minimized.

ohcibi commented Aug 15, 2016

I agree with @mtglsk. Vims builtin feature maybe powerful but this article is crap. I just install syntastic and the needed syntax-checkers and I'm ready to go. Not even a need to define a keybinding.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment