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
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: lang-bash -->
echo This snippet will be nicely formatted when copy-pasted inside a post
The exact same can be accomplished when formatting by wrapping the code in triple backticks, by putting <language_string>
after the first set of backticks:
```lang-bash
echo This snippet will be nicely formatted when copy-pasted inside a post
```
Here's the list of the currently supported languages along with their identifying string (updated on 2024-10-14; for an always up-to-date list, check out the original MSE post):
Language name | lang-code |
Custom Stack Exchange aliases |
Other Highlight.js aliases |
---|---|---|---|
Plain text (explicitly disables syntax highlighting) |
lang-plaintext |
lang-none |
lang-text lang-txt |
Bash and other shell scripts | lang-bash |
lang-bsh lang-csh lang-sh |
- |
C and C-likes | lang-c |
lang-cyc lang-m lang-c-like |
lang-h |
Clojure | lang-clojure |
lang-clj |
lang-edn |
CoffeeScript | lang-coffeescript |
lang-coffee |
lang-cson lang-iced |
C++ | lang-cpp |
lang-cc lang-cxx |
lang-c++ lang-h++ lang-hpp lang-hh lang-hxx |
C# | lang-csharp |
lang-cs |
lang-c# |
CSS | lang-css |
- | - |
Dart | lang-dart |
- | - |
Delphi, Pascal | lang-delphi |
- | lang-dpr lang-dfm lang-pas lang-pascal |
Erlang | lang-erlang |
lang-erl |
- |
Go | lang-go |
- | lang-golang |
Haskell | lang-haskell |
lang-hs |
- |
HTTP request/response | lang-http |
- | lang-https |
INI, TOML | lang-ini |
- | lang-toml |
Java | lang-java |
- | lang-jsp |
JavaScript | lang-javascript |
lang-js |
lang-jsx lang-mjs lang-cjs |
JSON | lang-json |
- | - |
Julia | lang-julia |
- | - |
Kotlin | lang-kotlin |
- | lang-kt lang-kts |
LaTeX, TeX | lang-latex |
lang-tex |
- |
Less | lang-less |
- | - |
Lisp | lang-lisp |
lang-cl lang-el lang-lsp |
- |
Lua | lang-lua |
- | - |
Makefile | lang-makefile |
- | lang-mk lang-mak lang-make |
Markdown | lang-markdown |
- | lang-md lang-mkdown lang-mkd |
Mathematica / Wolfram Language (Mathematica SE only) |
lang-mathematica |
lang-mma |
lang-wl |
MATLAB | lang-matlab |
- | - |
Objective-C | lang-objectivec |
- | lang-mm lang-objc lang-obj-c lang-obj-c++ lang-objective-c++ |
OCaml, F#, SML and other ML-family languages | lang-ocaml |
lang-fs lang-ml |
- |
Perl | lang-perl |
lang-pl |
lang-pm |
PHP | lang-php |
- | - |
PHP template HTML+PHP | lang-php-template |
- | - |
Protocol Buffers | lang-protobuf |
- | lang-proto |
Python | lang-python |
lang-py lang-cv |
lang-gyp lang-ipython |
R, S | lang-r |
lang-s |
- |
Ruby | lang-ruby |
lang-rb |
lang-gemspec lang-podspec lang-thor lang-irb |
Rust | lang-rust |
lang-rc lang-rs |
- |
Scala | lang-scala |
- | - |
Scheme, Racket | lang-scheme |
lang-scm lang-ss lang-rkt |
- |
SCSS | lang-scss |
- | - |
Shell session | lang-shell |
- | lang-console lang-shellsession |
SQL | lang-sql |
- | - |
Swift | lang-swift |
- | - |
TypeScript | lang-typescript |
- | lang-ts lang-tsx lang-mts lang-cts |
Visual Basic .NET, VBScript | lang-vbnet |
lang-vb lang-vbs |
- |
VHDL | lang-vhdl |
lang-vhd |
- |
XML, HTML and their derivatives | lang-xml |
lang-html lang-xsl |
lang-xhtml lang-rss lang-atom lang-xjb lang-xsd lang-plist lang-wsf lang-svg |
YAML | lang-yaml |
- | lang-yml |
As explained by @Braiam, the lang-
prefix is needed for those languages that are not "hinted at" by any tag (e.g., lang-bash
on Ask Ubuntu, being hinted at by bash, could be written simply as bash
).
Finally, if the question is tagged with a tag that "hints" at a language (e.g. again bash), the snippet will automatically formatted.
-
1It turns out that
lang-bash
also works, despite onlylang-bsh
being listed.– terdonCommented Apr 22, 2015 at 12:01 -
1
-
1
-
1
-
@Rinzwind no idea. Also notice the conspicuous and unexplainable absence of
lang-whitespace
!– terdonCommented Apr 22, 2015 at 13:35 -
1
-
1
-
@terdon Also no Brainfuck support. Believe it or not, people writes PHP interpreters with it– kosCommented Apr 22, 2015 at 14:39
-
There is a question over on MetaSE that has a answer on how to use syntax highlighting features: meta.stackexchange.com/q/184108 . Google's prettify is used, for which the README is currently here (it may move to here). BASIC is apparently available by extensions, which SE may be able to add :)– WilfCommented Apr 27, 2015 at 0:05
-
2@terdon the lang- prefix is required unless there's a tag associated with the language, ie. you can type either
python
orlang-python
for python would work, but onlypip
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 typelang-scala
.– BraiamCommented May 3, 2015 at 21:00