Smart Apps Pro

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

  1. Decide on tabs or spaces.
  2. Select the affected lines and re-indent them the same way.
  3. 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.

Get Notepad+ Text Editor →

Notepad+ Text Editor app icon

Notepad+ Text Editor

A fast, advanced text editor for notes and code, with emoji support

Get the app

More about Notepad+ Text Editor