Skip to content

Instantly share code, notes, and snippets.

@artanikin
Created June 2, 2020 19:35
Show Gist options
  • Save artanikin/45a4b5b2dc97263b7da667d5a59f9ca6 to your computer and use it in GitHub Desktop.
Save artanikin/45a4b5b2dc97263b7da667d5a59f9ca6 to your computer and use it in GitHub Desktop.

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)?

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