Eğitim / Kodlama 19 Mart 2024

How To Use Prettier in Visual Studio Code

How To Use Prettier in Visual Studio Code

Nowadays, every tech company strives to build quality software fast. That’s why every developer must learn how to write clean and readable code.

But when a project is managed by multiple developers, the focus shifts into consistency especially in terms of written code.

Keeping a consistent code style and formatting across many team members and project is a challenging task. It’s almost impossible to do it manually, but that’s where Prettier comes into play.

In this guide, you will learn how to install Prettier in Visual Studio Code and how to use it to format code.

Prerequisites

Before you follow this guide, you will need to download and install Visual Studio Code.

What is Prettier?

Prettier is a powerful code formatter that automates this process from start to finish. It gives you confidence that your code adheres to defined coding standards without any manual actions (unless you want to have it manual).

Prettier not only supports all JavaScript libraries and frameworks, such as Angular, React, Vue, and Svelte, but also works with TypeScript.

That’s why it is used by many people in tech worldwide.

How To Install Prettier in Visual Studio Code

To install Prettier in Visual Studio Code, you need to:

  1. Open the Extensions tab.
  2. Type prettier in the search box.
How To Use Prettier in Visual Studio Code
Visual Studio Code / Extensions

At the top of the list you will find the Prettier – Code formatter extension. You need to open it, and click the Install button:

How To Use Prettier in Visual Studio Code
Visual Studio Code / Extensions / Prettier – Code Formatter 

After the successful installation you will see the text saying “This extension is enabled globally”:

How To Use Prettier in Visual Studio Code
Visual Studio Code / Extensions / Prettier – Code Formatter (Installation completed)

How To Activate Prettier in Visual Studio Code

When your Prettier extension is installed, you need to configure Visual Studio Code to take advantage of it. You can do in the Settings tab.

Side node: to open the Settings tab, you can use COMMAND + , on macOS or CTRL + , on Windows and Linux:

How To Use Prettier in Visual Studio Code
Visual Studio Code / Main view

At the top of the Settings tab you will find a search box. Now, you need to type formatter, and then Editor: Default Formatter will pop up on the settings list:

How To Use Prettier in Visual Studio Code
Visual Studio Code / Settings

Now, open the dropdown and select Prettier – Code formatter from the list:

How To Use Prettier in Visual Studio Code
Visual Studio Code / Settings / Default Formatter

Now, Prettier is your default code formatter, but you might want to enable Visual Studio Code to automatically format code when you save files.

If you want to, just tick the checkbox in the Format On Save section:

How To Use Prettier in Visual Studio Code
Visual Studio Code / Settings / Format On Save

How To Format Code with Prettier in Visual Studio Code

Let’s take a look at one React component I created:

How To Use Prettier in Visual Studio Code
Visual Studio Code / Unformatted React 18 Component 

As you can see, this code is completely misaligned, it misses semicolons, and it is very difficult to read. The code could formatted in a better way, right? Here’s where Prettier comes into play.  

To format code, we need to open the command palette – you can use COMMAND + SHIFT + P on macOS or CTRL + SHIFT + P on Windows and Linux.

Now, you need to find Format Document. Feel free to use the search box:

How To Use Prettier in Visual Studio Code
Visual Studio Code / Format Document command

After running Format Document your code becomes neat and clean:

How To Use Prettier in Visual Studio Code
Visual Studio Code / Formatted React 18 Component (with Prettier)

Conclusion

Integrating Prettier into Visual Studio Code is a game-changer for developers striving to maintain a consistent and high-quality codebase.

By automating the formatting process, you are not only adhering to coding standards but are also reducing the struggle that comes with manual code formatting. That’s why every developer should use Prettier to ensure consistency in their codebase.

I hope this article helped you a lot. It’d mean the world to me if you shared it on your social media.

If you have any questions you can reach me on Twitter.

Learn React

Looking for a practical course to learn React?

🚀 Join my React 18 Course on Udemy.

This course includes:

  • 🎥 5.5 hours on-demand video
  • 📱 Access on mobile and TV
  • 🗓️ Full lifetime access
  • 🎓 Certificate of completion

Click below to enroll.

How To Use Prettier in Visual Studio Code
Click to get started


source

Spread the love <3

You may also like...

Eyl
23
2024
0

Redmi Watch 5 Lite tanıtım tarihi açıklandı! İşte özellikleri

Xiaomi, alt markası Redmi ile yükselen bir grafiğe büründü. Ülkemizde de sıkı takipçisi olan Redmi ismi, akıllı telefonların yanı sıra...

Spread the love <3
Mar
28
2024
0
Mercedes G500’ü suda yürüttüm!

Mercedes G500’ü suda yürüttüm!

Mercedes-Benz’in efsanevi SUV modeli olan G-Serisi, 1979 yılından bu yana üretimde olan ve dünya çapında büyük bir hayran kitlesi bulunan...

Spread the love <3
May
03
2024
0
Intel, yapay zekayı masaüstüne getiriyor! İşte yeni Core Ultra serisi

Intel, yapay zekayı masaüstüne getiriyor! İşte yeni Core Ultra serisi

Intel geçtiğimiz aylarda tanıttığı Core Ultra işlemcileriyle yeni bir neslin önünü açtı. Bu seriyle birlikte yapay zeka dönemini de başlatan...

Spread the love <3
Tem
28
2024
0

Menzil 756 km’ye çıktı! Tesla yeni pil paketini duyurdu

Tesla, Cybertruck kullanıcılarına güzel ama bir o kadar da tuzlu bir haberle geldi Artık 16 bin dolara Cybertruck araçlara menzil...

Spread the love <3
Whatsapp İletişim
Merhaba,
Size nasıl yardımcı olabilirim ?