Tiny vim convenience hack

Since I maintain a lot of code that wasn't originally written by me, I am in the unfortunate situation that I get to edit code with many different coding and indentation styles on a regular basis. Reformatting usually isn't a good idea since not only may the original author object, but the revision control history is more important than code and indentation style.

I'm a vi(m) user. Modifying the settings to suit whatever insane tab-compression-indentation scheme some crazy emacs user decided on for individual documents is severely annoying. My editor should do this for me. If I was using Padre for my daily work, it would do this for me automatically. Today, finally, I sat down and wrangled vi until it would auto-detect the indentation style of the current document. More precisely, Text::FindIndent does (version 0.09 was just released and is required). Install the module, include this in your .vimrc on one line:


map <F5> <Esc> :perl use Text::FindIndent;VIM::DoCommand($_) for Text::FindIndent->to_vim_commands(join "\n", $curbuf->Get(1..$curbuf->Count()));<CR>

Now, open up somebody else's source code that uses some crazy indentation scheme. Hit F5. Your vi settings have now been marvelously modified to produce the same broken indentation as whatever you opened for editing.

9 Comments

I'm a fan of this one:
:1,$!perltidy -pro="/home/myhome/.perltidyrc" -st

I don't think you understood what he is trying to do with this Vim hack.


"Your vi settings have now been marvelously modified to produce the same broken indentation as whatever you opened for editing."

Put this in your .vimrc instead:

perl << EOS
use Text::FindIndent;
sub FindIndent {
    my @cmd = do {
        my $doc = join "\n", $curbuf->Get( 1 .. $curbuf->Count );
        Text::FindIndent->to_vim_commands( $doc );
    };
    for ( @cmd ) {
        s{:set\b}{:setlocal};
        VIM::DoCommand $_;
    }
}
EOS
autocmd FileType perl :perl FindIndent()

No need to envy Padre just yet. :-)

The autocmd matches the filetype against a pattern so can mention any number of types, eg.:

autocmd FileType {perl,c,sh,make,xml,html} :perl FindIndent()

Unless you want this to affect all files, in which case you’d hook the BufNewFile and BufRead events (matching any filename) instead:

autocmd BufRead,BufNewFile * :perl FindIndent()

(Cf. Scripting the Vim editor, Part 5: Event-driven scripting and automation, Damian Conway)

I've upgraded my Padre on Strawberry from 0.56 to 0.70. The upgrade went smoothly, and took about 15 minutes via CPAN.pm. I think the previous time it took an hour, so that's cool. Also, memory consumption has dropped, as previously an empty Padre instance used 110MB of RAM, and now "only" 78MB... It's still ridiculously much, but it's definitely much better.

What is not cool is that now, when starting up the first instance of Padre, there is an empty "console" window. The icon is the blue butterfly. I can manually close it, and the editor keeps working...

Running "padre.exe" from the command line doesn't return until the editor is closed.

I don't know what changed, but Padre didn't do that in 0.56. (Or maybe it did, and I fixed it myself, but if that's the case, I don't remember how.) Windows XP, BTW.

It looks like logging in transferred me to a different blog than I was at before... Sorry about that. Now it looks like I can't delete it...

Leave a comment

About Steffen Mueller

user-pic Physicist turned software developer. Working on Infrastructure Development at Booking.com. Apparently the only person who thinks the perl internals aren't nearly as bad as people make them. See also: the Booking.com tech blog, CPAN.