vim tricks for perl

I’ve been making use of some helpful vim snippets posted on for a little while now and I thought they deserved a quick mention.  Until I started writing this blog post I hadn’t realised they were actually be the same author.  Marcel Grünauer has been writing some of the most interesting blog posts on the new(ish) perl blog site from my point of view.  The series on vim tricks is here.The first script that adds use statements makes use of expand('<cword>') which out of the box doesn’t pick up the whole package name, stopping at the :: instead.  The script makes a note that this can be ‘fixed’ by changing the iskeyword setting.  One of the commenters luckily makes a patch to make that change while also localising the effect, so that it’s only done for that call, rather than requiring you to fundamentally change your vims behaviour.

I only read that comment when I was constructing this blog post though and so I had a play at vim scripting myself.  Instead of using expand I used matchstr and gave it a hand coded regex to find from the cursor to the end of the package name.

    let p = matchstr(line, '[A-Za-z_0-9:]\+', col('.') - 1)
    let s:package = input('Package? ', p)

Having read the alternative fix for that I think the expand('<cword>') is still the better way to do it, but it at least allowed me to finally do a tiny bit of vim scripting.  As it happens I also noticed a minor bug in the use statement script that causes issues when you try to add a use statement for a package named something that is a subset of an existing use statement.  i.e. you have use A::B::C; and you try to add a use statement for A::B.

This can be fixed simply by adding a little to the end of the regex that checks the use statement doesn’t already exist,

   if (search('^use\s\+'.s:package.'[^A-Za-z_0-9:]', 'bnw') == 0)

I also modified my version of the script to use <Leader> instead of , as the command character, so you type \us instead of ,us to execute the command.  That makes it more inline with his second script.

I’ve stuck my vim config onto github, as much as a backup as to share it.  It’s not really very sophisticated.

Now I’ve started to get more into using this vim scripting I really ought to go back and take a look at some of Ovids past blogs to see what useful things I can find there.  He has tended to blog interesting editor snippets.

Have a look at Marcel’s blog I realised he also blogged another really interesting thing, the dip script.  In some ways I’m impressed he’s managed to demonstrate it so succinctly here, but I also can’t help but think he ought to be shouting about it more because the potential of that is freaking awesome.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s