Tabs or Spaces? Indentation in Plain Text and Code
Why indentation matters, the difference between tabs and spaces, and how to avoid mixed-indentation problems when editing files.
Indentation, the blank space at the start of a line, helps you read structure. In some formats it also carries meaning. Understanding tabs and spaces avoids frustrating errors.
What is indentation for?
- Readability: indented lines show which items belong together.
- Structure: in some formats, such as YAML and Python, indentation is part of the syntax.
Tabs versus spaces
| Tab | Spaces | |
|---|---|---|
| What it is | a single tab character | several space characters |
| Width | set by the editor, often shown as 4 or 8 spaces | fixed |
| Looks the same everywhere? | no; it changes with settings | yes |
The age-old debate has no universal winner. Different projects and languages prefer different styles.
Which should you use?
- Follow the file or project. If existing lines use spaces, use spaces.
- Follow the language's convention. For example, Python commonly uses four spaces, and YAML requires spaces.
- Be consistent. Don't mix them within one file.
Mixed indentation problems
If some lines use tabs and others use spaces, files can look neat in one editor and messy in another. In Python and YAML, mixing can cause errors.
How to spot it
- Lines look misaligned when you switch editors or text size.
- A program reports an indentation error.
- Moving the cursor shows it jumping different distances.
How to fix it
- Decide on tabs or spaces.
- Select the affected lines and re-indent them the same way.
- Check every line in the block.
Indentation in ordinary notes
In plain notes, indentation helps with outlines:
Shopping
Fruit
apples
pears
Dairy
milk
Spaces or tabs both work, but be consistent so the outline stays neat when you view it elsewhere.
Tips for phones
On a touch keyboard, typing several spaces can be slow. If your editor has a tab key or supports it, use that to indent quickly.
The takeaway
Choose one style, follow the project, and keep it consistent.
Notepad+ Text Editor
A fast, advanced text editor for notes and code, with emoji support
More about Notepad+ Text Editor
Editing Config Files on Your Phone: Doing It Carefully
What configuration files are, the common formats, and sensible precautions before making a quick edit.
Read article →Editing README Files and Project Notes
What a README is, what belongs in one, and how to keep project notes tidy in plain text.
Read article →JSON Basics: The Punctuation That Breaks Files
A beginner's guide to reading and editing JSON files, the rules for commas, quotes and brackets, and the mistakes that cause errors.
Read article →Quick HTML Edits: Fixing a Web Page from Your Phone
How HTML is structured and how to make small, safe changes to a page in a text editor, such as fixing a typo or a link.
Read article →