{"id":335,"date":"2025-05-13T20:14:27","date_gmt":"2025-05-14T03:14:27","guid":{"rendered":"https:\/\/catbradley.io\/?p=335"},"modified":"2025-05-13T20:14:27","modified_gmt":"2025-05-14T03:14:27","slug":"handling-indentation-in-vs-code","status":"publish","type":"post","link":"https:\/\/catbradley.io\/?p=335","title":{"rendered":"Handling Indentation in VS Code"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/itsfoss.com\/content\/images\/2025\/04\/Changing-indentation-in-vscode.webp\" alt=\"Handling Indentation in VS Code\" \/><\/p>\n<p><strong>Indentation<\/strong> is how code is visually spaced. It helps define structure, scope, and readability. For example, Python <em>requires<\/em> indentation to define blocks of code. <\/p>\n<p>Other languages might not <em>require<\/em> it, but <a href=\"https:\/\/stackoverflow.com\/questions\/2472553\/why-is-there-so-much-poorly-indented-code-out-there\" rel=\"noreferrer\">messy indentation can make code really hard to read<\/a> (and debug). Common indentation styles include:<\/p>\n<ul>\n<li><strong>2 spaces<\/strong> (popular in JS, HTML, CSS)<\/li>\n<li><strong>4 spaces<\/strong> (common in Python, Java)<\/li>\n<li><strong>Tabs<\/strong> (some devs swear by them)<\/li>\n<\/ul>\n<p>VS Code lets you customize indentation per file, per language, or globally.<\/p>\n<p>Let\u2019s explore all the ways to tweak that!<\/p>\n<h2>1. Change indentation via the status bar (per-file basis)<\/h2>\n<p>This is the easiest method and perfect when you&#8217;re editing just one file.<\/p>\n<ol>\n<li><strong>Open a file<\/strong> in VS Code.<\/li>\n<li>Look at the bottom-right corner of the window. You\u2019ll see something like <code>Spaces: 4<\/code> or <code>Tab Size: 4<\/code>.<\/li>\n<\/ol>\n<figure class=\"kg-card kg-image-card\"><img decoding=\"async\" src=\"https:\/\/itsfoss.com\/content\/images\/2025\/04\/spaces-column-in-vs-code-1.png\" class=\"kg-image\" alt=\"Handling Indentation in VS Code\" loading=\"lazy\" width=\"1979\" height=\"426\" \/><\/figure>\n<ol>\n<li>Click that label, a menu pops up!<\/li>\n<\/ol>\n<p>Now, you can choose:<\/p>\n<ul>\n<li>Indent Using Tabs<\/li>\n<li>Indent Using Spaces<\/li>\n<\/ul>\n<figure class=\"kg-card kg-image-card\"><img decoding=\"async\" src=\"https:\/\/itsfoss.com\/content\/images\/2025\/04\/spaces-column-in-vs-code.gif\" class=\"kg-image\" alt=\"Handling Indentation in VS Code\" loading=\"lazy\" width=\"800\" height=\"426\" \/><\/figure>\n<p>And below that, choose how many spaces (2, 4, 8 &#8211; up to you).<\/p>\n<p>Just changing the indentation setting doesn\u2019t automatically re-indent the whole file. You\u2019ll want to reformat the document too.<\/p>\n<p>Here\u2019s how:<\/p>\n<ul>\n<li>Press <code>Ctrl + Shift + P<\/code> (Linux\/Windows) or <code>Cmd + Shift + P<\/code> (macOS).<\/li>\n<li>Type <code>Format Document<\/code> and select it.<\/li>\n<\/ul>\n<figure class=\"kg-card kg-image-card\"><img decoding=\"async\" src=\"https:\/\/itsfoss.com\/content\/images\/2025\/04\/format-document.png\" class=\"kg-image\" alt=\"Handling Indentation in VS Code\" loading=\"lazy\" width=\"1460\" height=\"491\" \/><\/figure>\n<ul>\n<li>Or use the shortcut:\n<ul>\n<li><code>Ctrl + Shift + I<\/code> on Linux<\/li>\n<li><code>Shift + Alt + F<\/code> on Windows<\/li>\n<li><code>Shift + Option + F<\/code> on macOS<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<p>Boom! The file gets prettied up with your chosen indentation.<\/p>\n<h2>2. Set global indentation in user settings<\/h2>\n<p>Want to make your indentation choice apply to <em>all new files<\/em> in VS Code? Here\u2019s how:<\/p>\n<ol>\n<li>Open Command Palette with <code>Ctrl + Shift + P<\/code> or <code>F1<\/code>.<\/li>\n<li>Type <code>Preferences: Open User Settings<\/code>.<\/li>\n<\/ol>\n<figure class=\"kg-card kg-image-card\"><img decoding=\"async\" src=\"https:\/\/itsfoss.com\/content\/images\/2025\/04\/user-settings.png\" class=\"kg-image\" alt=\"Handling Indentation in VS Code\" loading=\"lazy\" width=\"1334\" height=\"495\" \/><\/figure>\n<ol>\n<li>In the Settings UI, search for Tab Size and set it (e.g., 4).<\/li>\n<\/ol>\n<figure class=\"kg-card kg-image-card\"><img decoding=\"async\" src=\"https:\/\/itsfoss.com\/content\/images\/2025\/04\/default-indentation-tab-size.png\" class=\"kg-image\" alt=\"Handling Indentation in VS Code\" loading=\"lazy\" width=\"1566\" height=\"806\" \/><\/figure>\n<ol>\n<li>Then search Insert Spaces and make sure it\u2019s checked.<\/li>\n<\/ol>\n<figure class=\"kg-card kg-image-card\"><img decoding=\"async\" src=\"https:\/\/itsfoss.com\/content\/images\/2025\/04\/insert-spaces.png\" class=\"kg-image\" alt=\"Handling Indentation in VS Code\" loading=\"lazy\" width=\"1554\" height=\"466\" \/><\/figure>\n<p>This tells VS Code:<\/p>\n<blockquote><p>\u201cWhenever I press Tab, insert 4 spaces instead.\u201d<\/p><\/blockquote>\n<p>Also check for <strong>Detect Indentation, <\/strong>if it\u2019s ON, VS Code will override your settings based on the file content. Disable it if you want consistency across files.<\/p>\n<h2>3. Set project-specific indentation (Workspace settings)<\/h2>\n<p>Maybe you want different indentation just for one project, not globally.<\/p>\n<ol>\n<li>Open the project folder in VS Code.<\/li>\n<li>Go to the Command Palette and select <code>Preferences: Open Workspace Settings<\/code>.<\/li>\n<\/ol>\n<figure class=\"kg-card kg-image-card\"><img decoding=\"async\" src=\"https:\/\/itsfoss.com\/content\/images\/2025\/04\/toggling-workspace-settings.png\" class=\"kg-image\" alt=\"Handling Indentation in VS Code\" loading=\"lazy\" width=\"1333\" height=\"553\" \/><\/figure>\n<ol>\n<li>Switch to the <code>Workspace<\/code> tab.<\/li>\n<li>Search and set the same <strong>Tab Size<\/strong>, <strong>Insert Spaces<\/strong>, and <strong>Detect Indentation<\/strong> options.<\/li>\n<\/ol>\n<figure class=\"kg-card kg-image-card\"><img decoding=\"async\" src=\"https:\/\/itsfoss.com\/content\/images\/2025\/04\/workspace-indentation-setting.png\" class=\"kg-image\" alt=\"Handling Indentation in VS Code\" loading=\"lazy\" width=\"1274\" height=\"690\" \/><\/figure>\n<p>These get saved inside your project\u2019s <code>.vscode\/settings.json<\/code> file.<\/p>\n<p>Perfect if you want 2-space indentation in a JS project but 4 spaces in a Python project you&#8217;re working on separately.<\/p>\n<h2>4. Set indentation based on programming language<\/h2>\n<p>Now, here&#8217;s the power-user move. Let\u2019s say you want:<\/p>\n<ul>\n<li>4 spaces for Python<\/li>\n<li>2 spaces for JavaScript and TypeScript<\/li>\n<\/ul>\n<p>Easy!<\/p>\n<ol>\n<li>Open the Command Palette \u2192 <code>Preferences: Open User Settings (JSON)<\/code><\/li>\n<\/ol>\n<figure class=\"kg-card kg-image-card\"><img decoding=\"async\" src=\"https:\/\/itsfoss.com\/content\/images\/2025\/04\/user-settings-1.png\" class=\"kg-image\" alt=\"Handling Indentation in VS Code\" loading=\"lazy\" width=\"1334\" height=\"495\" \/><\/figure>\n<ol>\n<li>Add this snippet:<\/li>\n<\/ol>\n<pre><code class=\"language-json\">\"[python]\": {\n  \"editor.tabSize\": 4\n},\n\"[javascript]\": {\n  \"editor.tabSize\": 2\n},\n\"[typescript]\": {\n  \"editor.tabSize\": 2\n}\n<\/code><\/pre>\n<p>This overrides the indentation per language. <\/p>\n<figure class=\"kg-card kg-image-card\"><img decoding=\"async\" src=\"https:\/\/itsfoss.com\/content\/images\/2025\/04\/indentation-based-on-programming-language.png\" class=\"kg-image\" alt=\"Handling Indentation in VS Code\" loading=\"lazy\" width=\"1367\" height=\"694\" \/><\/figure>\n<p>You can find all language identifiers in the VS Code docs if you want to customize more.<\/p>\n<p>You can also drop this into your .vscode\/settings.json file if you want project-level overrides.<\/p>\n<h2>Bonus Tip: Convert tabs to spaces (and vice versa)<\/h2>\n<p>Already working on a file but the indentation is inconsistent?<\/p>\n<ul>\n<li>Open the Command Palette \u2192 Type <code>Convert Indentation<\/code><\/li>\n<li>Choose either:\n<ul>\n<li><code>Convert Indentation to Spaces<\/code><\/li>\n<li><code>Convert Indentation to Tabs<\/code><\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<figure class=\"kg-card kg-image-card\"><img decoding=\"async\" src=\"https:\/\/itsfoss.com\/content\/images\/2025\/04\/quick-convert-indentation.png\" class=\"kg-image\" alt=\"Handling Indentation in VS Code\" loading=\"lazy\" width=\"1262\" height=\"444\" \/><\/figure>\n<p>You can also do this from the status bar at the bottom.<\/p>\n<p>If you need to convert <em>all<\/em> tabs in the file to spaces:<\/p>\n<ol>\n<li>Press <code>Ctrl + F<\/code><\/li>\n<li>Expand the search box<\/li>\n<li>Enable <strong>Regex<\/strong> (.* icon)<\/li>\n<li>Search for <code>t<\/code> and replace it with two or four spaces<\/li>\n<\/ol>\n<h2>Wrapping up<\/h2>\n<p>Like word wrapping in VS Code, indentation may seem like a small thing, but it&#8217;s one of the cornerstones of clean, readable code. <\/p>\n<p>Whether you&#8217;re coding solo or collaborating on big projects, being consistent with indentation helps avoid annoying bugs (especially in Python!) and keeps the codebase friendly for everyone.<\/p>\n<p>VS Code makes it super easy to control indentation your way, whether you want to set it globally, per project, or even per language.<\/p>\n<p>We\u2019ll be back soon with another helpful tip in our VS Code series.<\/p>","protected":false},"excerpt":{"rendered":"<p>Indentation is how code is visually spaced. It helps define structure, scope, and readability. For example, Python requires indentation to define blocks of code. Other languages might not require it,&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-335","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\/335","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=335"}],"version-history":[{"count":0,"href":"https:\/\/catbradley.io\/index.php?rest_route=\/wp\/v2\/posts\/335\/revisions"}],"wp:attachment":[{"href":"https:\/\/catbradley.io\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=335"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/catbradley.io\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=335"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/catbradley.io\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=335"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}