Is there a dirt-simple way to post code snippets in a StackOverflow answer? [duplicate]

StackOverflow https://stackoverflow.com/questions/10955900

  •  13-06-2021
  •  | 
  •  

Question

Possible Duplicate:
How do I format my code blocks?

Is there a dirt-simple way to post code snippets in a StackOverflow answer?

I've been putzing around the with either the 'inline' code curly braces, or this 'indent 4 spaces' concept. Still losing whitespace and/or wordwrapping where it shouldnt be.

Was it helpful?

Solution

Hold down Shift while you select the code block with either the mouse or arrow keys, this should highlight the block.

Now hit the {} button above the entry box, this should indent all the selected code by the necessary 4 spaces and also put blank lines before and after if necessary. You can see the effect immediately in the preview below the entry box.

OTHER TIPS

In your favorite code editor, ensure that the farthest left indent of the code that you are about to copy is four characters from the left margin of the editor. Once you've done this, select and copy. Then, when you paste in the editor, you're already done.

For example, in my case, I'll select my code (visual studio), hit shift-tab (decrease indent) until at least one line of code has touched the left margin of the code editor, then I'll hit tab once. That adds four spaces of indent to the code. I then copy and paste it into the question editor.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top