{{Short description|Empty space at the beginning of a line to signal the start of a new paragraph}} {{Redirect|Indentation}} {{Use dmy dates|date=July 2022}} {{Wiktionary|indentation}} __FORCETOC__ In the written form of many languages, '''indentation''' describes empty space (white space) used before or around text to signify an important aspect of the text such as: * Beginning of a paragraph * Hierarchy {{endash}} subordinate concept * Quotation
Many computer languages use ''block indentation'' to demarcate blocks of source code.
Indentation is essentially the same regardless of whether the writing system is left-to-right (e.g. Latin and Cyrillic) or right-to-left (e.g. Hebrew and Arabic) when considering line beginning and end. For example, indenting at the beginning of line means on the left for a left-to-right script and on the right for right-to-left script.
Indent is both a noun and a verb. The verb is the act of formatting text to be indented whereas the noun refers to the resulting empty space.
== Types == There are three main types of indentation: first-line, hanging and block.
Each example below is in a box that represents the page boundary and uses the common typesetting lorem ipsum content. The width of indentation here is in units of em spaces.
{{anchors|first-line indent|first line indent|first line indentation|1st-line indent|1st-line indentation|1st line indent|1st line indentation|first-line|first line|1st-line|first line|firstline|firstline indent|firstline indentation}} For '''first-line indentation''' the first line of a paragraph is indented, {{unichar|2029|Paragraph Separator|html=}}
A first-line indentation of 2 em: <div style="background: #FFFCFA; width: 50%;"><div style="text-indent: 2em; border-left: thin solid #999999; border-right: thin solid #999999; padding-left: 1em; padding-right: 1em; margin-top: 0.5em; margin-bottom: 1em;">{{lorem ipsum}} </div></div> {{anchor|hanging indent}} For '''hanging indentation''' all but the first line of a paragraph is indented.
A hanging indentation of 2 em: <div style="background: #FFFCFA; width: 50%;"><div style="padding-left: 3em; text-indent: -2em; border-left: thin solid #999999; border-right: thin solid #999999; padding-right:1em; margin-top: 0.5em; margin-bottom: 1em;">{{lorem ipsum}}</div></div> {{anchor|block indent|block quote|block quotation|blockquote}} For '''block indentation''' the entire paragraph is indented relative to the preceding paragraph.
A block indentation of 2 em on the left: <div style="background: #FFFCFA; width: 50%;"><div style="padding-left: 3em; border-left: thin solid #999999; border-right: thin solid #999999; padding-right:1em; margin-top: 0.5em; margin-bottom: 1em;">{{lorem ipsum}}</div></div> Block indentation on both sides is commonly used for a quotation, a block quotation, here shown with 2 em on the left and right but since it is not right-aligned, the empty space on the right varies based on line length: <div style="background: #FFFCFA; width: 50%;"><div style="padding-left: 3em; padding-right:3em; border-left: thin solid #999999; border-right: thin solid #999999; margin-top: 0.5em; margin-bottom: 1em;">{{lorem ipsum}}</div></div> Here's the same as the previous but justified (left and right aligned): <div style="background: #FFFCFA; width: 50%;"><div style="padding-left: 3em; padding-right:3em; text-align: justify; border-left: thin solid #999999; border-right: thin solid #999999; margin-top: 0.5em; margin-bottom: 1em;">{{lorem ipsum}}</div></div>
== Indentation in programming ==
{{See also|Indentation style}}
In computer programming, indentation describes formatting source code with whitespace to the left of code text {{endash}} often to visually show that a sequence of code lines is syntactically a code block. Typically, the lines of a block are aligned with an amount of white space that indicates the block's depth in the hierarchical structure of the code. Each inner level of the hierarchy is indented by a multiple of this ''indentation width''.
White space in code is typically stored as whitespace characters.
For a free-form language, indentation is exclusively for the programmer since a code processor (i.e. compiler, interpreter) ignores whitespace characters. Code can have inconsistent or even no indentation, but in general is formatted with somewhat consistent indentation.
Some languages rely on indentation to demarcate block structure, often via the off-side rule. Due to this syntax requirement, the code must have a level of consistency that is not required in free-form language code.
The neologisms '''outdent''', '''unindent''' and '''dedent''' describe the opposite of indentation {{endash}} aligning code text of a line to the left of the previous line.
=== Variations ===
Common variations in the implementation of indentation include: how much to indent a block at each level of the code hierarchy, usually measured in spaces, and whether to store whitespace characters as space or tab characters. Although there are common practices, consensus is not universal. <ref>{{cite web|url=http://www.jwz.org/doc/tabs-vs-spaces.html |title=Tabs versus Spaces: An Eternal Holy War |website=Jwz.org |date=2007-01-05 |access-date=2014-01-18}}</ref> These variations are driven by factors that may include but are not limited to: language syntax, organizational mandate and personal preference.
=== Common practices ===
The following table identifies notable practices with respect to code indentation.
{| class="wikitable" |- !Language !! Free-form? !! Note |- | Bash || {{Yes}} || Fritz Mehner's style guide suggests 2, 4, or 8 spaces and uses 2 in all examples<ref>{{cite web|url=https://lug.fh-swf.de/vim/vim-bash/StyleGuideShell.en.pdf|title=Bash Style Guide and Coding Standard|website=Lug.fh-swf.de}}</ref>
Google uses 2 spaces <ref>{{cite web |url=https://google-styleguide.googlecode.com/svn/trunk/shell.xml |title=Archived copy |access-date=2014-06-23 |url-status=dead |archive-url=https://web.archive.org/web/20140626134128/http://google-styleguide.googlecode.com/svn/trunk/shell.xml |archive-date=2014-06-26 }} </ref> |- | C || {{Yes}} || The Linux kernel uses 1 tab<ref>{{cite web |url=https://www.kernel.org/doc/Documentation/CodingStyle |title=Archived copy |access-date=2015-02-15 |url-status=dead |archive-url=https://web.archive.org/web/20150214004842/https://www.kernel.org/doc/Documentation/CodingStyle |archive-date=2015-02-14 }} </ref><ref> {{cite web |url=https://www.kernel.org/doc/html/latest/process/coding-style.html |title=Linux kernel coding style |access-date=2022-07-21 |url-status=live |archive-url=https://web.archive.org/web/20220611181038/https://www.kernel.org/doc/html/latest/process/coding-style.html |archive-date=2022-06-11 }} </ref>
NASA uses 4 spaces.<ref>{{cite web|url=https://ntrs.nasa.gov/citations/19950022400|title=C STYLE GUIDE|date=August 1994|website=NASA Technical Reports Server}}</ref>
Clinton Staley advocates 3 spaces<ref>{{Cite web|last=Staley|first=Clinton|date=2006|title=C Program Style Rules|url=https://users.csc.calpoly.edu/~cstaley/General/CStyle.htm|website=Cal Poly Department of Computer Science & Software Engineering}}</ref> |- | C++ || {{Yes}} || WebKit recommends 4 spaces<ref>{{Cite web|date=2015-11-07|title=Code Style Guidelines|url=https://webkit.org/code-style-guidelines/|access-date=2021-12-31|website=WebKit}}</ref> Google uses 2 spaces<ref>[http://google-styleguide.googlecode.com/svn/trunk/cppguide.html#Spaces_vs._Tabs] {{webarchive|url=https://web.archive.org/web/20141006093744/http://google-styleguide.googlecode.com/svn/trunk/cppguide.html|date=2014-10-06}}</ref> |- | C# || {{Yes}} || Microsoft convention: 4 spaces<ref>{{cite web|url=http://msdn.microsoft.com/en-us/library/vstudio/ff926074.aspx|title=C# Coding Conventions (C# Programming Guide)|author=Bill Wagner|website=Msdn.microsoft.com|access-date=3 September 2017}}</ref> |- | CSS || {{Yes}} || Drupal, GitHub, and Google use 2 spaces<ref>{{cite web|url=https://drupal.org/node/1887862 |title=CSS formatting guidelines |website=Drupal.org |date=14 January 2013 |access-date=2014-01-18}}</ref><ref>{{cite web|url=https://github.com/styleguide/css |title=CSS · Styleguide · GitHub |website=Github.com |access-date=2014-01-18}}</ref><ref name="google">{{cite web |url=http://google-styleguide.googlecode.com/svn/trunk/htmlcssguide.xml#Indentation |title=Google HTML/CSS Style Guide |at=Indentation |quote=Indent by 2 spaces at a time. |work=Google GitHub |access-date=2016-03-13 |archive-url=https://web.archive.org/web/20160313125550/https://google-styleguide.googlecode.com/svn/trunk/htmlcssguide.xml#Indentation |archive-date=2016-03-13 |url-status=dead }} This is an HTML-rendered copy of the official version, which is maintained in XML format</ref><ref>{{cite web|url=https://github.com/google/styleguide|title=styleguide: Style guides for Google-originated open-source projects|date=3 September 2017|website=Github.com|access-date=3 September 2017}}</ref>
WordPress uses tabs<ref>{{cite web|url=https://make.wordpress.org/core/handbook/best-practices/coding-standards/css/#structure |title=WordPress › CSS Coding Standards « Make WordPress Core |website=Make.wordpress.org |date=17 July 2012 |access-date=2018-07-20}}</ref> |- | Dart || {{Yes}} || <code>dart format</code> uses 2 spaces |- | Delphi || {{Yes}} || Delphi style guide: 2 spaces <ref>{{cite web|url=https://docwiki.embarcadero.com/RADStudio/Alexandria/en/White_Space_Usage#Indentation|title=Delphi's Object Pascal Style Guide - White Space Usage|access-date=13 December 2022 |language=en-us}}</ref> |- | F# || {{Yes}} || F# style guide: 4 spaces<ref>{{cite web |title=F# code formatting guidelines |url=https://docs.microsoft.com/en-us/dotnet/fsharp/style-guide/formatting |website=docs.microsoft.com |access-date=14 October 2020 |language=en-us}}</ref> |- | Go || {{Yes}} || ''Effective Go'': 1 tab <ref>{{cite web|url=http://golang.org/doc/effective_go.html|title=Effective Go – The Go Programming Language|website=golang.org|access-date=3 September 2017}}</ref> |- | Haskell || {{No}} || Google uses 2 spaces<ref>{{cite web|url=https://code.google.com/p/ganeti/wiki/HaskellStyleGuide#Indentation |title=HaskellStyleGuide – ganeti – Style Guide for the Haskell code – Cluster-based virtualization management software – Google Project Hosting |website=Code.google.com |date=2014-01-08 |access-date=2014-01-18 |url-status=dead |archive-url=https://web.archive.org/web/20130627194135/http://code.google.com/p/ganeti/wiki/HaskellStyleGuide |archive-date=June 27, 2013 }}</ref> |- | HTML || {{Yes}} || Google uses 2 spaces<ref name="google" />
HTML Tidy defaults to 2 spaces<ref>{{cite web|url=https://tidy.sourceforge.net/docs/quickref.html#indent-spaces |title=HTML Tidy Configuration Options Quick Reference |website=Tidy.sourceforge.net |date=2008-06-18 |access-date=2014-01-18}}</ref> |- | Java || {{Yes}} || Oracle uses 4 spaces<ref>{{cite web|url=http://www.oracle.com/technetwork/java/javase/documentation/codeconventions-136091.html#262 |title=Code Conventions for the Java Programming Language: 4. Indentation |website=Oracle.com |access-date=2014-01-18}}</ref>
Android uses 4 spaces<ref>{{cite web|url=http://source.android.com/source/code-style.html#use-spaces-for-indentation |title=Code Style Guidelines for Contributors | Android Developers |website=Source.android.com |access-date=2014-01-18}}</ref>
Most Eclipse IDE components use tabs |- | JavaScript || {{Yes}} || Douglas Crockford advocates 4 spaces<ref>{{cite web|url=http://javascript.crockford.com/code.html |title=Code Conventions for the JavaScript Programming Language |website=Javascript.crockford.com |date=2006-11-13 |access-date=2014-01-18}}</ref>
GitHub and Google use 2 spaces<ref>{{cite web |url=https://github.com/styleguide/javascript |title=JavaScript 路 Styleguide |website=Github.com |access-date=2014-01-18 |url-status=dead |archive-url=https://web.archive.org/web/20130815075924/https://github.com/styleguide/javascript |archive-date=2013-08-15 }}</ref><ref>{{cite web |url=http://google-styleguide.googlecode.com/svn/trunk/javascriptguide.xml#Code_formatting |title=JaveScript Style Rules |website=Google-styleguide.googlecode.com |access-date=2014-02-18 |archive-url=https://web.archive.org/web/20140216111413/http://google-styleguide.googlecode.com/svn/trunk/javascriptguide.xml#Code_formatting |archive-date=2014-02-16 |url-status=dead }}</ref>
jQuery uses tabs<ref>{{cite web|author=jQuery Foundation - jquery.org |url=http://contribute.jquery.org/style-guide/js/ |title=JavaScript Style Guide | Contribute to jQuery |website=Contribute.jquery.org |access-date=2014-01-18}}</ref>
Firefox's built-in jsbeautifier defaults to 2 spaces
prettyprinter in Google Chrome and Internet Explorer use 4 spaces |- | Julia | {{Yes}} | Julia style guide: 4 spaces<ref>{{cite web |title=Style Guide · The Julia Language |url=https://docs.julialang.org/en/v1.6/manual/style-guide/#Indentation |website=docs.julialang.org}}</ref> |- | Kotlin | {{Yes}} | Kotlin style guide: 4 spaces<ref>{{cite web |title=Coding Conventions – Kotlin Programming Language |url=https://kotlinlang.org/docs/reference/coding-conventions.html |website=Kotlin}}</ref> |- | Lua || {{Yes}} || Lua style guide: 2 spaces<ref>{{cite web|url=http://lua-users.org/wiki/LuaStyleGuide|title=lua-users wiki: Lua Style Guide|website=lua-users.org|access-date=3 September 2017}}</ref> |- | Perl || {{Yes}} || Larry Wall prefers 4 spaces<ref>{{cite web |url= http://perldoc.perl.org/perlstyle.html |title=perlstyle: Perl style guide |work=Perl 5 Version 32.1 Documentation |editor-first=Jon (JJ) |editor-last=Allen |publisher=Perl Foundation |access-date=2021-04-19}}</ref> |- | PHP || {{Yes}} || Drupal use 2 spaces<ref>{{cite web|url=https://drupal.org/coding-standards#indenting |title=Coding standards |website=Drupal.org |access-date=2014-01-18}}</ref>
PEAR and Zend use 4 spaces<ref>{{cite web|url=http://pear.php.net/manual/en/standards.indenting.php |title=Manual :: Indenting and Line Length |website=Pear.php.net |access-date=2014-01-18}}</ref><ref>{{cite web |url=http://framework.zend.com/wiki/display/ZFDEV/PHP+Coding+Standard+%28draft%29 |title=PHP Coding Standard (draft) – Contributors – Zend Framework Wiki |website=Framework.zend.com |access-date=2014-01-18 |archive-url=https://web.archive.org/web/20080103203245/http://framework.zend.com/wiki/display/ZFDEV/PHP+Coding+Standard+(draft) |archive-date=2008-01-03 |url-status=dead }}</ref>
CodeIgniter and WordPress use tabs<ref>{{cite web |url=http://ellislab.com/codeigniter/user-guide/general/styleguide.html#code_indenting |title=Style Guide : CodeIgniter User Guide |website=Ellislab.com |access-date=2014-01-18 |archive-date=1 June 2013 |archive-url=https://web.archive.org/web/20130601111626/http://ellislab.com/codeigniter/user-guide/general/styleguide.html#code_indenting |url-status=dead }}</ref><ref>{{cite web|url=http://make.wordpress.org/core/handbook/coding-standards/php/#indentation |title=WordPress › PHP Coding Standards « Make WordPress Core |website=Make.wordpress.org |date=23 July 2012 |access-date=2014-01-18}}</ref>
PSR-2 specifies 4 spaces<ref>{{cite web |url=http://www.php-fig.org/psr/2/ |website=Php-fig.org |access-date=2014-02-18 |title=PHP : Coding Style Guide |url-status=dead |archive-url=https://web.archive.org/web/20140315044508/http://www.php-fig.org/psr/2/ |archive-date=2014-03-15 }}</ref> |- | PowerShell || {{Yes}} || Unofficial PowerShell Best Practices and Style Guide: 4 spaces<ref>{{cite web |title=Code Layout and Formatting – PowerShell Practice and Style |url=https://poshcode.gitbook.io/powershell-practice-and-style/style-guide/code-layout-and-formatting |website=PowerShell Practice and Style |access-date=17 October 2021}}</ref> |- | Python || {{No}} || PEP 8: 4 spaces<ref>{{cite web|url=https://www.python.org/dev/peps/pep-0008/#indentation|title=PEP 8 – Style Guide for Python Code|website=Python.org|access-date=3 September 2017}}</ref> |- | Ruby || {{Yes}} || Ruby style guide: 2 spaces <ref>{{cite web|url=https://github.com/bbatsov/ruby-style-guide|title=ruby-style-guide: A community-driven Ruby coding style guide|first=Bozhidar|last=Batsov|date=3 September 2017|access-date=3 September 2017|website=Github.com}}</ref> |- | Rust || {{Yes}} || Rust style guide: 4 spaces <ref>{{cite web|url=https://github.com/rust-lang/rust|title=rust: A safe, concurrent, practical language|date=3 September 2017|website=Github.com|access-date=3 September 2017}}</ref> |- | Scala || {{Yes}} || Scala style guide: 2 spaces <ref>{{cite web|url=http://docs.scala-lang.org/style/indentation.html|title=Indentation – Scala Documentation|website=docs.scala-lang.org|access-date=3 September 2017}}</ref> |- | Swift || {{Yes}} || Google uses 2 spaces<ref>{{cite web |title=Swift Style Guide |url=https://google.github.io/swift/ |website=google.github.io |access-date=17 October 2021}}</ref> |- | Tcl || {{Yes}} || Tcl style guide: 4 spaces <ref>{{cite web|url=http://www.tcl.tk/doc/styleGuide.pdf|title=Tcl Style Guide|author=Ray Johnson|website=Tcl.tk|access-date=3 September 2017}}</ref> |- | Visual Basic || {{Yes}} || Microsoft convention: 4 spaces <ref>{{cite web|url=http://msdn.microsoft.com/en-us/library/aa733583(v=vs.60).aspx|title=Structured Coding Conventions|website=msdn.microsoft.com|date=4 January 2007 |access-date=3 September 2017}}</ref> |- | XML || {{Yes}} || Google uses 2 spaces<ref>{{cite web |title=Google XML Document Format Style Guide |url=https://google.github.io/styleguide/xmlstyle.html |website=google.github.io |publisher=Google |access-date=17 October 2021}}</ref> |}
=== Elastic tabstops ===
In 2006, a new method of indentation was proposed, called elastic tabstops.<ref>{{cite web |url = https://nick-gravgaard.com/elastic-tabstops/ |first = Nick | last = Gravgaard |title = Elastic tabstops - a better way to indent and align code |access-date = 2025-12-03 |date = 2006-07-02 |quote = The simple solution is to redefine how tabs are interpreted by the text editor. Rather than saying that a tab character places the text that follows it at the next Nth column, we should say that a tab character is a delimiter between table cells in a manner more reminiscent of how they're used in tab separated value (TSV) files. }}</ref><ref>{{cite web |url = http://nickgravgaard.com/elastictabstops/ |first = Nick | last = Gravgaard |title = Elastic tabstops - the solution to the tabs-versus-spaces issue |date = 2006-07-02 |archive-url = https://web.archive.org/web/20060719102209/http://nickgravgaard.com/elastictabstops/ |archive-date = 2006-07-19 |access-date = 2025-12-03 |quote = The solution then is to redefine how tabs are interpreted by the text editor. Rather than saying that a tab character (a "hard tab") will move the cursor until the cursor's position is a multiple of N characters, we should say that a tab character is a delimiter between table cells in a similar manner to how they're used in tab separated value (TSV) files. }}</ref><ref>{{cite web | url = https://old.reddit.com/r/programming/comments/77dpp/elastic_tabstops_a_better_way_to_indent_and_align/ | title = Elastic tabstops - a better way to indent and align code (Reddit comment thread) | date = 2008-10-15 | access-date = 2025-12-03 | author = cvk }} 111 comments.</ref><ref>{{cite web | url = https://github.com/tenbillionwords/spaceship-mode?tab=readme-ov-file#tabble-mode | title = spaceship-mode and tabble-mode: text/code alignment for variable-width fonts | first = Plasma S. | last = Messick | date = 2021-05-27 | access-date = 2025-12-03 | quote = Using tabble-mode, you can use tabs to align text in other places than the left edge. tabble-mode is essentially an implementation of elastic tabstops. }}</ref>
==References== {{Reflist}}
{{Commons cat|Indentation (typesetting)}}
Category:Typography Category:Source code