1

I always struggle for like 10min trying to add code to my question..

I either have to paste it all in, then go and manually add spaces, or I use Ctrl+K and paste it in one line at a time.

Is this just me not understanding something, or is it just really time consuming?

10
  • 13
    Just paste it all, select it all, and use control + K or the code button on the formatting bar. That should do it.
    – Erik A
    Commented Jan 15, 2018 at 12:43
  • 1
    see meta.stackoverflow.com/questions/300188/…
    – rene
    Commented Jan 15, 2018 at 12:45
  • Strangely enough, the basic editor help does not mention keyboard shortcuts or toolbar buttons. You need to go to the advanced help to learn about the Ctrl-K shortcut, and it still doesn't mention the toolbar button.
    – user247702
    Commented Jan 15, 2018 at 12:52
  • 3
    @usr2564301 Let's not suggest such things, even if meant as a joke. (And if you are serious... well...)
    – user247702
    Commented Jan 15, 2018 at 12:53
  • @Stijn: I was not suggesting, I was asking. I'm curious as to why people would ever do so – and look! Here is a possible reason!
    – Jongware
    Commented Jan 15, 2018 at 12:54
  • @Stijn the tooltip mentions ctrl+K, not thrilled by that ...
    – rene
    Commented Jan 15, 2018 at 12:57
  • Related feature request (by me): meta.stackoverflow.com/a/359564/4284627 Commented Jan 15, 2018 at 16:33
  • 1
    I haven't looked at any of your questions, so this may not be applicable to you, but in general the idea that there is so much code there that it's really time consuming to indent it, even if you did have to do it one line at a time, suggests that there might be too much code. Not necessarily; some questions need quite a bit of code to show what's going on. I just wanted to take this opportunity to promote the M in MCVE. Commented Jan 15, 2018 at 17:59
  • does Ctrl+K replace tabs with spaces too?
    – Kevin B
    Commented Jan 15, 2018 at 18:08
  • "Is this just me not understanding something" - yes: Stack Overflow posts use a custom Markdown syntax (not exactly like GitHub's) that is thoroughly described here. Commented Jan 15, 2018 at 18:11

2 Answers 2

15

You should be able to select all the code you have in your question and use Ctrl+K to format it all as code simultaneously.

2
  • Yep, thanks :) I have always done it the other way around :P I think they should make it clearer for new people how to add code though..
    – Kossi
    Commented Jan 15, 2018 at 12:57
  • 4
    @Kossi it would be helpful if you could suggest better wording then for edting-help and/or i.sstatic.net/6UzU9.png as most users here are spoiled by what they already know. Maybe a single sentence can be enough or a word that would have helped you to figure this out, That would be really beneficial to future visitors.
    – rene
    Commented Jan 15, 2018 at 13:06
10

If I had a dollar for every developer that doesn't know that Shift+Tab decreases indentation, I'd be filthy rich.

So: create an MCVE in your IDE or text editor*, select all code and unindent all your code until the leftmost character hits the left gutter. Then press Tab once, and your code is ready to post.

*: except Windows Notepad, because only there (Shift+)Tab replaces the selected text...

10
  • 1
    You got a PayPal link, I think I owe you a dollar.
    – George
    Commented Jan 15, 2018 at 13:00
  • 1
    Nice tip, but the other answer was much simpler :P You should also explain your fancy abbreviations next time so I dont have to Google it... maybe Im just lazy
    – Kossi
    Commented Jan 15, 2018 at 13:25
  • 1
    I would be lost without shift+tab
    – Matt
    Commented Jan 15, 2018 at 17:37
  • Tried to add a tag for [mcve] but I guess it doesn't resolve on meta?
    – JeffC
    Commented Jan 15, 2018 at 18:02
  • 1
    @JeffC, you have to do it manually. I fixed it now. Commented Jan 15, 2018 at 18:04
  • Also, technically this answer doesn't work when users have set their IDE to indent with 2 or 3 spaces. Not sure about tabs. Commented Jan 15, 2018 at 18:06
  • This is why I set my IDE to 4-space indents. (Though my tab key is mapped to something else.) Commented Jan 15, 2018 at 18:13
  • This is what I do. I like this answer because I wish more people would take the time to use formatting tools in their IDE to fix all the indent issues I see in questions all the time. Pointing them at ways to use their IDE to post code more easily I think (hope) makes it more likely they will take advantage of these features also.
    – JeffC
    Commented Jan 15, 2018 at 18:13
  • @JeffC or online, even. Commented Jan 15, 2018 at 18:17
  • @Mat'sMug I agree. I use this site but only to format HTML. I don't understand why people would type code in an IDE but use a site to beautify it... I guess unless it's some super simple IDE that doesn't beautify code. I'm sure those exist but surely 99% of the time there must be a better replacement.
    – JeffC
    Commented Jan 15, 2018 at 18:34

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