One 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.
-
Imports System
-
Imports EnvDTE
-
Imports EnvDTE80
-
Imports System.Diagnostics
-
-
Public Module DuplicateLastLineModule
-
Sub DuplicateLine()
-
Dim line As String
-
DTE.ActiveDocument.Selection.StartOfLine(0)
-
DTE.ActiveDocument.Selection.EndOfLine(True)
-
line = DTE.ActiveDocument.Selection.Text
-
DTE.ActiveDocument.Selection.EndOfLine()
-
DTE.ActiveDocument.Selection.NewLine()
-
DTE.ActiveDocument.Selection.StartOfLine(0)
-
DTE.ActiveDocument.Selection.Text = line
-
End Sub
-
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:
- go to Tools->Options...
- under Environment, click Keyboard
- in the "Show Commands Containing" textbox, enter "duplicate" (this according to the name you gave the module.)
- you should now see the macro in the list below
- choose "Text Editor" from the "Use new shortcut in" list
- 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)
- hit the "Assign" button
- you should now see the shortcut in the "Shortcuts for selected command" textbox
- 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
Christophe Herreman is a software developer living in Belgium. He's working on high-end Flex and AIR solutions at 
August 28th, 2006 at 7:39 am
Hi Christophe,
If you like this kind of stuff, you definitively must take a look to ReSharper. As you said, “once you get used to it, you can’t live without it”.
http://www.jetbrains.com/resharper/
++
May 7th, 2008 at 9:30 pm
Thanks, I’ve been looking for something like this.
May 12th, 2008 at 12:18 pm
Thanks, I’m moving from Linux dev and all the text editors I have ever used have had ctrl+d for duplicate line. I find it hard to believe this functionality was missing from VS2005! Thanks for the macro
September 21st, 2008 at 3:55 pm
Thanks Christophe! I’ve been missing this from Eclipse… nice to have it back!
September 25th, 2008 at 5:21 pm
Hi,
I’m modified the macro so that if multiple line are selected, they are duplicated. If not, only the current line is dupplicated (as before).
Note: The String.Replace function is used to prevent the “Automatic insertion of end constructs” option from being triggered, which would add uneeded additionnal end constructs.
[CODE]
Imports System
Imports EnvDTE
Imports EnvDTE80
Imports System.Diagnostics
Public Module DuplicateSelectionOrCurrentLineModule
Sub DuplicateLine()
Dim line As String
line = DTE.ActiveDocument.Selection.Text
If (line.Length = 0) Then
DTE.ActiveDocument.Selection.StartOfLine(0)
DTE.ActiveDocument.Selection.EndOfLine(True)
line = DTE.ActiveDocument.Selection.Text
End If
line.Replace(ControlChars.NewLine, ControlChars.NewLine & “‘”)
DTE.ActiveDocument.Selection.EndOfLine()
DTE.ActiveDocument.Selection.NewLine()
DTE.ActiveDocument.Selection.StartOfLine(0)
DTE.ActiveDocument.Selection.Text = line
End Sub
End Module
[/CODE]
February 2nd, 2010 at 4:00 am
This didn’t work for me in VS 2008 Pro.