Technical

Autoindent according to language in VS Code

black screen with code

I am a bit tired off manual handling of indentation, specially when working on full stack projects. Jumping two to four spaces or tabs are not cutting it any more. So out of a bit of rage, made this .editorconfig file for VS Code. It is a configuration file standard that helps maintain consistent coding styles across different programming languages. This integration of EditorConfig in VS Code ensures that developers can easily define and implement indentation settings for various programming languages within a project.

Setting Up EditorConfig: To enable autoindentation using EditorConfig in VS Code, developers need to create an .editorconfig file at the root of their project. This file acts as a top-level configuration, providing a central place to define indentation rules for different file types. The configuration includes parameters such as indent_style (e.g., space or tab) and indent_size (the number of spaces or tabs to use for indentation).

First off, to use this hack, you need to have EditorConfig extension installed in your VS Code. It’s free and painless. Once that’s done, you simply need to make a .editorconfig file in your project directory copy-paste these code in it:

Python
# Top-most EditorConfig file
root = true

[*]
indent_style = space
indent_size = 2
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true

# C and C++ files
[*.{c,cpp,h,hpp}]
indent_size = 4

# Rust files
[*.rs]
indent_size = 4

# Go files
[*.go]
indent_size = 4

# Python files
[*.py]
indent_size = 4

# JavaScript and TypeScript files
[*.{js,ts}]
indent_size = 2

# HTML files
[*.html]
indent_size = 2

# CSS and SCSS files
[*.{css,scss}]
indent_size = 2

# YAML files
[*.yaml, *.yml]
indent_size = 2

# Ruby files
[*.rb]
indent_size = 2

# Java properties files
[*.properties]
indent_size = 2

This is also highly customisable. Please change it according to your need.

You can also find the code reference in GitHub repo here.. Happy Coding 🙂

Tagged ,

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.