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.
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."
As Robert pointed out, my goals are indeed different from perltidy. I am explicitly trying to avoid reformatting since that would break at least version control systems of the last generation.
PS: Maybe I should have mentioned that Text::FindIndent was written specifically for this purpose in the context of Padre. Except Padre goes a bit further than my vi hack. It can automatically adjust settings whenever you switch between open documents. Very convenient.
Put this in your
.vimrc
instead:No need to envy Padre just yet. :-)
Aristotle, thanks for sharing that, I'll be using it henceforth.
Though Text::FindIndent doesn't just apply to Perl code. It makes sense to apply it to pretty much any source code.
The
autocmd
matches the filetype against a pattern so can mention any number of types, eg.:Unless you want this to affect all files, in which case you’d hook the
BufNewFile
andBufRead
events (matching any filename) instead:(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...