If I format some code by indenting it using four spaces, the code is not formatted. How do I format some code specifically for the language used? Or how do I prevent it from being formatted? Which languages are supported?


1 Answer 1


Add the line <!-- language: <language_string> --> (where <language_string> is the string identifying the language) before the start of the code, and make sure that the code itself has an empty line before it:

<!-- language: <language_string> -->

    <line 1>
    <line 2>
    <line 3>

*<language_string> = string identifying the language

As suggested by muru, if you want to prevent the code from being formatted instead, you can add the line <!-- language: none -->:

<!-- language: none -->

    # ...
    echo This snippet will not be nicely formatted when copy-pasted inside a post
    # ...

bash example:

<!-- language: bash -->

    # ...
    echo This snippet will be nicely formatted when copy-pasted inside a post
    # ...

Courtesy of terdon, here's the list of the currently supported languages along with their identifying string (most of the languages listed won't need the lang- prefix):

none:      none
bash:      lang-bash OR lang-bsh
c:         lang-c
clj:       lang-clj
coffee:    lang-coffee
cpp:       lang-cpp
cs:        lang-cs
csh:       lang-csh
css:       lang-css
dart:      lang-dart
erlang:    lang-erlang
go:        lang-go
hs:        lang-hs
html:      lang-html
java:      lang-java
js:        lang-js
latex:     lang-latex
lisp:      lang-lisp
lua:       lang-lua
ml:        lang-ml
pascal:    lang-pascal
perl:      lang-perl
php:       lang-php
proto:     lang-proto
py:        lang-py
r:         lang-r
rb:        lang-rb
regex:     lang-regex
rust:      lang-rust
scala:     lang-scala
sh:        lang-sh
sql:       lang-sql
vb:        lang-vb
vhdl:      lang-vhdl
xml:       lang-xml

Finally, if the question is tagged with , , or - any of the ones listed above in fact, it will automatically format it. Example: What is the syntax for IF/ELSE in bash?.

As explained by @Braiam, the lang- prefix is needed for those languages that are not associated with any tag.

  • 1
    It turns out that lang-bash also works, despite only lang-bsh being listed.
    – terdon
    Apr 22, 2015 at 12:01
  • @terdon Thanks, I updated it
    – kos
    Apr 22, 2015 at 12:07
  • 1
    Why is there no BASIC in there? @terdon
    – Rinzwind
    Apr 22, 2015 at 12:49
  • 1
    For plenty of languages, you don't need the lang- at all.
    – muru
    Apr 22, 2015 at 13:14
  • 1
    @muru I think the lang- is optional for all of them.
    – terdon
    Apr 22, 2015 at 13:35
  • @muru Thanks, but the <pre></pre> blocks were meant to show the empty lines at the end of the snippet
    – kos
    Apr 22, 2015 at 13:35
  • @Rinzwind no idea. Also notice the conspicuous and unexplainable absence of lang-whitespace!
    – terdon
    Apr 22, 2015 at 13:35
  • 1
    @terdon Unless it's a bug, not for lang-sql
    – kos
    Apr 22, 2015 at 13:36
  • 1
    @kos you don't need empty lines at the end of a snippet. You do need an empty line before the <!-- language line.
    – muru
    Apr 22, 2015 at 13:36
  • @muru You're right, updated
    – kos
    Apr 22, 2015 at 13:44
  • 1
    @kos yeah, some do work without it but not all.
    – terdon
    Apr 22, 2015 at 13:52
  • @terdon Also no Brainfuck support. Believe it or not, people writes PHP interpreters with it
    – kos
    Apr 22, 2015 at 14:39
  • @Tim Didn't know about that, I think you should add another answer to this question ;)
    – kos
    Apr 23, 2015 at 16:28
  • @kos nah, it can all go in one :)
    – Tim
    Apr 23, 2015 at 16:29
  • 2
    @terdon the lang- prefix is required unless there's a tag associated with the language, ie. you can type either python or lang-python for python would work, but only pip would work as the tag is associated (at least on SO) with the lang-python formating. If a tag, like scala doesn't have associated a formating at the end of the tag info then neither adding the tag, nor just "scala" would work, you will have to type lang-scala.
    – Braiam
    May 3, 2015 at 21:00

You must log in to answer this question.

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