Retab code between tabs and spaces

Pasted code breaks the file's indent convention — a review tool rejects mixed indentation. Convert the leading whitespace in one pass.

Tools used in this guide

The job

Teams split on tabs versus spaces, and snippets copied between projects carry the wrong one. Rather than fixing lines by hand, convert the whole block's leading indentation at once.

How to do it

Paste the code, pick tabs→spaces or spaces→tabs, and set the width — the number of spaces that stand for one indent level (usually 2 or 4). The converted text is shown with a count of changed lines; copy it back over the original.

  • Spaces→tabs works in full runs: at width 4, eight leading spaces become two tabs, but six become one tab plus two leftover spaces.
  • Only the start of each line is touched — alignment between columns inside a line stays as written.
  • Empty lines and lines with no indentation pass through unchanged.

Worked example

A YAML file rejects a pasted stanza indented with tabs. Paste the stanza, run tabs→spaces at width 2, and each leading tab becomes two spaces — the linter stops complaining.

Limits

The converter does not detect which convention a file uses — check the surrounding code or .editorconfig. It also does not expand tabs that appear mid-line (those are usually alignment, not indentation, and are left alone on purpose).