SSMS Tips and Tricks 3-7: Replace tabs with spaces and do macro-like work using regular expressions

SSMS Tips and Tricks 3-7: Replace tabs with spaces and do macro-like work using regular expressions

A request that I hear all the time, is “I don’t like tabs but insert name of annoying colleague here decided he likes to use them. How do I remove them?” Similar thing happens when installing SSMS and leaving the default options.

Tabs vs Spaces

Whether to use tabs or spaces leads to near religious level arguments amongst SQL developers. I see strong arguments on both sides. In the past, I’ve always ended up using spaces because I’ve run into issues with tabs in some of the tooling that I needed to use.

I’m quite swayed though, by the argument that says that tabs are best for accessibility. For someone with a visual impairment, being able to change the size of the tabs can be really helpful. It’s also useful for people switching between different size screens.

In this item, I’m not taking sides, but showing you how to change from tabs to spaces, if that’s what you want to do.

SSMS options

Now SSMS allows you to choose to use spaces instead of tabs (in Tools > Options) but that doesn’t “fix” tabs that are already in the code.

The regular expression functions in SSMS let you do this. Hit Ctrl-H to open the Find and Replace dialog (or use the Edit > Find and Replace > Quick Replace menus), then configure the window as follows:

Note that I’ve clicked the little square and asterisk thing (which says to use regular expressions), then put the change from text to \t which is a tab, and set the change to text to four spaces (or however many you want), then I click the Replace All button and magically all my tabs are replaced.

Other regex options

But changing tabs and spaces is just the start of what we can do.

For example, if I have a long row of comma-delimited values like this:

Now I could go to each comma and hit enter after each one. That’s what I see most people doing. But you could do this:

I’m changing a comma to a comma followed by a carriage return. And magically, this is the outcome:

There are so many things you can use this regular expression functionality for within SSMS. I encourage you to try it out.

2025-07-10