226

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?

starball
  • 20,030
  • 7
  • 43
  • 238
Zach Lysobey
  • 14,959
  • 20
  • 95
  • 149
  • See also: https://stackoverflow.com/questions/36814642/visual-studio-code-convert-spaces-to-tabs – Trutane Oct 06 '21 at 01:11

8 Answers8

380

Since fix of: https://github.com/Microsoft/vscode/issues/1228 the editor supports it out of the box. Simply go for:

  1. F1,
  2. indentationToSpaces or indentationToTabs (depending on your need)
  3. Enter.
Peter Butkovic
  • 11,143
  • 10
  • 57
  • 81
  • 51
    This only converts leading spaces/tabs. – Nigel Scott May 19 '17 at 09:28
  • 27
    For step 1 you could also do `ctrl+shift+p` – Alex Jul 04 '18 at 04:20
  • 10
    None 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
    Is there a way to do this for ALL files in a project? – a06e Dec 04 '21 at 18:29
  • @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
114

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. enter image description here

maximum
  • 1,303
  • 1
  • 8
  • 9
82

If you are trying to convert non-leading tabs to spaces (or vice versa) you can use a regex search and replace.

  1. Press CTRL + H
  2. Click the .* button to search using regular expressions.
  3. To search for tabs enter [\t] in Find box.
  4. Enter spaces in Replace box and perform your replace.

Search box in regex mode: screenshot of VSCode regex search box

Trutane
  • 1,279
  • 12
  • 12
jrupe
  • 1,399
  • 1
  • 11
  • 11
11

To round out these answers, I will add my take for converting each tab to n spaces.

  1. Highlight a tab character
  2. Use CTRL + F2 select all occurrences
  3. 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.

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

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
Alexei Z
  • 79
  • 1
  • 3
  • 5
    Just 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
0

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}, where N 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.

starball
  • 20,030
  • 7
  • 43
  • 238
0

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:

  1. Replace ^(([^\t]*\t+)?([^\t]{4})*?[^\t]{0})\t with $1 (4 trailing spaces).
  2. Replace ^(([^\t]*\t+)?([^\t]{4})*?[^\t]{1})\t with $1 (3 trailing spaces).
  3. Replace ^(([^\t]*\t+)?([^\t]{4})*?[^\t]{2})\t with $1 (2 trailing spaces).
  4. 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:

  1. Replace ^(([^\t]*\t+)?([^\t]{4})*?[^\t]{0})\t with $1 (2 trailing spaces).
  2. Replace ^(([^\t]*\t+)?([^\t]{4})*?[^\t]{1})\t with $1 (1 trailing spaces).

Replacing spaces into tabs can be done in a similar way.

ashidc
  • 21
  • 1