14

It's weird that Google did not bring up an answer to this question:

I am using Visual Studio 1.56.0 (user setup) and would like to configure an editor to input the appropriate number of spaces when I press the tab key, instead of inserting a physical tab character.

I found some discussion on this topic, and the suggestions I found included to use one of:

Tools->Options->Text Editor->All Languages->Tabs

Preferences -> Source Code -> Code Formatting

Edit->Advanced->Set Indentation ->Spaces

None of these solutions work, becaue

  • I don't see a menu called "Tools" (I just have File, Edit, Selection, View, Go, Run, Terminal and Help)

  • I have an item "Preferences" under the "File" Menu, but this in turn does not have an entry called "Source Code".

  • There is no item "Advanced" under the "Edit" menu.

A (for me) logical place to find such an option would be File/Preferences/TextEditor/Formatting, but there is no tabs->space option in that section.

UPDATE, based on the answer given by harrymc :

I understand that Visual Studio Code (what I use) is a different text editor from Visual Studio.

Interestingly, the link to User and Workspace Settings shows a screenshot which looks different from what my Visual Studio Code shows: In the website, Settings should be available for either "User" or "Workspace" (there are two tabs of this name visible in the screenshot on that side), while in my editor, the Settings page shows only "User". However I don't think that this is related to my problem, because when I choose "Commonly Used" in these User-Settings and scroll down, I indeed see a configuration item named Editor: Insert Spaces with the description

Insert Spaces when pressing Tab

This should be exactly the option I was looking for. However, it does have the checkmark set, but my files still get a tab character.

I notice that next to this settings we can find the remark: This setting is overridden based on the file contents when "Editor: Detect Indentation" is on. Indeed, Detect Indentation has a checkmark too, and I unchecked it.

I thought that this should settle it, but it does not. To repeat: I know have

Detect Indentation:  unchecked
Insert Spaces:  checked

I now created a new file and typed a tab and some other characters, and saved the file. I could see that the new file also has a physical tab in it.

1 Answer 1

14

You are using Visual Studio Code and not Visual Studio, which is why the advice you found does not pertain to you.

The post Basic Editing : Indentation says this:

VS Code lets you control text indentation and whether you'd like to use spaces or tab stops. By default, VS Code inserts spaces and uses 4 spaces per Tab key. If you'd like to use another default, you can modify the editor.insertSpaces and editor.tabSize settings.

"editor.insertSpaces": true,
"editor.tabSize": 4,

Sometimes tabs are not detected when pressed if the following setting is not set:

 "editor.detectIndentation": true

For more information, see User and Workspace Settings.

Note that existing files are not automatically converted once these settings are changed, since they only affect future presses of the Tab key.

Comment by user megamorf :

Sometimes VSCode keeps old settings for the currently opened files. In that case open the file and in the bottom right corner click on Spaces and select Convert Indentation to Spaces.

4
  • While this clarified a bit of my problem, it surprisingly does not solve it. Since the explanation of this problem is too lengthy for a comment, I have placed it in my question (see the part saying UPDATE). Maybe you could have a look ..... Commented Jun 9, 2021 at 13:55
  • 2
    This is the correct answer. Sometimes VSCode keeps old settings for the currently opened files. In that case open the file and in the bottom right corner click on Spaces and select Convert Indentation to Spaces
    – megamorf
    Commented Jun 9, 2021 at 14:14
  • I have added a part about editor.detectIndentation.
    – harrymc
    Commented Jun 9, 2021 at 17:19
  • @megamorf Thanks for the comment as that fixed it for me. You should put it in an answer.
    – Scooter
    Commented Dec 16, 2023 at 5:46

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .