Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Visual Studio: Is there a setting to delete whitespace a tab at a time?

I'm using the latest VS Community. Let's say my cursor is right here:

enter image description here

When I press delete, a space is deleted. Is there a way to make it so four spaces are deleted instead? That's the way it works in a lot of other editors, including VS Code.

like image 322
Daniel Kaplan Avatar asked Feb 22 '19 23:02

Daniel Kaplan


2 Answers

When I press delete, a space is deleted. Is there a way to make it so four spaces are deleted instead? That's the way it works in a lot of other editors, including VS Code.

Many editors including Visual Studio (VS), can be configured to show tab characters either as tabs or rendered to a fixed number of spaces, say four (4). (there are reasons for this, each with their own pros and cons - a subject for another day)

I suspect what you were seeing in VS Code (VSC) was a file that contained a tab character and VSC is showing tabs as tabs (instead of spaces) in the text editor. Hitting Delete over a tab makes it look like four spaces get deleted in one go!

However, your installation of Visual Studio however might be set to show tabs as spaces (I forget which is the default) which explains why you have to hit Delete more than once over an area of indentation.

You can confirm how tabs are shown in VS by selecting Edit.Advanced.View White Space (I like to keep it on). If you see little arrows it means a tab character is present. If you see dim period symbols its a space. In the screenshot below we can see a file with tabs rendered as spaces.

enter image description here

If tabs are rendered as spaces in VS that explains why you have to press the key four times.

You can control whether you want to always have tabs or spaces by selecting Tools.Options.Text Editor.C#.Tabs (assuming the file is C#)

enter image description here

Once this is setup "tabs" will either be tabs or 'n' number of spaces. However this only applies for new documents and VS generally doesn't auto-tabify-on-save.

Now it would be rather tedius to go through every file, manually flip everything to tabbed format so luckily VS has a rather neat feature to convert a file to tab or spaces based on your options:

  1. In Options specify whether that you want tabs
  2. open the file in question (say a C# file). Looks like the following image (yuck! spaces!):

enter image description here

  1. Select Edit.Advanced.Format Document

enter image description here

  1. document is now tabbed. Delete what you want. Repeat 2-4 as required.

enter image description here

like image 178
MickyD Avatar answered Oct 16 '22 08:10

MickyD


The only built-in way to delete spaces just like they were tabs is to shift-TAB. Otherwise there is a plug-in called TabSanity that does what you want: https://marketplace.visualstudio.com/items?itemName=jedmao.TabSanity-10817

Turning on Visual Studio's "View White Space" (toggle with CTRL-R, CTRL-W) always shows what's in the file; it never "renders" tabs as spaces or vice versa.

"Keep tabs"

like image 45
user1969177 Avatar answered Oct 16 '22 08:10

user1969177