Monday 30 April 2012

A Few Handy VIM Commands

I’ve been off work for the last few days with a horrible head cold and today was my last day off before returning tomorrow. I’ve spent all of today (slowly) working in vim and here’s a a few of the commands that I’ve found really useful.

J - (J)oin two lines together

Joining two lines together can be a pain if you have to move the cursor around and jump in and out of insert mode so if you have something like this …

                                                                  'CURRENCYCODE': currency  } 

It’s painfully easy to turn it into this with a single keystroke

                                                                  'CURRENCYCODE': currency} 

< Indentation

Python is fussy about it’s indentation and I’m very fussy about it. Everything has to be near perfect. I do copy and paste a fair bit of code from various sources outside of vim and it’s easy to end up with a mess of indentation. I think we can all agree that the below indentation is horrific.

                                                                              {'METHOD': 'SetExpressCheckout',                         'NOSHIPPING': 1,                                     'PAYMENTACTION': 'Sale',                                                 'RETURNURL': return_url,                                                             'CANCELURL': cancel_url,                                                                         'AMT': amount,                                                                                     'CURRENCYCODE': currency } 

Thankfully it’s very easy to re-indent all that code to something like below in a just a few keystrokes. You can indent one level in or out using shift + < or shift + > or if you can do multiple levels (in this case 5) by doing shift + 5< or shift + 5>

            {'METHOD': 'SetExpressCheckout',            'NOSHIPPING': 1,            'PAYMENTACTION': 'Sale',            'RETURNURL': return_url,            'CANCELURL': cancel_url,            'AMT': amount,            'CURRENCYCODE': currency } 

When I get full use of my head back tomorrow I’ll be diving right back into vim tutor and picking up where I left off. I’m also going to attempt to do all of my work in VIM tomorrow. After all, you can’t learn to swim by reading a book and I suspect the same rules apply to learning VIM well enough to be able to use it full time.

The code

In case you were wondering I’ve been working on creating a leaner version of the django-paypal-driver for a new checkout system I’m building for c&c.