Occasionally, I’ll stumble across one of those neat tricks that makes me ask myself, “Why didn’t I know about this years ago?”. This next tip was one such discovery, and has been a huge time saver for me.
Vertical Text Selection in SSMS
Typically, selecting blocks of text is done in a left-to-right, top-to-bottom order. If you use your mouse to select a word on line 2, for example, you can drag the cursor down multiple lines, which will select everything – including line breaks and whitespace – all the way through the end of the selection. But did you know that you can select and edit text vertically in SQL Server Management Studio? In the same way you select from left to right, you can select fixed blocks (rather than the entire line) of code. By holding down the Alt key during text selection, you can limit the selection to only the region you specify rather than grabbing all of the text on every line.
As shown below, holding down the Alt key during selection allows me to pick only the text region I want.
Once selected, you can do most anything with that text block. You can enter some replacement text or simply delete that block of text. When you enter or remote text, it applies to every single line in that selection, which allows you to perform bulk tasks such as adding a COALESCE() statement to every column as shown below.
You can also copy and paste text selected in this way. Expanding the example from above, I want to use an alias for each coalesced column, so I can use the same method to vertically select and then vertically paste the code.
Just remember when you copy and paste like this, you have to vertically select the paste region just like you did with the copy.
I’ve found that this vertical text selection isn’t limited to just SSMS. If you are a Notepad++ user, you can do the same thing with that tool. Sadly, it doesn’t yet work in Visual Studio.
I hope you find this tip as useful as I have. This little trick has saved me a lot of manual typing over the years!