Configure Angular CLI to use tabs instead of spaces
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.
Introduction
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.
Note: I'll soon be sharing short, practical tips on Angular — a good way to pick up something new if you're interested.
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 .js
, .ts
, .html
, .css
, .scss
or .json
and their path does not contain any of:
/.git/
/node_modules/
package-lock.json
You can add more rules if necessary. It then converts the initial spaces and converts every two spaces (-t 2
) to a tab.
Warning
Run the command first without the rm
and 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.
First, the .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).
Next, the tslint.json
file determines the linting options for TypeScript.
And, finally, 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.
Has anyone got this working for Angular 9?
Hi, what was your problem with Angular 9? I have just followed the steps and it worked.
Thanks for the reply. I tried step 3, to configure Angular to use tabs. I modified .editorconfig, tslint.json and angular.json. But when I generated a component it had two spaces for indentation in the file.
I noted that the “schematics” property in angular.json didn’t have any content so I copied your code.
I used
ng
version 9.0.2.My
angular.json
in version 9 had only one schematics:@schematics/angular:component
, but that’s because I created the project with SCSS styles.I made several further tests:
* I did only step 1 & 3
* I closed the IDE and used only the terminal
* I added all the schematics from the example above
and still the generated Home component had tabs
I got tabs only if I removed all schematics and left the property empty:
"schematics": { },
Maybe your IDE/editor replaces the characters? Maybe you have an other linter that changes the tabs? Maybe you have outdated
ng
? You can always try with an empty projectthanks!
How can I run the shell command on a Mac terminal?