6

Possible Duplicate:
How do I format my code blocks?

When adding a code block of several lines, must I indent each line of the code block by fpir spaces, or just the first line? In the following block, each line of the code is indented four lines.

<?php
echo "each line is here indented four spaces";
setCookie(\'testtest\', '.$book['code'].', time()+ 172800);
<script type="text/javascript">alert("does this work");</script>

In my earlier posts, I was using the "code sample" {} button on the toolbar above this text entry box. That didn't work well at all... So here comes the second code block. It is the same code, but only the first line of the code block is indented the four space. Just seeing if the format is different.

<?php

echo "each line is here indented four spaces"; setCookie(\'testtest\', '.$book['code'].', time()+ 172800);

alert("does this work");

And this is the end of this post.

  • 3
    Select the code and press ctrl+K – Erik Mar 27 '11 at 18:04
  • You didn't have to post this question, you could see the preview at the bottom as you typed it. – Ilya Kogan Mar 27 '11 at 18:05
  • You may be right... I am new to StackOverflow (fewer than 10 posts) and have been figuring it out. The {} toolbar button's tooltip "code sample" was slightly misleading (not all code samples, just a partial snippet) and I wanted to be sure that it would be the same. I'm not yet a pro coder myself, so I'm never sure how things are working "under the hood." – user159392 Mar 27 '11 at 22:24
  • Highlight the text that you want as code. Click the code button {} or press control K, this button will toggle your highlighted text between code or non code - no need to indent. – DermFrench Feb 1 '13 at 17:46
  • I paste in code and the indent seems to just be wrong. Method signatures are right but the entire braces block is too far indented compared to how it looks in my editor. Extra white-space included in the copy and paste maybe? – Chucky Aug 29 '19 at 15:20
1

You must indent each line; see http://daringfireball.net/projects/markdown/syntax (which is linked at the bottom of the StackOverflow markdown help for details. I put together a tiny Mac OS X service to do this for me using Automator (it just runs sed).

UPDATE: You can select a whole block of code after pasting and click the code sample button {} on the toolbar. This will indent everything selected by 4 spaces.

0

There's the answer...

The code sample button {} on the toolbar is for a single line of inline code.

To paste a block of code, each line of the code block must be indented four spaces.

  • +1 for answering your own question :) – faif Mar 27 '11 at 18:05
  • 7
    but you can select a whole block of code and click the code sample button {} on the toolbar, it will indent that whole code block 4 spaces. – gideon Mar 28 '11 at 3:32
  • THANK YOU -- That's a great tip! – user159392 Mar 29 '11 at 14:48
  • 1
    @user Np. Also the keyboard shortcut is Ctrl + K – gideon Mar 29 '11 at 15:04

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