[1+1=2]

OneAndOneIs2

« Privacy & social media'cuz multiple steps into one is cool :) »

Thu, Feb 23, 2012

[Icon][Icon]Perls of Visdom

• Post categories: Omni, FOSS, Technology, Programming, Helpful

(That should be "Perls of Vimsdom" to be strictly accurate, but it doesn't work as a pun that way)

The last thing I did at work yesterday was to come up with a way of accessing information stored in a database via the Vim text editor.

The information in question was a Data::Dumper'd hash key used by our Catalyst server. We already had a way to view it via the website itself, but that's just a big text file. I wanted it in Vim: It would be syntax hilighted; I could fold away bits I didn't want to see; I could parenthesis-match; I could even diff two different hashes if I had Dumper sort the keys first.

Using Vim is just a better way of doing a lot of things. It's certainly better than viewing data on a web page. I'm even working on my own blogging platform in my occasional spare time, designed specifically to be used from the command line so I can write new posts using Vim.

When most people first sit down to use a vi, their abiding impression is how hard it is to use. The first time I ever used vi, I had to reboot my computer to get out of it. But if you can stick with it long enough to pick up some of the basics, you start to find out just how powerful and helpful it can be. But after even longer, you start to hit the limits of what Vim can do out of the box. At this point, it's time to start looking into Vimscript and plugins so you can add your own power as you need it.

Since I mostly use Vim to edit Perl these days, I thought I'd share a few of the things I've done to make it more helpful.

First off is really just a bit of sugar - It's easy enough to use perltidy from vim:
:from,to!perltidy

But it's a bit ugly. So I set Vim to work with:
:Tidy from to

For this, add to your .vimrc the following:
"Simple command to perltidy a range of lines
command -nargs=+ Tidy call TidyFunc(<f-args>)
fun! TidyFunc(arg1, arg2)
execute a:arg1","a:arg2"!perltidy"
endfun

This creates a new command, Tidy, that calls a function that accepts two arguments. Line numbers or marks should both be fine.

It's not going to make a huge difference to your day, but it's useful as a basic template to use to create any new command you might want that accepts arguments & does things with them. A couple of other examples you may find of interest are:
"Command to calculate & insert package name
command -nargs=0 PkgName call PkgName()
fun! PkgName()
let file = @%
let package = substitute(file, "^.*lib/", "\\1", "g")
let package = substitute(package, "/", "::", "g")
let package = substitute(package, "^::", "", "")
let package = substitute(package, ".pm$", "", "")
exec "normal i" package "\<Esc"
endfun

"View any PODs in current file
command -nargs=0 Pod call PodFunc()
fun! PodFunc()
let file = @%
exec "!perldoc " file
endfun

Next up, syntax hilighting. I know some people dislike it, but I can't bear looking at a screen of grey code. I find it far more readable to have everything nicely coloured. Problems can arise, though, when you like MOST of a hilighting theme, but want to change just one aspect. For example, here's a sample of perl code, using the default hilight scheme.

It has comments, it has PODs, it has code.. but it's almost entirely blue. Comments are a very slightly different shade of cyan to variables. This isn't helpful. So I tweaked the theme, and it now loks like this.

A subtle, but helpful, change. So how do you do it?

Firstly, you need to know the name of the thing you want to change the colour of. So hunt it down: Firstly, find the syntax definition file:
locate perl.vim

This'll find you a file like /etc/vim/syntax/perl.vim - open this in vim and you'll see all the things this file tells vim to hilight. perlConditional for if/elseif/unless, perlRepeat for do/while/until.. and so on.

In this case, we want to find the name for comments - a bit of searching for either # or 'comment' will find that it is, unsurprisingly, called "perlComment"

So now, we just want to change the colour used for perlComment: First, open up a perl file in vim, and use the command
:highlight perlComment ctermfg=whatever

You can try finding the various colour names by running locate rgb.txt but they don't all work. They're a good starting place though. If you're using gvim, you may need guifg instead of ctermfg - have a play until you find what works for you.

Once you find a command that works for you and sets the colour to the one you want, to make it permanent, open the file ~/.vim/after/syntax/perl.vim and simply put in a copy of the command, without the : at the start - so my file has the single line
highlight perlComment ctermfg=lightgreen

From now on, your vim should have your preferred syntax colour.

Finally, snippets can save you a lot of typing. We use snipMate. It comes with a few defaults, but it's most useful if you add your own - go into /etc/vim/snippets/perl.snippets and just add them. A few that we find useful for Catalyst, for example, are:
cdeb - expands to $c->log->debug();
cdln - expands to $c->log->debug(__LINE__);
cdump - expands to $c->log->debug(Dumper())

Simply typing cdeb<tab> gets you the Catalyst debug code. cdln gives you the same thing, only with the special variable __LINE__ - this is very handy when trying to do things like follow through a complex set of if-else's - instead of having to write a bunch of different debugs to identify which get run, simply look at the line-numbers output in the log. The nice thing about the Dumper snippet is that it's defined by:
snippet cdump
$c->log->debug(${2}Dumper(${1}));

So when you expand the snippet, your cursor is automatically placed in the Dumper() brackets, ready to be told what object to dump; and then another press of 'tab' will take you to the start of the debug() so you can add some extra text.

Anything you find yourself typing often is worth replacing with a snippet. Take a look at the 'cfor' expansion to see how flexible snippets can be - updating all variable names at once, for example.

There's a lot of things you can do to make Vim even more helpful - hopefully the examples above will have given you a few ideas!

2 comments

Paul Evans
Comment from: Paul Evans [Visitor]
You probably want to apply it to Comment, and not perlComment, so the same colours apply to all syntax types.

Also I find that comments stand out even more if you set a background colour as well. Mine are white-on-blue, against anything else in the language not having a background, so being the terminal default.
23/02/12 @ 16:11
oneandoneis2
Comment from: oneandoneis2 [Member] · http://geekblog.oneandoneis2.org/
So far, I've not found the default colour to be a problem with the other languages I program, so I've left them alone. 'tis a good point, tho!

White-on-blue would certainly make the comments stand out! I prefer my comments to be relatively unobtrusive tho - I think they'd be a bit too overpowering for my liking with a background colour. I'd actually forgotten about the possibility of setting a background colour, TBH - I'll have to see if there's anywhere that they'd be useful. Ta!
23/02/12 @ 17:33

Leave a comment


Your email address will not be revealed on this site.

Your URL will be displayed.
(Line breaks become <br />)
(Name, email & website)
(Allow users to contact you through a message form (your email will not be revealed.)
This is a captcha-picture. It is used to prevent mass-access by robots.
Please enter the characters from the image above. (case insensitive)
 

[Links][icon] My links

[Icon][Icon] I'm in the Perl newsletter again. I should try and write about some other language...
21/05/12

[Icon][Icon] Facebook Syndication Error
22/05/12

[Icon][Icon] I last listened to:
Johann Pachelbel - Canon in D major

[Icon][Icon] Most recent photo:
js.js

[Icon][Icon]About Me

[Icon][Icon]About this blog

[Icon][Icon]My LQ profile

[Icon][Icon]My /. profile

[Icon][Icon]My Wishlist

[Icon]MyCommerce

[FSF Associate Member]


May 2012
Mon Tue Wed Thu Fri Sat Sun
 << <   > >>
  1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30 31      

Search

User tools

XML Feeds

eXTReMe Tracker

Valid XHTML 1.0 Transitional

Valid CSS!

[Valid RSS feed]

powered by b2evolution