Visual Studio Tips: Copy & paste tricks

Visual Studio logoHere’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):

  • Line Copy
  • How to Not accidentally copy a blank line
  • How to cycle the clipboard ring to get clipboard history
  • Using the toolbox for code snippets

 

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!

Line Copy

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.

How to Not Accidentally Copy a Blank 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

Visual Studio Options

 

How to Cycle the Clipboard Ring

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>.

Add Code Snippets to the Toolbox

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.

Code in the toolbox

Get your copy of Visual studio today!

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.

Technorati Tags: Visual Studio,Copy,Paste,Tips,how to not copy Blank Lines,Visual Studio professional,dreamspark,saving code snippets