10

Which should be used to format a block of text in code (monospace font). I personally believe that manually indenting each line of long console output or a log file can be very slow and repetitive, so those times, I often find myself using <pre></pre> tags. I also feel that it's much easier when creating a new line of code, you don't have to put 4 spaces at the start of each line.

So, should <pre></pre> tags be used over indentation for code blocks?

  • I'm rather curious... is this question in response to this? I'm just curious how many users actually go back and look at their edits... – Seth Sep 16 '13 at 19:29
  • @Seth basically, things like that are what I mean. Also, for the 2nd part, usually, I'd add a comment or an answer as well if I feel I can do that, otherwise an edit is all I can do. – kiri Sep 16 '13 at 23:37
17

Manual pre/code allows you to stick HTML in code. This is both a positive and a negative, depending on what you want to show. If you want to embolden a line, as below:

dadasdasd
asdasdasd
asdasdasd
asdasdasd

... pre/code makes sense. If you wanted to show the <strong> tag in use, it's a problem. You have to manually escape the tags.

I have personally settled on using four-spaces. You can highlight the entire block of code (once you've typed it, or pasted it in) and then press Control+K or press the <$> button in the toolbar and it'll do all the indentation for you.

I think that's a lot easier than messing around remembering to escape things.


The source of the code sample above:

<pre><code>dadasdasd
asdasdasd
as<strong>dasdasd</strong>
asdasdasd</code></pre>
  • 3
    What's the use of putting both <pre> and <code>? Don't they both do the same thing. Also, thanks for saying that you can highlight it all and press the <$> button, didn't know that. – kiri Sep 16 '13 at 8:18
  • 7
    Thanks Oli for making me realize I have wasted a lot of my time adding in spaces in tons of lines >:) – Rinzwind Sep 16 '13 at 8:23
  • 7
    @minerz029 It's just more semantically correct for HTML. Technically <pre> should mean pre-formatted, <code> means it's code. Their CSS definitions here mean <code> isn't required inside a <pre> to get the right look, but it's a bad habit to fall into. See: stackoverflow.com/questions/11742907/… – Oli Sep 16 '13 at 8:56
  • 6
    Crtl+K a revelation and a time saver all those hours waisted typing 4 spaces at the start of each line! Thanks for that – SimplySimon Sep 17 '13 at 11:55
  • Don't you have a programmer's text editor that can do that for you? – Martin Ueding Sep 17 '13 at 13:13
  • Thanks for the in-browser hint! (@queueoverflow) I'd previously been using :%s/^/ / in vim. – Sparhawk Sep 18 '13 at 5:50

You must log in to answer this question.

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