I cannot figure out how to do this for the life of me apart from doing a find-replace on four 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 Visual Studio Code?
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
.
ctrl+shift+p
–
Husein becko
as well –
Unwelcome 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.
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.
[\t]
to [\t]+
. –
Tesstessa 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.
- Select Replace: Ctrl + H
- Enter a horizontal tab in the Find box: hold Alt and type 009 on the keypad.
- Enter a space (or more spaces) into the Replace box: press the space bar
- Press Enter to begin replacing Tabs with Space(s).
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
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-ID>]: { ... }"
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.
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.
I couldn't make it work in Visual Studio Code, because as Nigel Scott and David Given have posted, the Convert Indentation to Spaces command in Visual Studio Code does not convert non-leading tabs.
Additionally, a search and replace for the tab character also does not take into account how wide is the effective horizontal space occupied by the tab character and blindly replaces them with 8 (or any other configured number of) spaces each. This breaks the visual appearance of existing indentation levels.
I guess implementing this case would need an extra command or extension in Visual Studio Code.
In case a non-Visual Studio Code solution is acceptable for you, vim
can perform this conversion easily:
:set tabstop=8
:set expandtab
:retab
As suggested in the comment, I tried the Vim extension for Visual Studio Code and the above Vim commands work on it as expected too.
I am posting this because none of the existing answers worked for me and this is the top Google result for the question.
© 2022 - 2024 — McMap. All rights reserved.