How to Convert Tabs to Spaces and Vice Versa

Tips Search

How to Convert Tabs to Spaces and Vice Versa

  • Comments 10

Menu:  Edit -> Advanced -> Tabify Selected Lines; Edit -> Advanced -> Untabify Selected Lines
Command:  Edit.TabifySelectedLines; Edit.UntabifySelectedLines;Edit.ConvertTabsToSpaces;Edit.ConvertSpacesToTabs
Versions:  2008,2010
Published:  3/14/2010
Code:  vstipEdit0028

 

Some people prefer spaces, others prefer tabs.  Well you can have it any way you want it with this next item.  You can convert spaces to tabs and convert tabs to spaces on your selected lines.  There are a couple of ways to do this and each has different results:

 

Tabify / Untabify

If all you want to do is convert leading spaces to tabs (or vice versa) then you would use the Tabify/Untabify commands.  First, start out with a line with some leading spaces (NOTE:  You don't have to select the entire line for this to work as long as any part of the line is selected it will perform the action):

image

 

Now go to Edit -> Advanced -> Tabify Selected Lines:

image

 

And you should get the leading spaces converted to tabs:

image

 

Obviously, to change leading tabs to spaces you would use Untabify Selected Lines:

image

 

 

ConvertSpacesToTabs / ConvertTabsToSpaces

Okay, so what if you want ALL spaces to tabs?  Well you have to use a command to do that and will have to make a custom keyboard mapping.  The commands you are interested in are Edit.ConvertTabsToSpaces and Edit.ConvertSpacesToTabs.  Here is what ConvertSpacesToTabs does to our example (NOTE: For these commands you have to select everywhere you want to convert it will not automatically convert the entire line in case you just want to convert a part of the line):

image

 

As you can see, almost all spaces are converted to tabs.  Since spaces are converted to tabs in increments of 4, if you have, say, 6 spaces it will result in a tab and 2 spaces left over.  That is why there are some left over spaces in the example.  And, naturally, if we select these lines and run ConvertTabsToSpaces this will be the result:

image

 

And now you know how to convert tabs to spaces and spaces to tabs.  Have fun! 

  • The company I work for requires tabs, although I prefer spaces since I often copy and paste code into forums, where tabs are ignored or take up too much room.  I didn't know about this feature, but it seems like it will allow me to keep tabs as the default setting and then quickly switch to spaces before copying code; then a quick Ctrl+K+D and I'm back to tabs.

  • I prefer tabs by the zain thanks for posting this blog honestly before reading this blog i do not know the shortcuts in visual studio.

  • In Visual Studio 2005/8, these function don't do what their names say they do (in VS2003 and earlier, they actually did what their names said that they did, and I was very sad when they changed).  They only convert *leading* tabs and spaces.  If you want to convert *all* tabs and spaces, you now need to use Edit.ConvertTabsToSpaces and Edit.ConvertSpacesToTabs, which do not exist in the menus.

    See Sara's original tip: http://blogs.msdn.com/saraford/archive/2005/06/14/429133.aspx

    (Thanks to Sara for pointing me at the function that does what Untabify did in VS2003 and earlier.)

  • From experience in lots of different devenvs, tabs are nice on the surface but can get painful when you leave the IDE. I prefer spaces for this reason and note that I can still do block or line in/outdenting with tab/shift which is the main win.

  • Even though it is only leading tabs/spaces these are very useful. I have worked on files that contain a mix of tabs and spaces and if you work in an editor that has anything other than the same tab stops as the person that inserted the tabs, reading the code can get ugly fast. This is a quick way to get the problem out of the way

  • "Even though it is only leading tabs/spaces these are very useful. I have worked on files that contain a mix of tabs and spaces and if you work in an editor that has anything other than the same tab stops as the person that inserted the tabs, reading the code can get ugly fast."

    Yes, exactly.  At one point, we were working with three different sets of code that had different tab sizes.  One had 3, one had 4, and one had 8 with an indent size of 2.  That was horrible.

    "This is a quick way to get the problem out of the way"

    Our files often have tabs in other places than the middle of the line, so we need to use the "Convert" function instead of the Untabify function to get everything back in line.

  • Garbage. Tried the tabify power tool, clicked 'Tabify' and it changed all the leading tabs to spaces. When I manually enter in a tab, after a save the tool changes all tabs back to a spaces. I even clicked 'Untabify' to see if it would at least reverse the changes. It didn't. Messed up a 1300 line file.

  • Alice --- are you saying the the Tabify command converted your tabs to spaces?  

  • I have a problem with Visual Studio 2012. There is not this option in Edit-advance. Do you know if should I download any extention. Thanks in advance.

  • Oscar,

    I've had issues with the context chaning for the main menu system.  Make sure you click in the editor first THEN Edit -> Advanced.  Let me know if that works.

    Z

Page 1 of 1 (10 items)
Leave a Comment
  • Please add 5 and 1 and type the answer here:
  • Post