2

When writing a source file in VS Code that is styled with spaces of a specific width (maybe determined by the .editorconfig file), how can I force VS Code to treat the spaces like tabs without reformatting the file?

For example, the indent width may be 4 spaces, so rather than displaying 4 spaces in my editor, I'd rather see one tab space character with a width of 4 spaces.

4
  • Essentially you want the editor to convert spaces to tabs on the active file, but convert back when you save to spaces. Commented Feb 25, 2020 at 2:24
  • @marblewraith yes Commented Feb 25, 2020 at 2:24
  • @marblewraith do you know how to do this? Commented Feb 26, 2020 at 15:39
  • I have a few thoughts, i'll split it into 2 seperate answers. Commented Feb 28, 2020 at 3:40

3 Answers 3

2

The selection aspect of space-tabulated code is supported with VSCode 1.52 (Nov. 2020) and:

Sticky Tab Stops when indenting with Spaces

If you prefer to indent your code with spaces, there is a new setting called editor.stickyTabStops, which makes VS Code treat cursor movements in leading spaces similar to tabs.

Sticky Tab Stops -- https://media.githubusercontent.com/media/microsoft/vscode-docs/7bdaf30689a2352238048150da7087cc6857ca72/release-notes/images/1_52/sticky-tab-stops.gif

Sign up to request clarification or add additional context in comments.

Comments

0

Appearance

You said vscode is:

displaying 4 spaces in my editor

I assume that means you're getting those little Interpunct dot characters coming up like this ····

If you want those to go away, so they appear more consistent with tabs, go into VScode settings (JSON) and enter the following:

"editor.renderWhitespace": "selection" 

Assuming everything else is default, both tabs and spaces should be rendered as regular whitespace. But that alone it doesn't really help, because it doesn't allow you to distinguish nested structures i.e. you can't tell how many levels of indentation you're at.

To fix that, there's 2 things.

  1. (minimum) Set indent guides explicitly in your user settings, this will render vertical lines at each indentation level, no matter if the file is using tabs or spaces:
"editor.renderIndentGuides": true 
  1. (optional extra) If you want to take it further, there are a few extensions you can try, but the one i recommend is indent-rainbow. There are lots of options for it, but i have mine config'd so after a certain level of indentation it becomes more obnoxious, because i treat it as a code smell i.e. i like to minimize how much i nest if possible.

The end result of doing all this is that tabs and spaces are rendered exactly the same way, and you can't tell the difference unless you have part of your code highlighted:

VScode spaces highlight

Vscode convert spaces to tabs

VSCode tabs highlight

2 Comments

Great stuff! How about navigating with arrow keys? Can I get spaces to act like tabs in that respect as well? (Cursor should jump to each indentation level when using left and right arrow keys)
I think this thread is what you're looking for - github.com/microsoft/vscode/issues/2798
0

Behavior

To make the behavior of indentation more consistent, the following should be in your settings if it's not already applied by default:

"editor.detectIndentation": true, "editor.insertSpaces": true, "editor.useTabStops": true 

As for this:

source file in VS Code that is styled with spaces of a specific width (maybe determined by the .editorconfig file)

I don't think this is possible, or at least not natively. You may be able to find/write an extension that can do detection based on tabsize since there is in fact a property called:

"editor.tabSize": 4, 

Not sure if this will help, but you can do selective setting overrides based on filetype, for example:

"[yaml]": { "editor.insertSpaces": true, "editor.tabSize": 2, "editor.autoIndent": "advanced" } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.