Perl-enabled Vim tricks

Monday, 17 May 2004

In Vim with Perl support compiled in, the following little snippet (on a single line!) will convert a decimal number under the cursor into hexadecimal:

:perl my ($y, $x) = $::curwin->Cursor(); $_ = $::curbuf->Get($y);
s/^(.{1,$x})(\d+)/sprintf '%s%x', $1, $2/e && $::curbuf->Set($y, $_)

It’s not pretty at all, and somewhat buggy – but it can serve as a basis for a whole range of solutions. A first stab at fixing it would probably involve something like (?(?{ pos() < $x })(?!)), but I need to recompile my Vim before I can test that.