Keyboard shortcut to close a query tab in SSMS

Shortcuts & tips from an Impatient DBA

This series of short posts will demonstrate some lesser-known features, keyboard shortcuts, and other tips that make my day as a DBA more productive. None of these are groundbreaking super-secret features–they are the little things that I do as part of my daily work that make me more efficient.

Always be closing windows when not in use.

 

CTRL + W

In all major web browsers, CTRL+W will close your current tab. Muscle memory be damned, CTRL+W doesn’t close the current tab in SSMS. Instead, CTRL+W will select the word at the cursor’s current position. This is still a nice shortcut, but it won’t help you close any windows.

 

CTRL + F4

Huzzah! This will close your query tab! Easy as pie. Also, note that CTRL+F4 is a pretty universal shortcut that works to close tabs/windows in other applications, too–including your favorite web browser. If you can switch your muscle memory away from CTRL+W to CTRL+F4, you can use that shortcut pretty much everywhere.

 

But you really want to use CTRL+W (or something else)

If you are willing to sacrifice the “Select current word” default behavior that is tied to CTRL+W, you can re-assign that keystroke to close the query window. Keyboard shortcuts can be mapped in the SSMS Options window. On the navigation tree, go to Environment–>Keyboard–>Keyboard:

  1. Select “Window.CloseDocumentWindow” from the list. You can type a partial match into the text box to find the item you want.
  2. Put your cursor in the “Press shortcut keys” text box, type CTRL+W (or whatever else you want), then click “Assign”.
    Notice that the “Shortcut currently used by:” drop-down shows that CTRL+W is in use  by Edit.SelectCurrentWord. We’ll need to undo that assignment, too.
  3. Select “Edit.SelectCurrentWord” from the list.
  4. From the “Shortcuts for selected command” drop-down, you’ll see two items here (assuming you have the out of the box defaults). Select the option for “CTRL+W (Text Editor)” and click “Remove”.

Now, both CTRL+W and CTRL+F4 work to close your query tab!

 

If you stop after step 2, CTRL+W is assigned to both the select a word, and close tab actions. The result is that SSMS has to choose which action to take. The choice is going to be based on scope. Since the text editor is inside the window you’re closing, the CTRL+W does the “inner most” action…selecting a word. Therefore, we have to do steps 3 & 4 to unassign that “inner” action.

B1C0

1 Comment

  1. Alt is to windows what Ctrl is to tabs. E.g. Alt-Tab switches windows (in the Windows OS and some others), whereas Ctrl-Tab switches tabs within an app. So it was logical to me many years ago that Ctrl-F4 should close a tab, just as Alt-F4 has always closed a window. Muscle memory still intact. Then M#!$ went and stopped these from working in their MS Access toys, although fortunately us SQL Server adults still have Ctrl-F4 and Alt-F4

Comments are closed.