11

Stack Overflow has a button you can press to indent a block of text four spaces so Markdown will treat it as code. GitHub uses Markdown, too, but does not seem to have formatting buttons.

Does GitHub have a keyboard shortcut or button to indent a block of code, or will I have to copy the code to a text editor, indent it, and paste it back in?

3 Answers 3

5

There is no keyboard shortcut to automatically indent a block of code.

You will either have to find a browser extension, like a userscript, or write one yourself. Or just add the four leading spaces as you would now.

You can see a list of current GitHub keyboard shortcuts by Shift + ? and clicking "See all" when on any GitHub page.

3

GitHub Flavored Markdown supports an alternative syntax for code formatting (notice the backticks):

```
#include <stdio.h>

int main(void)
{
    puts("Hello, world\n");
    return 0;
}
```

This does not require you to indent the code. Another bonus: you can specify the programming language to enable syntax highlighting:

```ruby
require 'redcarpet'
markdown = Redcarpet.new("Hello World!")
puts markdown.to_html
```
1
  • 1
    This is the best answer because GFM extends the Markdown standard so you don't need to indent. If only we could get some sort of consensus on what flavor should become the standard.
    – styfle
    Commented Feb 22, 2014 at 0:19
1

Select the lines you want to indent and hit Tab twice. Shift+Tab unindents.

3
  • 2
    This doesn't work for me in either Firefox or Chrome. What browser are you using?
    – Joey Adams
    Commented Oct 22, 2012 at 19:00
  • @JoeyAdams I've just tried with Chrome and Firefox. Also, it doesn't work in Gist (the editor is just a textarea). Commented Oct 24, 2012 at 23:42
  • Works nicely in Chrome
    – ZygD
    Commented Oct 15, 2018 at 12:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.