I cannot figure out how to do this for the life of me apart from doing a find-replace on 4 spaces and converting to tabs (Version 0.10.2). I can't think of an editor/IDE that doesn't have a specific feature to do this. Does VS Code?
-
See also: https://stackoverflow.com/questions/36814642/visual-studio-code-convert-spaces-to-tabs – Trutane Oct 06 '21 at 01:11
8 Answers
Since fix of: https://github.com/Microsoft/vscode/issues/1228 the editor supports it out of the box. Simply go for:
F1
,indentationToSpaces
orindentationToTabs
(depending on your need)Enter
.

- 11,143
- 10
- 57
- 81
-
51
-
27
-
10None of the answers converts each tab to the correct and VARYING number of spaces. For example, enter this text and then convert: Line 1: "A\tB". Line 2: "ABC\tD". – typpo Dec 12 '18 at 22:41
-
1
-
@NigelScott: I have the same question as the thread-starter. I want to convert ALL tabs in every file in my document. It appears that the above affects only indentation. Thumbs up to the comment from `becko` as well – Tom Stambaugh Feb 12 '22 at 21:53
-
Amazingly, this still isn't built into Code. I tried a load of extensions, but most of them convert tabs directly into fixed number of spaces which will break any alignment (which is the main reason you would use tabs in the first place). This one does work though: https://marketplace.visualstudio.com/items?itemName=benjaminprojas.vscode-inline-spacify – Nigel Scott Feb 14 '22 at 21:04
Another way to do it is click the current indentation (Tab/Spaces:n) on the footer which will open your indentation options where you can select what you want to do.

- 1,303
- 1
- 8
- 9
-
24
-
Does this work for converting future tabs to spaces, or only existing ones? – evanrmurphy Oct 24 '22 at 20:44
If you are trying to convert non-leading tabs to spaces (or vice versa) you can use a regex search and replace.
- Press CTRL + H
- Click the
.*
button to search using regular expressions. - To search for tabs enter
[\t]
in Find box. - Enter spaces in Replace box and perform your replace.
-
46This converts each tab to the same number of spaces, which isn't correct. – David Given Dec 12 '18 at 23:37
-
-
1This is really helpful if you want to convert a tab separated data format to comma separated. :) – Breedly May 21 '20 at 13:12
-
2This answer is flexible. If you want to convert *consecutive* tabs to a fixed number of spaces and aren't familiar with regular expressions, please change `[\t]` to `[\t]+`. – Ders Jul 16 '21 at 18:33
-
@Emmanual this answer is flexible, if you want to change programmatically , you just need to update the regex pattern – Luke Oct 13 '21 at 05:50
-
To round out these answers, I will add my take for converting each tab to n spaces.
- Highlight a tab character
- Use
CTRL
+F2
select all occurrences - Press
SPACE
n times
This is the easiest way to do this (going beyond only converting leading tabs).
Note that this does not convert consecutive tabs to k spaces. It converts each tab. For consecutive tabs please see my comment on jrupe's answer. You will need VS Code find and replace with regular expressions to accomplish that.

- 1,068
- 13
- 16
- Select Replace: CTRL-H
- Enter Horizontal Tab in Find box: hold ATL and type 009 on the keypad.
- Enter a space(or more spaces) into the Replace box: press space bar
- Press Enter to begin replacing Tabs with Space(s).

- 97
- 1
- 1
-
2This didn't work for me, but I posted an answer below on doing this for non-leading spaces/tabs using regular expressions. – jrupe Aug 07 '18 at 21:45
-
18This converts each tab to the same number of spaces, which isn't correct. – David Given Dec 12 '18 at 23:37
-
1
Fast forward to 2020/2021, there are some extensions that will give us that conversion. I have just needed that functionality (hence I found this article), and searching for extensions I found:
- geocode.spacecadet - providing both TAB->SPC and SPC->TAB, but not updated since 2017, with 1.3k installs, 3.5 review
- takumii.tabspace - TAB->SPC, from 2020, 1.5k installs, no reviews
- pygc.spacetab - SPC->TAB, from... wait, literally yesterday! (or today depending on your TZ), 2 installs, no reviews

- 79
- 1
- 3
-
5Just to follow up on this, I just downloaded [Tab To Space](https://marketplace.visualstudio.com/items?itemName=TakumiI.tabspace&ssr=false#review-details) by Takumil and it does _exactly_ what it's supposed to do. I can't believe it had almost 2000 downloads and zero ratings. I wrote a review and gave it 5 stars, because while it only does one thing, it does it absolutely perfectly. Regardless of upvotes, this is the most complete answer to OP's question. – Amandalishus Mar 03 '22 at 17:42
-
Could you please explain how the extensions are different than the built-in functionality described in other answers? Per those answers, this seems like its actually quite easy with VSCode out of the box. – Zach Lysobey Jul 12 '22 at 22:15
-
@ZachLysobey there are many different options, and most of them with the explanation. Which one are you comparing with? – Alexei Z Jul 21 '22 at 13:30
-
@AlexeiZ VSCode seems to happily convert tab <-> spaces and vice versa *without* any extensions. I'm just curious what, if any, additional value these extensions provide? – Zach Lysobey Jul 21 '22 at 20:39
-
2@Zach Lysobey VSCode doesn't convert tabs if they are not the leading characters in a new line. I guess that's the main problem for many ppl in this thread. – csiz Sep 05 '22 at 15:08
-
@csiz ah that makes sense. I'd still personally go for another approach (like regex search+replace) but maybe this is useful for some folks then – Zach Lysobey Nov 04 '22 at 15:35
For converting indentation
Use the Convert Indentation to Tabs
or Convert Indentation to Spaces
command in the command palette.
By default, VS Code will try to detect what style of indentation an editor tab is using and match that behaviour when you press the tab key. You can disable that behaviour by putting "editor.detectIndentation": false
in your settings.json file.
If you keep the indentation type detection on, you can set the indentation style for a new file with no indentation in it yet to tabs by using the Indent Using Tabs
command in the command palette, or to spaces using Indent Using Spaces
.
If you have editor.detectIndentation
set to false
, the editor.insertSpaces
setting takes effect, whose default value is true
, which causes the tab key to insert spaces instead of actual tab characters.
See also the editor.tabSize
setting (the visual width of tabs, and how many spaces to insert when editor.insertSpaces
is true
and editor.detectIndentation
is false
).
You can also configure these settings on a per-language-mode basis by wrapping them in "[<language-mode-name>]: { ... }"
blocks in your settings.json file.
You may also be interested in How to execute command across multiple files in VS Code?.
For fixed-width conversion anywhere
(emphasis on fixed-width- this is not tab-stop sensitive)
You can use the find widget (ctrl/cmd+f):
To go from tabs to spaces, either enable regex mode enabled (alt+r) and search for
\t
, or paste a tab character into the search field, and then put your number of spaces of choice in the replace field, and then press the "Replace All" button or use the keyboard shortcut shown in its tooltip.To go from spaces to tabs, enable regex mode, and search for
{1,N}
, whereN
is your desired tabstop width, and then put\t
in the replace field and Replace All. If you want a variant that preserves standalone single spaces, try putting{2,N}|((?<= {N-1}) )
in the search input instead. Again, note that this cannot preserve the exact visual spacing because of editor tabstop behaviour (tabs will be aligned to tab stops).
For replacing in multiple files, use the Search View, which has a similar interface to the find widget. You can exclude/include glob patterns to do replacement in using the extended search controls (Use the three-dot "Toggle Search Details" button).
I tried working on some more tab-stop-sensitive, vanilla-VS-Code solutions, but didn't get much headway and possibly ran into a VS Code bug. Maybe someday I'll come back to this, but I think in any project where you allow each user to choose how wide tabs are, and used or want to use tabs for non-leading-indentation, you will not get something ideal- at least- not with common tab-stop behaviours in IDEs.

- 20,030
- 7
- 43
- 238
Here's a practical solution for the correct replacement (varying amount of spaces), and not only for leading whitespaces.
Assume your tab legnth is 4 spaces do the following:
- Replace
^(([^\t]*\t+)?([^\t]{4})*?[^\t]{0})\t
with$1
(4 trailing spaces). - Replace
^(([^\t]*\t+)?([^\t]{4})*?[^\t]{1})\t
with$1
(3 trailing spaces). - Replace
^(([^\t]*\t+)?([^\t]{4})*?[^\t]{2})\t
with$1
(2 trailing spaces). - Replace
^(([^\t]*\t+)?([^\t]{4})*?[^\t]{3})\t
with$1
(1 trailing spaces).
After each replacement Press Ctrl+Alt+Enter repeatedly until no result can be found.
If your tab length is 2 spaces for example then you just need to do these:
- Replace
^(([^\t]*\t+)?([^\t]{4})*?[^\t]{0})\t
with$1
(2 trailing spaces). - Replace
^(([^\t]*\t+)?([^\t]{4})*?[^\t]{1})\t
with$1
(1 trailing spaces).
Replacing spaces into tabs can be done in a similar way.

- 21
- 1