SSMS: Removing all tabs in your query window with spaces

This is just a short post that I've been meaning to write for a while.

I know that there is an eternal tabs vs spaces debate that goes on in development teams. Currently I'm in the spaces team for SQL queries.

So, it's a pain in the neck when I receive a script file from someone and it's full of tabs. Even worse when there are a bunch of tabs at weird tab positions. And I start to edit it, and things jump around, and I think @#$@$!@#$@#$ tabs !

But nowadays, SSMS can help yet very few people seem to realize it.

If you hit Control-H to bring up the Quick Replace dialog (or do it manually when Control-H doesn't work –> still investigating that), you can set an option to use Regular Expressions:


Click to turn that option on, it will have a background color (default is beige-ish). Enter \t for the from text, and 4 spaces (or whatever your favorite number is) for the "to" text and click the replace all option:


And you get to say "farewell you pesky tabs".

I also often use this with \r etc. to replace multiple double-lines, etc. etc. (One day we might even get a macro recorder but this helps for many situations)

Hope that helps someone.

4 thoughts on “SSMS: Removing all tabs in your query window with spaces”

  1. Under "Edit->Advanced" there's an "Untabbify Selected Lines" that I use quite frequently… but it doesn't get rid of ALL the tabs, just the ones at the beginning of lines it seems.  Still, it's highly useful.

  2. I am in the no tabs club because of the way different applications define the number of characters for a tab and for the reason you mention.
    Most apps treat tabs as 4 characters, but if you use notepad it treats tabs as 8 characters.
    This plays havoc with sql scripts created in SSMS with standard settings.
    I choose to set the option of replacing tabs with 4 spaces in the Options menu in SSMS and Visual Studio (or whatever dev evironment) and I request my peers to do the same.
    That way you can use tabs all you want but the final script only has spaces.

  3. How do you remove CHAR(13) and CHAR(10) in SSMS using RegularExpressions? I cannot get RegularExpressions to match New Line and Carriage Returns to work consistently in SSMS. And funny thing is that it sometimes works for entire document but not for selected text. Strange.

Leave a Reply

Your email address will not be published. Required fields are marked *