As a tab indentation enthusiast, I wanted to use an Angular project with tabs instead of 2 spaces. Unfortunately, there are several settings to change, and the schematics used by
ng CLI command supports only spaces. Despite numerous requests (e.g. here and here), Google did not decide to introduce an option to set indentation for generated files. I went through the process of making Angular project tabbed and I’ll describe it here.
The process will be much more comfortable if you remember to run
git add . && git commit after every step to easily verify (
git diff) and undo (
git checkout -- .) changes.
First, create a new project (or start with your existing project):
Reformat existing code
Next, there are two options.
One is installing and using the Prettier package for formatting code:
Verify that the indentation changed with
git diff and that no content was changed with
git diff -w (which ignores whitespace differences).
If you don’t mind minor reformatting, mainly in JSON arrays:
- "assets": [ - "src/favicon.ico", - "src/assets" - ], - "styles": [ - "src/styles.scss" - ], + "assets": ["src/favicon.ico", "src/assets"], + "styles": ["src/styles.scss"],
then leave that version.
Another possibility is using a Linux terminal or Git Bash. Open it and launch:
That command finds all files in the current folder which end in
.json and their path does not contain any of:
You can add more rules if necessary. It then converts the initial spaces and converts every two spaces (
-t 2) to a tab.
Run the command first without the
echo ... > ... lines to list the files that will be changed. If the regex is invalid, you may damage your
.git files, and therefore irreversibly destroy data in your repository!
This method shouldn’t introduce any artifacts, only the whitespaces should be changed. Verify it with
git diff and
git diff -w.
Configure the Angular project to use tabs
As many as 3 files have to be changed to start using tab indentation.
.editorconfig file that determines the setting used by your editor when you edit files. This has to be checked first (so you don’t enter spaces while editing other configuration files).
tslint.json file determines the linting options for TypeScript.
angular.json needs to have
lintFix: true added to all schematics. Note – if you have more, list them all. Previously there was just one default, then the option disappeared but was discovered as the settings you see below.
Now, when you generate new items using CLI, e.g.
ng generate component Home, it will be indented with tabs.