Is there any way to quote/format code including automated line numbers?

Sometimes especially in quite long configuration files and scripts it would be very helpful for both question and answers to be able to refer to a certain line of code by line number.

Is there a way to get something like

1. Code
2. More code
3. Even more code
4. ...

without having to insert the numbering manually?

  • 1
    – muru
    Commented Oct 18, 2017 at 13:00
  • Thx @muru I didn't find this discussion. Anyway the arguments against this because of changing code blocks seem quite vague to me because the same is happening also right now changing code blocks without line numbers. But I understand .. maybe this would be a great feature if all user would think before posting or at least before editing questions
    – derHugo
    Commented Oct 18, 2017 at 13:06

1 Answer 1


You could run it through cat -n (see man cat for more info) on your computer to automatically add numbers to each line, but to my knowledge there is no way to make Stack Exchange add them automatically.

However, bear in mind that if you add line numbers to the code text, someone who wants to copy your code and maybe test or edit it will also get those numbers and have to remove them manually again.

Therefore, IMHO better repeat the line you're talking about (plus saying the number or in what section of the code it is) than numbering the whole code block.

  • Yeah for the same reason you say already (the copying) I wandered if there is a better solution to this. this function obviously shouldn't copy the line numbers if copying the code .. they would have to be apart of the actual code block.
    – derHugo
    Commented Oct 18, 2017 at 12:55
  • 1
    I think I'll stick to the "comments way" adding line comments to refer to.
    – derHugo
    Commented Oct 18, 2017 at 13:07

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .