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
Drafting Emails in a Text Editor First
Why drafting important emails in a separate text editor prevents accidental sends, improves clarity and helps you write calmer messages.
Read article →Keeping a CV in Plain Text for Easy Updates
Maintain a master CV in a plain-text file so you can update it anywhere, tailor it quickly and paste it cleanly into online application forms.
Read article →Keeping a Work Diary for Performance Reviews
Record your achievements, feedback and challenges throughout the year in a simple text file, so performance reviews and promotions are easier to prepare.
Read article →Outlining a Novel in a Text Editor
Use a plain-text outline to plan characters, chapters and plot for a novel — flexible, searchable and easy to rearrange.
Read article →