Skip to main content

Dev tools and workflow · Beginner · 14 min

Share indentation and line-ending rules across editors with EditorConfig

By Padding · Updated 18 Nov 2026
node:26 (Debian) · Node.js 26.13 · editorconfig-checker 7.0.0 (core v4.0.2) · Prettier 3.10.1 · Git

You will have a mixed Python, JavaScript, Makefile and Markdown repository with one .editorconfig that every editor follows, an editorconfig-checker run that passes, and a .gitattributes file that keeps Git storing LF line endings.

On this page

One person's editor inserts tabs, another's inserts four spaces, a third saves files with Windows line endings, and every pull request fills up with whitespace changes nobody meant to make. EditorConfig fixes this with one small file, .editorconfig, at the top of the repository. It says how files of each type should be indented, which line ending to use, which character set, and whether to keep trailing spaces. Most editors read it as soon as you open the project; a few need a plugin.

A file of rules only helps if something checks it, so you will also run editorconfig-checker, a command-line tool that reads the same .editorconfig and reports every file that breaks it. Finally you will add a .gitattributes file so Git agrees on line endings even for someone whose editor ignores EditorConfig.

You will practise in a throwaway repository with a Python script, some JavaScript, a Makefile and a README, with two problems planted in it. The tools run through npx, so nothing is installed globally.

Step 1 · Make a small mixed repository

Create the demo folder and start a Git repository in it:

Bash
mkdir -p ~/editorconfig-demo/web ~/editorconfig-demo/scripts
cd ~/editorconfig-demo
git init

Add the files. Some are written with printf so that tabs, trailing spaces and Windows line endings arrive exactly as intended, rather than whatever your terminal does to pasted whitespace:

Bash
cat > web/main.js <<'JS'
export function greet(name) {
  return `Hello, ${name}`;
}
JS

cat > app.py <<'PY'
def total(prices):
    return sum(prices)
PY
printf 'print(total([2, 3]))   \n' >> app.py

printf 'build:\n\tnode web/main.js\n' > Makefile

printf '# Demo\n\nFirst line with a hard break  \nE kú àárọ̀. Ẹ káàbọ̀.\n' > README.md

printf 'console.log("build");\r\nconsole.log("done");\r\n' > scripts/build.js

git add -A

The last line of app.py ends in three spaces, and scripts/build.js uses CRLF (Windows) line endings. The README line that ends in two spaces is deliberate: in Markdown, two trailing spaces make a line break. The Yorùbá greeting is there to show why the character set matters; text in Yorùbá, Igbo, Amharic or French only survives every editor and laptop intact if the whole team saves as UTF-8.

git add -A matters for the next steps: editorconfig-checker looks at the files Git tracks.

Check it worked

Bash
git ls-files
od -c scripts/build.js | head -n 2

git ls-files lists the five files. The od -c output shows \r \n at the end of each line of scripts/build.js: that \r is the carriage return a CRLF file carries.

Step 2 · Write the .editorconfig

Create .editorconfig in the repository root:

Bash
cat > .editorconfig <<'EC'
# Stop editors looking for .editorconfig files in parent folders
root = true

# Defaults for every file
[*]
charset = utf-8
end_of_line = lf
insert_final_newline = true
trim_trailing_whitespace = true
indent_style = space
indent_size = 2

[*.py]
indent_size = 4

# make requires real tab characters before recipe lines
[Makefile]
indent_style = tab

# Two trailing spaces are a line break in Markdown
[*.md]
trim_trailing_whitespace = false

# Older code that the team has not reformatted yet
[legacy/**.js]
indent_size = 4
EC
git add .editorconfig

How it is read:

  • An editor opening a file walks up from the file's folder, reading every .editorconfig it finds, until it reaches one with root = true. Putting root = true in the top file stops a stray .editorconfig in someone's home folder from leaking into the project.
  • Each [section] is a glob matched against the file's path. Every matching section applies, from top to bottom, and a later section overrides an earlier one. A Python file gets everything from [*], then indent_size = 4 replaces the 2.
  • A glob with no / in it, such as *.py, matches at any depth. A glob with a /, such as legacy/**.js, is relative to the folder the .editorconfig sits in.
  • end_of_line = lf means every line ends with a single line-feed character, which is what Linux, macOS, most tools and most CI runners expect. Windows editors that honour EditorConfig save LF too.

Check it worked

Bash
grep -c '^\[' .editorconfig

The command prints 5, one for each section. Open app.py in an editor that reads EditorConfig (see the last step) and press Enter at the end of def total(prices):; the cursor lands four spaces in. In web/main.js it lands two spaces in.

Step 3 · Check the repository with editorconfig-checker

editorconfig-checker is a single Go program. The npm package of the same name is a small wrapper that downloads the right binary for your machine the first time it runs, and passes every argument through to it. The package version (7.0.0) and the core version are separate: EC_VERSION pins the core, so every person and every CI run uses the same checks. Without it the wrapper fetches the newest core.

Bash
export EC_VERSION=v4.0.2

The export lasts for this terminal session. In a new terminal, run it again.

First ask it which files it would check, without checking them:

Bash
npx --yes editorconfig-checker@7.0.0 -version
npx --yes editorconfig-checker@7.0.0 -dry-run

-version prints the core version, 4.0.2 or v4.0.2. -dry-run lists the tracked files, including .editorconfig itself. Now run the check:

Bash
npx --yes editorconfig-checker@7.0.0

The output groups problems by file, with the line numbers and a message for each, something like this:

Text
app.py:
	3: Trailing whitespace
scripts/build.js:
	1: Wrong line endings or no final newline
	2: Wrong line endings or no final newline

The exact wording can change between core releases; what matters is which files and lines it names. The README is not reported because Markdown is allowed its trailing spaces, and the Makefile is not reported because its recipe line uses a tab, which is what its section asks for.

Check it worked

Run echo $? straight after the check. It prints 1: the checker exits with a non-zero status when it finds a problem, which is what makes it useful in CI.

Step 4 · Fix the problems the checker found

The checker can fix the problems that have one safe answer: trailing whitespace, line endings and the final newline. Indentation, line length and character set problems stay check-only, because fixing them means choosing what the code should look like. Pass the two files so nothing else is touched:

Bash
npx --yes editorconfig-checker@7.0.0 -fix app.py scripts/build.js
git add -A

If you prefer to see the change happen, tr -d '\r' < scripts/build.js > build.tmp && mv build.tmp scripts/build.js removes the carriage returns by hand on Linux and macOS.

Run the full check again:

Bash
npx --yes editorconfig-checker@7.0.0

Check it worked

The check prints nothing and echo $? prints 0. Look at the bytes again:

Bash
od -c scripts/build.js | head -n 2

The \r characters are gone; each line now ends in \n only.

Step 5 · Make Git agree with a .gitattributes file

EditorConfig only works in editors that read it. Someone on Windows with Git's core.autocrlf set, or with an editor that ignores the file, can still commit CRLF endings. .gitattributes makes Git itself normalise line endings, whatever the person's settings are:

Bash
cat > .gitattributes <<'GA'
# Store text files with LF in the repository and check them out with LF
* text=auto eol=lf

# Never touch these
*.png binary
*.jpg binary
*.woff2 binary
GA
git add .gitattributes
git add --renormalize .

text=auto lets Git decide per file whether it is text or binary, and normalises text files to LF when they are added. eol=lf also checks them out with LF on every operating system. The binary lines are a safety net for file types Git might misjudge. git add --renormalize . re-applies the new rules to files that were added before the file existed; run it once whenever you change .gitattributes.

If a file genuinely needs CRLF, such as a Windows .bat script, give it its own line: *.bat text eol=crlf.

Check it worked

Bash
git ls-files --eol

Every text file shows i/lf (as stored in the index), w/lf (as in your working folder) and attr/text=auto eol=lf. Now try to sneak a CRLF file in:

Bash
printf 'export const x = 1;\r\n' > web/extra.js
git add web/extra.js
git ls-files --eol web/extra.js

Git may warn that CRLF will be replaced by LF. The line shows i/lf w/crlf: Git stored LF even though your copy still has CRLF. Replace your copy with the stored one and look again:

Bash
rm web/extra.js
git checkout -- web/extra.js
git ls-files --eol web/extra.js
npx --yes editorconfig-checker@7.0.0

The line now shows i/lf w/lf, and the checker passes again.

Step 6 · See Prettier follow the same rules

Prettier, the JavaScript, CSS and Markdown formatter, reads .editorconfig when no .prettierrc sets the same option. It maps end_of_line to endOfLine, indent_style to useTabs, indent_size (or tab_width) to tabWidth, and max_line_length to printWidth. A .prettierrc overrides those values, so if a project has both, keep them in step or let .editorconfig be the only place they live.

Add a file under legacy/, where the .editorconfig asks for four spaces, and let Prettier print a formatted copy without changing the file:

Bash
mkdir -p legacy
printf 'function add(a,b){return a+b}\n' > legacy/add.js
npx --yes prettier@3.10.1 legacy/add.js
npx --yes prettier@3.10.1 --no-editorconfig legacy/add.js

Check it worked

The first command prints return a + b; indented by four spaces, because the [legacy/**.js] section applies. The second, with --no-editorconfig, uses Prettier's own default of two spaces. Your editor, the checker and the formatter now take their settings from one file.

Note that Prettier stops looking for .editorconfig at the project root (the folder with .git), even when root = true is missing.

Step 7 · Get every editor on the team reading it

Many editors and code hosts read .editorconfig with no plugin, including JetBrains IDEs (IntelliJ IDEA, PyCharm, WebStorm, Rider), Visual Studio, Xcode, Neovim, Vim, Emacs, Helix, Nova and BBEdit. GitHub and GitLab use it when they display files, so tab widths look right in code review.

Visual Studio Code, VSCodium, Sublime Text, Notepad++ and Eclipse need a plugin. For VS Code, install the official extension from the Extensions view, or from a terminal:

Bash
code --install-extension EditorConfig.EditorConfig

To prompt teammates to install it, list EditorConfig.EditorConfig in the project's .vscode/extensions.json, as the VS Code team setup tutorial shows.

Settings in .editorconfig win over the editor's own preferences for the files they match, so nobody needs to change their personal settings. Add the same npx check to CI so a file that slips through an editor without support is caught before it is merged. If teammates work on low-end laptops or prepaid data, the checker is a single small binary downloaded once and cached, so it costs little to run on every push.

Check it worked

Open the Makefile in your editor, put the cursor at the start of the node web/main.js line, and press Tab: your editor inserts a tab character (in VS Code the status bar shows "Tab Size" rather than "Spaces"). In app.py, Tab inserts four spaces. If either does not happen, your editor is not reading the file and needs the plugin above.

When you have finished, remove the demo:

Bash
cd ~
rm -rf ~/editorconfig-demo

Next steps

Need this done for you?

Padding does this work for growing companies, from the first diagnosis to a system that keeps working.

Platform Engineering

Get new tutorials

New tutorials, about twice a month.