Convert Tab Indentation to Spaces in YAML

Learn how to change tabs to spaces in YAML files without breaking indentation. Practical steps for editors, command-line tools, checks, and common fixes.

Before you start

YAML uses spaces for indentation. A tab character can make a parser reject the file or treat the structure differently than you expect. The goal is to replace existing tab characters with spaces while keeping the current nesting level intact. This process runs locally on your file or in your editing application; it does not require a hosted service. Before changing anything, close the YAML file in other programs that may write to it and keep a copy of the original file so you can restore it if the result is wrong.

Step-by-step instructions

1) Open the YAML file in an editor that can show whitespace. In many editors, enable visible whitespace in the view menu so tabs appear as a distinct marker.

2) Choose the number of spaces that should replace each tab. YAML does not require a specific width, but a setting of two spaces is common. Larger values only increase the visual distance and may change how deeply nested blocks appear.

3) Use the editor's conversion command if it is available. In some editors, a command named Expand Tabs or Convert Indentation to Spaces changes tab characters in the selection or the whole file. If the editor offers a choice, set Tab Size to the same number of spaces you chose in step 2.

4) If the editor does not have a dedicated command, use a text-processing tool that treats files as plain text. The general behavior is to take a copy of the input, produce a new file with each tab expanded, and leave the current file unchanged until you confirm the result.

5) Replace the original file only after the new file has been checked. If the tool writes to a second file, compare the two files before overwriting anything.

6) Indent newly written YAML content with spaces. In editor settings, enable Insert Spaces for the Tab key. In a plain-text tool, avoid entering tab characters because a second conversion pass may be required.

Check the output

Search for tab characters in the converted file. A regular-expression search for the vertical tab marker, often written as \t, should return no matches. A simple word search for tab will not find indentation characters.

Confirm that the nesting is still readable. The spaces must line up so that child entries are indented one level further than their parent. If a line contained a tab in the middle of a value, the converted spaces may change that value. YAML conversion tools generally affect tab characters anywhere in the file, not only at the start of a line.

The converted file should still load with a YAML parser if the original structure was valid. A valid result does not guarantee that the intended meaning is unchanged, so review the resulting indentation before using the file.

Common problems and next steps

If the YAML parser reports an indentation error after conversion, the likely cause is mixed indentation or incorrect nesting. Removing tabs can expose an existing problem that was previously hidden. Check that each mapping or sequence item uses the same number of spaces for the same level.

If a tab inside a quoted value was expanded, the value has changed. Undo the conversion and use an editor that can limit the replacement to leading indentation, or convert the file with that value protected. In YAML, indentation is structural, while spaces inside a quoted scalar are part of the data.

If the indentation looks correct but the parser still fails, confirm that the file does not contain a tab character introduced after conversion. Some editors insert a tab when auto-indent is enabled. Turn on visible whitespace and search for tab characters again.

For files with repeated indentation mistakes, align the entire document at each level rather than fixing one line at a time. Move a block down its hierarchy as a unit so that the relative depth is preserved.

If the conversion must be applied to many files, prefer the same local text-processing method for each file and verify one file before running a batch operation. This keeps the behavior consistent and reduces the chance of editing unrelated content.