Don’t forget students can get Visual Studio Professional for free through DreamSpark!
Find more Visual Studio tips and tricks here
Cut, Copy, Paste: Three commands programmers use every day. Let’s face it, all coders are basically lazy: The less keystrokes the better. That’s why it’s nice to know the fastest way to select the text you want to cut, copy & paste. In this post I’ll share a few different techniques for selecting code. You probably know a couple of the tricks listed below, but did you know all of them? If you have any tricks of your own, please share!
- How do I select one word at a time?
- How do I select to end of line?
- How do I select an entire line of code as quickly as possible?
- How do I select multiple lines of code starting from a particular column?
- Two Bonus tips (You have to scroll down to find them)
How do I select one word at a time?
You can select text using the keyboard as well as the mouse. If you put the cursor anywhere in the code and hold down the <SHIFT> key and then use the right or left arrow key, you can make a selection one letter at a time.
If you hold down the <SHIFT> and <CTRL> keys then using the arrow keys will select one word at a time.
How do I select to end of line?
If you hold down the <SHIFT> key and then press the <END> key you select from the cursor location to the end of the line
How do I select an entire line of code as quickly as possible?
I discovered this trick by accident, and fell in love with it. If you want to select an entire line of code, all you need to do is put the cursor anywhere on the line, do not make any selection at all and then do the desired command (Cut, copy, or paste). When there is no text selected, cut, copy and paste default to selecting the entire line. Try it!
How do I select multiple lines of code starting from a particular column?
Have you ever tried to select code on multiple lines? If you just start in the middle of a line of code and select multiple lines of code you end up with a selection like this:
Now try holding down the <ALT> key as you make a multiple line selection. This works if you are selecting with the keyboard or the mouse.
When you use the <ALT> key you get something called a box select. This can be useful if you are trying to copy a list of variable names, comments, or namespaces without the entire line of code.
Okay if you actually scrolled down this far, here are the two bonus tips I promised.
Not only can you select code over multiple lines with the <ALT> key, you can actually edit code on multiple lines as well. Let’s say you have a list of variables declared as private and you decide they need to be public. Use the <ALT> key to do a multiple line selection of the keyword private across all the declarations.
Now start typing the word public, as you type it will replace the selected text on each line with the new text you type
Last but not least, you can actually insert text on multiple lines of code using the box select technique as well. Let’s say I wanted to add some similar comment text after each variable declaration above. Use the <ALT> key to do a multiple line selection, but make your selection at the position in the line where you want to add the comments.
Now start typing the text you want to insert, as you type it will be added to all the lines.
If you like that multi-edit feature, you may also want to check out this neat little add-on for Visual Studio called MultiEdit Extension for Visual Studio. After you install the extension, if you hold down <ALT> while mouseclicking in the editor it will add multiple selection points wherever you clicked. Then you just type and your text is added to all the selected positions. Scott Hanselman wrote a blog post about it here. Thanks Andrew for pointing out this neat little extension.