Here’s a few hidden tricks in Visual Studio for those who love <CTRL><C>.
I once heard someone say “the only original line of code every written was the first line of code, the rest was copy and paste.” While that many not entirely be true, all of us who do any serious amount of coding get pretty good at <CTRL><C> and <CTRL><V>. So I thought I would share a couple of tricks I have discovered over the years for efficient copy, cut, and paste.
Here are the tricks I will share in this post (find more tips and tricks here):
Don’t forget if you are a student, don’t settle for Express versions! you can use the professional tools for free. Go to DreamSpark.com, your one stop shop for software, and download a copy of Visual Studio Professional today!
If you want to copy a single line of code, did you know you can just put the cursor anywhere on the line of code and then use <CTRL><C> to copy the line to your clipboard. When no text is selected, the default is to select the entire line.
Have you ever copied something to the clipboard , gone to a blank line to insert it and then accidentally hit <CTRL><C> instead of <CTRL><V>. When you do, it’s really annoying because when you hit <CTRL><C> on the blank line the blank line is pasted to the clipboard and overwrites the code you were planning to paste. So when you do finally hit <CTRL><V> you just insert another blank line.
Obviously someone on the Visual Studio team has made the same mistake because there is actually an option to tell Visual Studio not to copy blank lines.
From the menu, select Tools | Options | Text Editor | All Languages
Then deselect the checkbox beside Apply Cut or Copy Commands to blank lines when there is no selection
Have you ever copied a big block of code, and then just before you were going to insert the code, been distracted by another code change where you did a copy and paste. Of course, after that, when you go to paste that big block of code, it’s been overwritten by something else.
Each item you copy to the clipboard is kept in the clipboard ring, you can actually cycle through the clipboard ring by using the keyboard shortcut <CTRL><SHIFT><V>.
You use the toolbox all the time when you are dragging controls to your forms, did you know you can use the toolbox when you are coding? If you select code and drag it to the toolbox it is saved and at any time you can drag the code from the toolbox back to your code to paste it. The best part is the code you drag to the toolbox will be kept there from project to project, even when you close and re-open Visual Studio. It’s a great place to put favorite comment blocks, error handling, or any other code you want to be able to re-use.
Because you may end up storing a number of snippets here, you may want to rename the snippet. To rename a snippet just right click on the snippet in the toolbox and choose rename from the context menu. Renamed snippets display their names in the toolbox, If you do not rename a snippet, you will see the beginning of the first line of code in the snippet displayed in the toolbox.
The image below shows the toolbox after I dragged over four code snippets to the toolbox. The first three snippets have been renamed.
If you have any favorite tricks, please share them with the rest of us!
Students can download Visual Studio Professional for no charge at DreamSpark.com.
See the 'Find' feature in VS 2010 and 2012 that I blogged here:
I would highly recommend using a program like Snagit to take your screenshots. Straight arrows and clean ellipse can go a long way!
I see someone on Reddit also commented after reading this blog pointing out another tip:
The best copy/paste trick is the Edit >> Paste Special menu. "Paste JSON as Classes" and "Paste XML as Classes" do exactly what they say.
Don't forget holding Alt down and dragging the mouse down will create a multi-line cursor enabling you to edit multiple lines at once.
Can also be used with drag left/right to insert/remove similar chunks of code across multiple lines.