Converters

Indentation Converter

Convert between tabs and spaces, or switch between 2-space and 4-space indentation.

Advertisement

The Eternal Debate: Spaces vs Tabs

Few topics in software development spark as much debate as indentation style. The choice between tabs and spaces, and between 2-space and 4-space widths, has generated endless discussion in forums, conference talks, and team meetings. While the technical difference is trivial, the consistency impact is enormous — and that is where an indentation converter becomes essential.

Tabs offer theoretical advantages: each developer can configure their preferred display width, files are slightly smaller, and screen readers can interpret tabs as single navigation units for accessibility. Spaces offer consistency: the file looks identical in every editor, every diff tool, and every code review interface, regardless of local settings. Most style guides and large projects have settled on spaces for this predictability.

The 2-space versus 4-space question largely breaks down by language and ecosystem. JavaScript, Ruby, and HTML/CSS communities overwhelmingly prefer 2 spaces. Python, Java, C#, and C++ communities typically use 4 spaces. Go is the notable exception — the language enforces tabs via gofmt, ending the debate entirely for Go projects. When contributing to an existing project, following its established convention is more important than personal preference.

  • Tabs: configurable width, smaller files, accessibility benefits
  • Spaces: consistent display everywhere, preferred by most style guides
  • 2 spaces: JavaScript, Ruby, HTML/CSS ecosystems
  • 4 spaces: Python, Java, C#, C++ ecosystems
  • Follow existing project conventions when contributing

The Problem of Mixed Indentation

Mixed indentation — files that contain both tabs and spaces, or lines indented to different depths — is one of the most common formatting problems in collaborative software development. It causes visible misalignment, creates noisy diffs, and occasionally introduces logic errors in whitespace-sensitive languages like Python and YAML.

The visible misalignment is the most obvious symptom. When one developer uses tabs set to 4 spaces and another uses actual 4 spaces, lines that should align vertically appear offset in editors, diff viewers, and code review interfaces. What looks perfectly aligned on the original author screen may look broken on a teammate screen, creating confusion and apparent errors where none exist.

In Python, mixed indentation is a syntax error. The language treats tabs and spaces as distinct indentation characters, and a file that mixes them may fail to parse with an `IndentationError` or, worse, execute with unintended logic if the visual alignment does not match the logical block structure. YAML is similarly sensitive — inconsistent indentation causes parsing failures that can be difficult to diagnose.

Even in languages where indentation is purely stylistic, mixed indentation creates noise in version control. A diff that shows dozens of whitespace-only changes obscures the actual logic changes and makes code review slower and more error-prone. Cleaning up mixed indentation before committing keeps diffs focused on meaningful changes.

  • Visible misalignment across different editors
  • Syntax errors in Python and YAML
  • Unintended logic in whitespace-sensitive code
  • Noisy diffs that obscure meaningful changes
  • Reduced readability and maintainability

How Our Indentation Converter Works

Our Indentation Converter is a client-side tool that analyzes the leading whitespace of every line in your code and transforms it according to your chosen settings. It handles the four most common conversion scenarios: spaces to tabs, tabs to spaces, 2 spaces to 4 spaces, and 4 spaces to 2 spaces.

When converting spaces to tabs, the tool groups leading spaces into tab-width units and replaces each group with a single tab character. Partial groups — spaces that do not make a complete tab width — are left as spaces to preserve alignment that does not fall on tab boundaries. This preserves the visual structure while minimizing space usage.

When converting tabs to spaces, each leading tab is replaced with the configured number of spaces. The tool respects nested indentation levels, ensuring that a line indented with two tabs becomes a line indented with eight spaces (when using 4-space tabs), not just four spaces appended to the original tabs.

When converting between 2-space and 4-space indentation, the tool scales the indentation depth proportionally. A line with two levels of 2-space indentation (4 spaces total) becomes one level of 4-space indentation (4 spaces total). This is useful when migrating code between projects with different conventions or when refactoring to match a newly adopted style guide.

  • Convert spaces to tabs with boundary-aware grouping
  • Convert tabs to spaces with configurable width
  • Switch between 2-space and 4-space indentation
  • Preserves partial indentation for alignment
  • Processes entire files instantly in the browser

Practical Conversion Scenarios

Indentation conversion needs arise frequently in real-world development workflows. Recognizing these scenarios helps you apply the tool at the right time and avoid common pitfalls.

Adopting a new style guide is a common trigger. When a team decides to switch from tabs to spaces, or from 4 spaces to 2 spaces, every file in the repository needs updating. Doing this manually across hundreds of files is impractical. Our tool lets you convert individual files or paste snippets for immediate cleanup during the transition period.

Contributing to open source often requires reformatting your code. If you write code with your preferred 4-space tabs but the project you are contributing to uses 2 spaces, you need to convert before submitting a pull request. Tools like prettier and eslint can automate this, but a quick paste into our converter handles one-off snippets and files outside your normal toolchain.

Merging code from multiple sources frequently produces mixed indentation. A file copied from a Stack Overflow answer, a gist, or a different project may use a different indentation style than your own. Converting the pasted code before integrating it ensures consistency and prevents the gradual accumulation of mixed formatting.

Preparing code for presentation or documentation sometimes requires adjusting indentation. Code snippets in blog posts often need narrower indentation to fit within content columns. Converting 4-space indentation to 2 spaces before embedding in a blog template keeps lines from wrapping awkwardly.

  • Adopt new team style guides across existing code
  • Reformat contributions to match project conventions
  • Clean up pasted code from external sources
  • Adjust indentation for presentations and documentation
  • Fix files with accidental mixed indentation

Best Practices for Consistent Indentation

While conversion tools solve immediate problems, preventing indentation inconsistencies at the source is more efficient. Establishing team conventions and automated enforcement eliminates the need for reactive cleanup.

Use EditorConfig to define and enforce indentation rules. An `.editorconfig` file in your repository specifies `indent_style` (tab or space) and `indent_size` for each file type. Most modern editors read this file automatically and adjust their behavior accordingly. This single configuration file eliminates the most common cause of mixed indentation: developers with different default settings.

Configure your version control system to warn about whitespace changes. Git's `core.whitespace` setting can highlight trailing spaces, tab indentation in files that should use spaces, and other common issues. Enabling these warnings makes developers aware of problems before they commit.

Add automatic formatting to your build process. Tools like Prettier for JavaScript, Black for Python, and gofmt for Go reformat code automatically according to a fixed set of rules. Running these tools in a pre-commit hook or CI pipeline ensures that all committed code follows the same indentation style, regardless of how it was originally written.

When you must convert indentation manually, verify the result visually and with automated checks. Scan the converted file for lines that look misaligned. Run your test suite to catch any syntax errors introduced by the conversion, especially in whitespace-sensitive languages.

  • Add .editorconfig to every repository
  • Configure Git whitespace warnings
  • Use automated formatters in pre-commit hooks
  • Verify converted files visually and with tests
  • Document indentation conventions in project README