54

This happens to me a lot on Stack Overflow: I paste in some code, but it is indented to the right far too much. This requires cumbersome horizontal scrolling to view all of the code, and oftentimes the code block doesn't line up with other code blocks in the same post.

Is there a keyboard shortcut that I can use so that if I highlight the whole code block I can quickly shift it to the left or right?

3
  • 11
    Select your code and click on the code button a few times
    – Rizier123
    Mar 8, 2016 at 23:57
  • When I look at your Case In Point, the problem is obvious: it's the goddamned "Hot Network Questions" and other superfluous material taking up 30% of the browser width! SO needs an option to minimize that stuff (have it roll up to the right or whatever).
    – Kaz
    Mar 11, 2016 at 3:03
  • 2
    @Kaz It is funny you said that as they just made it bigger a couple of months ago. Mar 11, 2016 at 19:33

1 Answer 1

78

For reasons previously unbeknownst to me, the same keyboard shortcut that you use to format unformatted code as Markdown code blocks (Ctrl+K) can also be used to reduce the indentation of code blocks that have too much indentation — four spaces a punch. Once it is no longer possible to remove four spaces leading each line, the shortcut then alternates between removing and adding four spaces.

I've been using this same keyboard shortcut for years to perform similar edits on numerous posts, but I never understood why it worked, and I don't think it ever would have occurred to me if it weren't for the fact that I discovered it entirely by accident. So I don't blame you for not realizing this.

But now that I think about it, I understand why it works this way: as long as every line in the selection contains four leading spaces, the Markdown parser considers the selection to be a code block. So the editor also treats it as a code block, and the shortcut (or the "Code Sample" icon in the editor UI) removes the spaces in an attempt to convert it back to plain text — except in the case of excess indentation all you get is another code block, albeit with less indentation. Repeating the same shortcut causes the editor to keep removing four spaces until it is no longer possible to do so, by which time the selection is no longer a proper Markdown code block.

Note that this will not work on code that is indented with tabs — it only adds and removes spaces in groups of four, or if the indentation is all tabs then it behaves exactly as though the tabs were a part of the code, not whitespace.

8
  • 2
    Now, if only it recognized that a block of text with just 4 leading spaces ISN'T a code block when it's in a list item! WBN to be able to use a keyboard shortcut to shift the whole block right.
    – Mogsdad
    Mar 9, 2016 at 16:36
  • 1
    @Mogsdad When I want to format code inside a list, I use Crl+K, cut the spaces before the first line of code, use Ctrl-K again, and paste the spaces back.
    – Oriol
    Mar 10, 2016 at 0:53
  • 28
    But pretty please can we have tab/shift-tab to outdent/indent?
    – DavidG
    Mar 10, 2016 at 1:17
  • 1
    @Oriol - I do something similar; I indent the code, add a left-justified bit of garbage text, indent again, then delete the garbage line. All pretty non-intuitive, either way.
    – Mogsdad
    Mar 10, 2016 at 1:23
  • @Mogsdad Since you're here, perhaps we can nudge Tiny to act on your FR!
    – DavidG
    Mar 10, 2016 at 1:32
  • @DavidG - I just added more info to the FR, in fact. BUT, I've also installed balpha's script, and now have tab/shift-tab as well as the MagicEditor! (Of course, ME works everywhere, this script doesn't.)
    – Mogsdad
    Mar 10, 2016 at 1:56
  • 1
    Another major limitation with this is that it can't be used to add additional indentation to code when needed. While generally not a problem with any of my code (I keep it properly indented), when reviewing/editing posts I often found myself having to copy someone elses code into a 3rd party editor because they pasted in a mess with totally screwed up indenting. Mar 11, 2016 at 19:04
  • @DanNeely My workaround is to add a single character at the beginning of the line somewhere in the selection before using Ctrl+K.
    – Justin
    Sep 1, 2018 at 0:33

You must log in to answer this question.

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