Duplicate line command for Visual Studio

.NET, Visual Studio 3 Comments »

Visual Studio 2005One of the things I'm missing in Visual Studio is a keyboard shortcut to duplicate a line of code. I use this command alot in Eclipse/FDT (Ctrl+Shift+D) and once you get used to it, you can't live without it. After googling around for a moment, I found this VB code that does just that. The great thing is that in Visual Studio you can create your own macro and attach it to a keyboard shortcut.

Visual Basic:
  1. Imports System
  2. Imports EnvDTE
  3. Imports EnvDTE80
  4. Imports System.Diagnostics
  5.  
  6. Public Module DuplicateLastLineModule
  7. Sub DuplicateLine()
  8. Dim line As String
  9. DTE.ActiveDocument.Selection.StartOfLine(0)
  10. DTE.ActiveDocument.Selection.EndOfLine(True)
  11. line = DTE.ActiveDocument.Selection.Text
  12. DTE.ActiveDocument.Selection.EndOfLine()
  13. DTE.ActiveDocument.Selection.NewLine()
  14. DTE.ActiveDocument.Selection.StartOfLine(0)
  15. DTE.ActiveDocument.Selection.Text = line
  16. End Sub
  17. End Module

To create the macro, just go to the macro explorer ("Tools->Macros->Macro Explorer" or Alt+F8) and copy paste the code in a new module.

Now just assing a keyboard shortcut to it:

  1. go to Tools->Options...
  2. under Environment, click Keyboard
  3. in the "Show Commands Containing" textbox, enter "duplicate" (this according to the name you gave the module.)
  4. you should now see the macro in the list below
  5. choose "Text Editor" from the "Use new shortcut in" list
  6. set focus in the "Press shortcut keys" textbox and hit the combination on the keyboard you whish to use for it (Ctrl+Shift+D in my case)
  7. hit the "Assign" button
  8. you should now see the shortcut in the "Shortcuts for selected command" textbox
  9. hit the OK button

And that's it. Enjoy !


Add to Bloglines - Digg This! - del.icio.us - Stumble It! - Twit This! - Technorati links - Share on Facebook - Feedburner
 
WP Theme & Icons by N.Design Studio
Entries RSS Comments RSS Login