Wednesday, November 05, 2008

Vim Line Length

I happen to be a fan of the vi text editor. I also happen to be a fan of code style guidelines. And sometimes it is very handy to know if a line in your source code has gone over eighty characters long. Some style guides still recommend a max line length of eighty characters, a tradition dating all the way back to IBM punch cards, circa 1928. For an even more impressive feat of long term engineering influence look up how the size of Roman war chariots determined the size of NASA space shuttle booster rockets. But I digress.

To mark the characters which are over the eighty character limit, enter the following either within vim or in your .vimrc file.
:match ErrorMsg '\%>80v.\+'
I've noticed that some blogger templates tend to display fifty-five characters in a fixed width format (as in a <pre> block or <code>). To edit code which I plan to post on Blogger, I sometimes turn on
:match ErrorMsg '\%>55v.\+'

3 comments:

Unknown said...

Excellent. I work with 72 character lines and this is a nice way to quickly identify offenders.

To simply search, do:

/\%>72v.\+

Or to count all:

:%s/\%>72v.\+//n

Jeff Scudder said...

Great tip Michael! Very useful. Sometimes you want to jump dierctly to the long lines, instead up just higlighting.

MetalloUrlante said...

Cool tip. Thanks