Perl isn't... FORTRAN. Still, it's often handy to be able to see (and talk about) line numbers in code listings. Editors and pastebins do this; why not PerlMonks?

It turns out that a slightly obscure feature lets us do just that. Follow these instructions and you will see line numbers alongside every block of code here. (This will not affect inline code.)

First navigate to Display Settings. We will make a few changes here.

In the Code Listing Section, be sure Code Wrapping Off is not checked.

For Code Prefix, enter:    <span class="line-number">&1;</span>&nbsp;

Line numbers will mess you up if you like to copy code right out of node text; instead, use the several [download] links provided. Be sure Don't show embedded d/l links is not checked and Show download link on code N lines long or more is set to a very small number, or left blank.

To style the new line-number class, we need to fix the stylesheet. We can paste the following into On-Site CSS Markup:

.codeblock { margin-left: 1em; background-color: #ddf; } .line-number { color: #779; background-color: #fdd; }

That's it; we're done. You may want to experiment with the color balance but that's... up to you.

Thanks

roboticus took pity on me whining in CB and pointed me to New code wrap options.

Corion made a good notice which unfortunately did not pan out.

Honorable mention goes to other six a.m. CBers who put up with the fumbling.

Feste: Misprison in the highest degree. Lady, cucullus non facit monachum. That's as much to say as, I wear not motley in my brain....

In reply to Line Numbering in Code Listings by Xiong

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.