Tuesday, April 9, 2013

Vim and Syntastic



Since the days of writing everything from a "Hello World" app to a ray tracer in college, I've used the vim editor. Of late, I've had several projects touching apps that run on our Linux machines and, having read about the great power and extensibility of vim, it seemed like a great time to dig in and make myself more productive with it. (Here is a good argument for easing yourself into vim instead of diving headfirst into the hard stuff.)

Over the years, I had already started collecting a small set of settings for vim. I thought it'd be a great idea to make that set of settings public so that they might be of use to someone else (or so somebody can clue me in to a better way to do things). So here they are.

Of course, wanting to be more productive, I started hunting around the web for ideas on how to use vim better. In the process, I stumbled across this entry by Ian Langworth about how he uses vim and the plugins that he uses. Given that we had just turned on static analysis on some of our code, Syntastic stood out to me as a great idea.

               ,
              / \,,_  .'|
           ,{{| /}}}}/_.'            _____________________________________________
          }}}}` '{{'  '.            /                                             \
        {{{{{    _   ;, \          /            Ladies and Gentlemen,              \
     ,}}}}}}    /o`\  ` ;)        |                                                |
    {{{{{{   /           (        |                 this is ...                    |
    }}}}}}   |            \       |                                                |
   {{{{{{{{   \            \      |                                                |
   }}}}}}}}}   '.__      _  |     |    _____             __             __  _      |
   {{{{{{{{       /`._  (_\ /     |   / ___/__  ______  / /_____ ______/ /_(_)____ |
    }}}}}}'      |    //___/   --=:   \__ \/ / / / __ \/ __/ __ `/ ___/ __/ / ___/ |
jgs `{{{{`       |     '--'       |  ___/ / /_/ / / / / /_/ /_/ (__  ) /_/ / /__   |
     }}}`                         | /____/\__, /_/ /_/\__/\__,_/____/\__/_/\___/   |
                                  |      /____/                                    |
                                  |                                               /
                                   \_____________________________________________/
Simply saving the file I'm working on calls the appropriate compiler and instructs it to run a syntax check on the file. From there it visually highlights the lines with errors and, when the cursor is moved to that line, it displays the error at that line. Incidentally, it can also load the errors into the location list by running `:Errors`.

Of course, it took a little tweaking to get exactly right, for several reasons:

  1. Using the compiler to compile a single C file doesn't make much sense if it's part of a larger project. You'll get a lot of errors that are non-sensical because the appropriate headers aren't loaded, among other things. Running make makes a lot more sense. I updated the make command called by make.vim (located in the c syntax checker folder) so that it would call the appropriate target with the syntax checking flag turned on.
  2. We're using a different compiler than is assumed by Syntastic, so I came up with an appropriate error format that would parse the errors correctly. The discussion of this is here.

Syntastic is now providing me with immediate feedback to changes I make which is critically important to being productive. The sooner I find out something is wrong, the sooner I can fix it. Now, on to getting more done.

No comments:

Post a Comment