Posts Tagged ‘ VIM

IndentationError: unexpected indent

Tabs and spaces are code killers, visually and syntactically. Dealing with Python, you might see the error in the title often when sharing code with others. Focusing on VIM, people love to setup their rcfile so they get the most out of what VIM has to offer, but also makes their code easy to write and easy to read. Unfortunately, many times it’s just easy to read for them and no one else.

Python is very picky about spacing and indentation since it’s used to determine the flow of your script or program. Miss a tab or series of spaces somewhere and all the sudden you are executing code that’s not inside the if statement you just made, or you’ll get errors like above because your code just flat out fails.

When dealing with Git, and having several people committing code to a similar script or project, you are bound to run into situations where tabstops and shiftwidths are in conflict. It may look like all of the code lines up visually, but where one person uses spaces, another may have used actual tabs. Then you notice they made over 100 changes to the code you’re working on, and there’s tabs and spaces mixed all over the place. You have the following in your .vimrc:

set expandtab
set tabstop=4
set shiftwidth=4

It would be great to fix all of the tabs and spaces to be the same. Now what? VIM to the rescue! Bare with me, this is a bit complicated. Once you open the file in VIM, execute the following:

:%retab

You’re done, by the way. This uses your settings, and converts all of the tabs to the settings in your .vimrc. Now your entire file is standardized as far as your spacing goes and you can say goodbye to your unexpected indent errors. If you find yourself having to do this often, you can bind a key to automagically make the changes for you by adding this into your .vimrc (using F2 in this example):

map <F2> :retab <CR> :wq! <CR>

This will convert all of the tabs to spaces and save the file for you. Enjoy!

VIM Syntax Highlighting For Varnish VCL Files

Varnish has become a popular topic of conversation lately, and rightly so. It is amazing. The Varnish-cache project describes it as simply a high-performance HTTP Accelerator. Outside of its use as a caching layer, it is also an excellent load balancer and proxy.

One of the great things about Varnish is the Varnish Configuration Language. VCL files define the policies for Varnish when handling requests and for caching and are written in a syntax similar to C and Perl. What makes this so amazing is the following:

When a new configuration is loaded, the varnishd management process translates the VCL code to C and compiles it to a shared object which is then dynamically linked into the server process.

This just blows my mind. Talk about going the extra mile to squeeze out as much performance as you can from your application. It makes the VCL syntax much easier to understand when you can think about it in these terms and know what the application is going to do with it.

I’ve been sifting through VCL files in my free time to accumulate a list of nifty tricks that I might be interesting in using in the future. VIM being my editor of choice, I hunted for a syntax file that would help when reading VCL files. I ran across one written by Elan Ruusamäe and it was exactly what I needed. Download the latest version and put it in your ~/.vim/syntax directory. Then make the following additions to your .vimrc:

au BufRead,BufNewFile *.vcl :set ft=vcl
au! Syntax vcl source ~/.vim/syntax/vcl.vim

Now I can enjoy syntax highlighting when ripping through example code like I would most other things I edit with VIM.