{"id":414,"date":"2025-05-17T22:23:03","date_gmt":"2025-05-18T05:23:03","guid":{"rendered":"https:\/\/catbradley.io\/?p=414"},"modified":"2025-05-17T22:23:03","modified_gmt":"2025-05-18T05:23:03","slug":"automatically-format-code-on-file-save-in-visual-studio-code","status":"publish","type":"post","link":"https:\/\/catbradley.io\/?p=414","title":{"rendered":"Automatically Format Code On File Save in Visual Studio Code"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/itsfoss.com\/content\/images\/2025\/05\/automatically-format-code-on-save.webp\" alt=\"Automatically Format Code On File Save in Visual Studio Code\" \/><\/p>\n<p>Manually formatting code can be tedious, especially in fast-paced or collaborative development environments. <\/p>\n<p>While consistent formatting is essential for readability and maintainability, doing it by hand slows you down and sometimes leads to inconsistent results across a project.<\/p>\n<p>In this article, I\u2019ll walk you through the steps to configure Visual Studio Code to automatically format your code each time you save a file. <\/p>\n<p>We&#8217;ll use the VS Code extension called <a href=\"https:\/\/prettier.io\/\" rel=\"noreferrer\">Prettier<\/a>, one of the most widely adopted tools for enforcing code style in JavaScript, TypeScript, and many other languages. <\/p>\n<p>By the end of this guide, you&#8217;ll have a setup that keeps your code clean with zero extra effort.<\/p>\n<h2>Step 1: Install Prettier extension in VS Code<\/h2>\n<p>To start, you&#8217;ll need the <strong>Prettier &#8211; Code Formatter<\/strong> extension. This tool supports JavaScript, TypeScript, HTML, CSS, React, Vue, and more.<\/p>\n<p>Open VS Code, go to the <strong>Extensions<\/strong> sidebar (or press <code>Ctrl + Shift + X<\/code>), and search for <a href=\"https:\/\/marketplace.visualstudio.com\/items?itemName=esbenp.prettier-vscode\" rel=\"noreferrer\"><strong>Prettier<\/strong><\/a>.<\/p>\n<p>Click on <strong>Install<\/strong> and reload VS Code if prompted.<\/p>\n<figure class=\"kg-card kg-image-card\"><img decoding=\"async\" src=\"https:\/\/itsfoss.com\/content\/images\/2025\/05\/prettier-vscode-extension.png\" class=\"kg-image\" alt=\"Automatically Format Code On File Save in Visual Studio Code\" loading=\"lazy\" width=\"1229\" height=\"688\" \/><\/figure>\n<h2>Step 2: Enable format on save<\/h2>\n<p>Now that Prettier is installed, let\u2019s make it run automatically whenever you save a file.<\/p>\n<p>Open <strong>Settings<\/strong> via <code>Ctrl + ,<\/code> or by going to <strong>File &gt; Preferences &gt; Settings<\/strong>.<\/p>\n<figure class=\"kg-card kg-image-card\"><img decoding=\"async\" src=\"https:\/\/itsfoss.com\/content\/images\/2025\/05\/accessing-settings.png\" class=\"kg-image\" alt=\"Automatically Format Code On File Save in Visual Studio Code\" loading=\"lazy\" width=\"1064\" height=\"959\" \/><\/figure>\n<p>In the search bar at the top, type <code>format on save<\/code> and then Check the box for <strong>Editor: Format On Save<\/strong>.<\/p>\n<figure class=\"kg-card kg-image-card\"><img decoding=\"async\" src=\"https:\/\/itsfoss.com\/content\/images\/2025\/05\/format-on-save.png\" class=\"kg-image\" alt=\"Automatically Format Code On File Save in Visual Studio Code\" loading=\"lazy\" width=\"1391\" height=\"468\" \/><\/figure>\n<p>This tells VS Code to auto-format your code whenever you save a file, but that\u2019s only part of the setup.<\/p>\n<h2>Troubleshooting<\/h2>\n<p>If saving a file doesn\u2019t automatically format your code, it\u2019s likely due to multiple formatters being installed in VS Code. Here\u2019s how to make sure Prettier is set as the default:<\/p>\n<ul>\n<li>Open any file in VS Code and press <code>Ctrl + Shift + P<\/code> (or <code>Cmd + Shift + P<\/code> on Mac) to bring up the Command Palette.<\/li>\n<li>Type <strong>\u201cFormat Document\u201d<\/strong> and select the option that appears.<\/li>\n<\/ul>\n<figure class=\"kg-card kg-image-card\"><img decoding=\"async\" src=\"https:\/\/itsfoss.com\/content\/images\/2025\/05\/format-document-1.png\" class=\"kg-image\" alt=\"Automatically Format Code On File Save in Visual Studio Code\" loading=\"lazy\" width=\"1072\" height=\"465\" \/><\/figure>\n<ul>\n<li>If multiple formatters are available, VS Code will prompt you to choose one.<\/li>\n<\/ul>\n<figure class=\"kg-card kg-image-card\"><img decoding=\"async\" src=\"https:\/\/itsfoss.com\/content\/images\/2025\/05\/configure-formatter.png\" class=\"kg-image\" alt=\"Automatically Format Code On File Save in Visual Studio Code\" loading=\"lazy\" width=\"1110\" height=\"423\" \/><\/figure>\n<ul>\n<li>Select <strong>\u201cPrettier &#8211; Code formatter\u201d<\/strong> from the list.<\/li>\n<\/ul>\n<figure class=\"kg-card kg-image-card\"><img decoding=\"async\" src=\"https:\/\/itsfoss.com\/content\/images\/2025\/05\/multiple-formatter.png\" class=\"kg-image\" alt=\"Automatically Format Code On File Save in Visual Studio Code\" loading=\"lazy\" width=\"1139\" height=\"342\" \/><\/figure>\n<p>Now try saving your file again. If Prettier is correctly selected, it should instantly reformat the code on save.<\/p>\n<p>In some cases, you might want to save a file <em>without<\/em> applying formatting, for example, when working with generated code or temporary formatting quirks. To do that, open the Command Palette again and run <strong>\u201cSave Without Formatting.\u201d<\/strong><\/p>\n<figure class=\"kg-card kg-image-card\"><img decoding=\"async\" src=\"https:\/\/itsfoss.com\/content\/images\/2025\/05\/save-without-format-1.png\" class=\"kg-image\" alt=\"Automatically Format Code On File Save in Visual Studio Code\" loading=\"lazy\" width=\"1135\" height=\"411\" \/><\/figure>\n<h2>Optional: Advanced configuration<\/h2>\n<p>Prettier works well out of the box, but you can customize how it formats your code by adding a <code>.prettierrc<\/code> configuration file at the root of your project.<\/p>\n<p>Here\u2019s a simple example:<\/p>\n<pre><code class=\"language-json\">{\n  \"singleQuote\": true,\n  \"trailingComma\": \"es5\",\n  \"semi\": false\n}\n<\/code><\/pre>\n<p>This configuration tells Prettier to use single quotes, add trailing commas where valid in ES5 (like in objects and arrays), and omit semicolons at the end of statements.<\/p>\n<p>There are many other options available such as adjusting print width, tab width, or controlling how JSX and HTML are handled.<\/p>\n<p>You can find the full list of supported options in <a href=\"https:\/\/prettier.io\/docs\/configuration\" rel=\"noreferrer\">Prettier\u2019s documentation<\/a>, but for most projects, a few key settings in <code>.prettierrc<\/code> go a long way.<\/p>\n<h2>Try It Out<\/h2>\n<p>Create or open any file, JavaScript, TypeScript, HTML, etc. Add some poorly formatted code. <\/p>\n<pre><code class=\"language-html\">&lt;html&gt;&lt;head&gt;&lt;style&gt;body{background:#fff;color:#333;font-family:sans-serif}&lt;\/style&gt;&lt;\/head&gt;&lt;body&gt;&lt;h1&gt;Hello&lt;\/h1&gt;&lt;script&gt;document.querySelector(\"h1\").addEventListener(\"click\",()=&gt;{alert(\"Hello World!\")})&lt;\/script&gt;&lt;\/body&gt;&lt;\/html&gt;\n<\/code><\/pre>\n<figure class=\"kg-card kg-image-card\"><img decoding=\"async\" src=\"https:\/\/itsfoss.com\/content\/images\/2025\/05\/html-code-unformatted-1.png\" class=\"kg-image\" alt=\"Automatically Format Code On File Save in Visual Studio Code\" loading=\"lazy\" width=\"1268\" height=\"374\" \/><\/figure>\n<p>Then simply save the file (<code>Ctrl + S<\/code> or <code>Cmd + S<\/code>), and watch Prettier instantly clean it up.<\/p>\n<figure class=\"kg-card kg-image-card\"><img decoding=\"async\" src=\"https:\/\/itsfoss.com\/content\/images\/2025\/05\/saving-unformatter-html-code-file.gif\" class=\"kg-image\" alt=\"Automatically Format Code On File Save in Visual Studio Code\" loading=\"lazy\" width=\"800\" height=\"415\" \/><\/figure>\n<p>As you can see, Prettier neatly indents and spaces each part of the html code, even across different embedded languages.<\/p>\n<h2>Wrapping Up<\/h2>\n<p>It doesn&#8217;t matter if <a href=\"https:\/\/itsfoss.com\/vibe-coding-editors\/\" rel=\"noreferrer\">you are vibe coding<\/a> or doing everything on your own, proper formatting is a sign of writing good code.<\/p>\n<p>We\u2019ve already covered the fundamentals of writing clean, consistent code &#8211; <a href=\"https:\/\/itsfoss.com\/vs-code-indentation\/\" rel=\"noreferrer\">indentation<\/a>, spacing, and <a href=\"https:\/\/itsfoss.com\/vs-code-word-wrap\/\" rel=\"noreferrer\">word wrap<\/a>, and automatic formatting builds directly on top of those fundamentals.<\/p>\n<p>Once configured, it removes the need to think about structure while coding, letting you focus on the logic.<\/p>\n<p>If you&#8217;re also wondering how to actually run JavaScript or <a href=\"https:\/\/itsfoss.com\/vs-code-run-html\/\" rel=\"noreferrer\">HTML inside VS Code<\/a>, we&#8217;ve covered that as well, so check those guides if you&#8217;re setting up your workflow from scratch.<\/p>\n<p>If you\u2019re not already using automatic formatting, it\u2019s worth making part of your workflow. <\/p>\n<p>And if you use a different tool or approach, I\u2019d be interested to hear how you\u2019ve set it up, let us know in the comments. \ud83e\uddd1\u200d\ud83d\udcbb<\/p>","protected":false},"excerpt":{"rendered":"<p>Manually formatting code can be tedious, especially in fast-paced or collaborative development environments. While consistent formatting is essential for readability and maintainability, doing it by hand slows you down and&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"rop_custom_images_group":[],"rop_custom_messages_group":[],"rop_publish_now":"initial","rop_publish_now_accounts":[],"rop_publish_now_history":[],"rop_publish_now_status":"pending","footnotes":""},"categories":[1],"tags":[],"class_list":["post-414","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-rss"],"_links":{"self":[{"href":"https:\/\/catbradley.io\/index.php?rest_route=\/wp\/v2\/posts\/414","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/catbradley.io\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/catbradley.io\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/catbradley.io\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/catbradley.io\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=414"}],"version-history":[{"count":0,"href":"https:\/\/catbradley.io\/index.php?rest_route=\/wp\/v2\/posts\/414\/revisions"}],"wp:attachment":[{"href":"https:\/\/catbradley.io\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=414"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/catbradley.io\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=414"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/catbradley.io\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=414"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}