The purpose of this article is to teach or reinforce your basic keyboarding skills as you prepare for using SSMS and coding in T-SQL.
Using a mouse to navigate applications and web pages is a standard skill. However, when doing any type of software coding, quickly accomplishing the same things with the keyboard becomes more important. Not only are your hands already on the keyboard, but many keyboard shortcuts are simply faster than reaching for the mouse.
The keyboard shortcuts discussed here are common in all most Microsoft software products, and are also common in many other products. All of the shortcuts, for example, will also work in NotePad++, commonly used for HTML and other types of coding.
You may already know and use several of these shortcuts. As you begin working in T-SQL, I highly recommend you practice and use these wherever practical.
- Home Key / End Key
- Selecting lines
- Copy: CTRL-A
- Paste: CTRL-V
- Cut: CTRL-X
- Select All: CTRL-A
- UNDO: CTRL-Z
- REDO: CTRL-Y
- Find: CTRL-F
- Replace: CTRL-H
- Work with Multiple Lines: ALT-SHIFT
Home Key / End Key
The keyboard normally includes HOME (HM) and END keys. Use them! Regardless of where the cursor is currently located in a line, pressing HOME will move the cursor to the beginning textof the line. Pressing HOME twice will move the cursor to the left margin of the current container or window. (If the cursor is in a data entry box, for example, the cursor will move to the beginning text or margin in that container - not the outer page or document.)
The END key works the same way. In this case, it takes you to the end of the text - which is we care about.
Once the cursor is at the beginning or end of a line, it is easy to SELECT one or more lines by using the DOWN or UP arrow keys. With each press of the arrow key, an entire line of text will be highlighted and selected.
If the cursor is in the middle of a line, it is also possible to highlight and selected everything to the left of the cursor using SHIFT-HOME or to the right of the cursor using SHIFT-END.
This is the shortcut that should always be preferred over the mouse. CTRL-C will copy the selected (highlighted) text into the clipboard (memory). Every time CTRL-C is used, it replaces what is currently in the clipboard.
This shortcut should always be preferred over the mouse. CTRL-V will paste the current clipboard contents to the current location of the cursor. CTRL-V does not clear the clipboard. Which means that CTRL-V can be used multiple times to paste the clipboard to the current editing container. This is true, even if the location of the cursor is moved in between multiple uses of CTRL-V; it will always paste the current contents of the clipboard.
CTRL-X is similar to CTRL-C, in that it copies the currently selected text into the clipboard. CTRL-X, however, also deletes the text from the current container. CTRL-X is most often used to move text by using CTRL-X (cut) and then CTRL-V (paste).
Select All: CTRL-A
CTRL-A selects (highlights) all the text in the current container. In a text box, for example, it simply selects all the text in that box. In an editing space such as SSMS, CTRL-A selects all the text in the current tab.
This is very useful when wanting to make a copy of the current script, in order to edit the copied code in a new tab. In the SSMSS Query Manager, that specific task can be accomplished in four key strokes - regardless of where the cursor is located in the current tab:
This is our lifesaver: CTRL-Z (UNDO). CTRL-Z will "undo" the last editing step. That might be typing a character, deleting a word, or deleting an entire text.
CTRL-Z can be used multiple times and will continue "walking back" or undoing editing steps. For example, if the last three steps were 1) deleted a paragraph, 2) pasted a sentence, 3) entered a name, using CTRL-Z three times would un-delete the paragraph, remove the pasted sentence, and delete the last character entered in the name.
CTRL-Y (REDO) works in conjunction with CTRL-Z. It is an UNDO for the UNDO. CTRL-Y "walks forward" from steps taken with CTRL-Z. In other words, if copied 4 lines of text, then used CTRL-Z to remove (UNDO) the 4 lines, then realized that you had only wanted to remove 3 lines, CTRL-Y could be used to bring back the last line (the last step).
CTRL-F is a simple text finder. Starting at the current cursor, it will find the next instance of a string. In SSMS, you have the option to also narrow the search by matching only on the entire word or by case. If a section of text is already selected, the search defaults to the selected text ("Selection"), or it will default to the entire editing container ("Current Document").
In SSMS, the Find search will highlight in yellow all current instances of the string in the current container, with the cursor placed at the first instance. F3 (or the find box arrow) can also be used to advance the cursor to the next instance.
CTRL-H is an advanced Find shortcut, with the option to replace the search string with something else. This replacement can be made for the current/next instance of the string, or all instances in the current container (document).
In SSMS, this can be an extremely valuable tool. Many scripts are very similar, such as the script for creating a table. If one has a script to create a table named Customers and needs a table for Vendors, using CTRL-H would make the process much quicker:
It is common to ident code sections to make them more readable. In most IDE's, once a section of text has been selected, such as multiple lines of code, the TAB key can be used to indent the code or more tabs, as needed. SHIFT-TAB is a reverse tab shortcut, and will move the selected code to the left by one tab.
Work with Multiple Lines: ALT-SHIFT
This is one of the most valuable shortcuts you will ever use. It is not available in some software, such as Word and Excel. But it is available in SSMS, Visual Studio and NotePad++.
ALT-SHIFT starts with the current cursor position and draws a line up or down using the arrow keys (in SSMS it is a light-blue line). This line is effectively the cursor position for all the lines where it is drawn. Once it is drawn, you can release the SHIFT and ALT keys. You now have several options on editing all the selected lines at once.
For example, if you start typing, the same characters will be entered on all the lines at the same time. You may use the delete or back keys, as in any editing sequence.
It is also possible to use the HOME, END, and right/left arrow keys to select block of text in the multiple lines. CTRL-C and CTRL-X can be used to copy/cut the entire block of text and place it in the clipboard.
Lastly, CTRL-V can be used paste a text into the selected block.
Practical Usage of ALT-SHIFT and other Shortcuts
Let's take a practical example in SSMS. Assume you have created a States table for all U.S. States, and you now need to populate the table. You would need to use 50 INSERT statements or an INSERT statement with 50 VALUE clauses to get the job done.
Assuming we use a single INSERT statement, our code might look like this (for the first 8 states):
So now we have to type the names of each state into each value clause. Right?
But suppose we can easily copy a list of states from the Internet. They look like this:
Alabama Alaska Arizona Arkansas California Colorado Connecticut Delaware ...
What to do? Copy the list of states into the Query Manager. Place the cursor at Alabama. Use ALT-SHIFT and the down-arrow key to draw the cursor line down along all the state names. Use the right-arrow key to select all the state names. Make sure the block covers the longest name (ignore the fact that the block extends beyond the shorter names.
Use CTRL-C to copy the block into the clipboard. Place the cursor inside the single quotes of the first VALUE clause. Redraw the cursor line using ALT-SHIFT down through all the VALUE clauses. Paste the clipboard contents using CTRL-V. Mission accomplished. No typing of state names.
One interesting thing you will notice is that when pasting the content block above, the paste function only copies the actual state names. It does not copy trailing spaces that appeared to be part of the text block where some names were shorter.