9

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?

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.

4

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
    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 Feb 22 '14 at 0:19
2

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

  • This doesn't work for me in either Firefox or Chrome. What browser are you using? – Joey Adams Oct 22 '12 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). – Francesc Rosas Oct 24 '12 at 23:42
  • Works nicely in Chrome – ZygD Oct 15 '18 at 12:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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