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.
August 21st, 2010 at 3:23 pm
Same here, this does not work with VS 2008 Pro.
August 21st, 2010 at 3:46 pm
Ok, you have to Name your Macro “DuplicateLastLineModule” or if you want to change that name, you must change the name in the script file as well.
After that, go to Tools -> Options -> Environment -> Keyboard.
On the search box, enter
Macros.[NAME OF THE MACRO MODULE].DuplicateLastLineModule.DuplicateLine
Now you can assign a keyboard shortcut.
August 23rd, 2010 at 7:50 pm
Here’s a simpler, faster, and better (works with a single undo) way… The use of Collapse is not ideal, however:
Imports EnvDTE
Public Module DuplicateLine
Sub DuplicateLine()
Dim objSel As TextSelection = DTE.ActiveDocument.Selection
If (objSel.IsEmpty()) Then
objSel.SelectLine()
End If
objSel.Insert(objSel.Text, vsInsertFlags.vsInsertFlagsInsertAtEnd)
objSel.Collapse()
End Sub
End Module
September 9th, 2010 at 8:12 pm
this is my code to invert the equality on a line
a = b becomes b = a
Sub Invert()
Dim line As String
DTE.ActiveDocument.Selection.StartOfLine(0)
DTE.ActiveDocument.Selection.EndOfLine(True)
line = DTE.ActiveDocument.Selection.Text
Dim EqMembers = line.Split(“=”)
If EqMembers.Length > 0 Then
DTE.ActiveDocument.Selection.EndOfLine()
Dim newLine = EqMembers(1) + “=” + EqMembers(0)
DTE.ActiveDocument.Selection.StartOfLine(0)
DTE.ActiveDocument.Selection.EndOfLine(True)
While newLine.IndexOf(” “) > -1
newLine = newLine.Replace(” “, ” “)
End While
DTE.ActiveDocument.Selection.Text = newLine
End If
End Sub
October 20th, 2010 at 3:10 pm
Thanks for sharing!
I used the macro recorder in VS 2010 Pro. Your example was very helpful to connect macro and shortcut.
Cheers
Sven
May 12th, 2011 at 6:26 pm
Yay! Thanks a lot and have a good day!
June 24th, 2011 at 4:35 pm
Guys – if you do one thing for yourselves – you’ll read Scott Hanselman’s tool listings. Every decent developer knows that ReSharper is great (provided you’ve got either VS2008 and a maximum 3 year old machine – or VS2010 and a 64bit, 12 GB dev mmachine). If you have Visual Studio 2010 Professional and above, you can use the Extensions. (Tools > Extension Manager) for many of the features available as single downloads (VX10 Code Map, etc). Also – check out CodeRush from the guys over at DevExpress.
Google is your friend. Everything is possible.
June 29th, 2011 at 12:08 pm
Hi!
Thanks for useful macro!
Does anyone know how to achive similar results in VS Express, without Macro engine?
November 15th, 2011 at 4:06 pm
You can also use built-in Visual Studio (at least 2010) feature: just put cursor on the line and press Ctrl+C > Ctrl+V in a sequence. Found here:
http://vidmar.net/weblog/archive/2009/11/11/ldquosmartrdquo-duplicate-line-in-visual-studio-out-of-the-box.aspx