Configure Angular CLI to use tabs instead of spaces

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):

Shell

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:

Shell

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:

Shell

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).

Plain Text
.editorconfig

Next, the tslint.json file determines the linting options for TypeScript.

JSON
tslint.json

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.

JSON
angular.json

Now, when you generate new items using CLI, e.g. ng generate component Home, it will be indented with tabs.

Read next

5 1 vote
Article Rating
Subscribe
Notify of
guest
6 Comments
Inline Feedbacks
View all comments
David Andersson
David Andersson
5 years ago

Has anyone got this working for Angular 9?

David Andersson
David Andersson
5 years ago
Reply to  Łukasz Nojek

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.

Anthony
Anthony
5 years ago

thanks!

Marcel
Marcel
4 years ago

How can I run the shell command on a Mac terminal?

6
0
Would love your thoughts, please comment.x
()
x