How to Convert Tabs to Spaces and Vice Versa

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!