Many times I have some code that I want to copy-paste it to SO.

I usually format my code well(indentation and etc) and usually I like to copy it and paste it and then remove the irrelevant parts.

Unfortunately when pasting it preserves its format but it doesn't get indented by four spaces even after clicking {} and pasting it, So at many times I have to go line by line clicking space four times and this is somewhat frustrating.

So what should I do? (apart from getting some browser extension)

  • 1
    Copy-paste, select full code and press CTRL + K ? – Spikatrix Jul 25 '15 at 14:17
  • 1
    thanks @CoolGuy , why didn't I find it in help – niceman Jul 25 '15 at 14:22

The code formatter {} can't tell what code you are trying to format - after all, it is likely you have mixed text and code in the post, it is possibly that you are pasting text and not code.

It needs some help there - someone needs to tell it what to format.

This is done by selecting the code - once selected, clicking {} (or the keyboard shortcut for it, Ctrl + k) will indent it 4 spaces.

  • 1
    hahaha funny why didn't I think about trying that, thanks :) – niceman Jul 25 '15 at 14:23

Just an addition to Oded's answer. You could get into trouble if your formatting uses a mix of spaces and tabs. Many editors use 8 spaces for 1 tab, where SE paste seems to convert with 4 spaces for one tab.

So, my advice is to first convert all tabs to spaces in your external editor and only then copy and paste into Stack Overflow.

You must log in to answer this question.

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