Duplicate line command for Visual Studio

.NET, Visual Studio Add 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
  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

15 Responses to “Duplicate line command for Visual Studio”

  1. Shaoken Says:

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


    ++ :)

  2. slapout Says:

    Thanks, I’ve been looking for something like this.

  3. Matt Says:

    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 :D

  4. Chris Says:

    Thanks Christophe! I’ve been missing this from Eclipse… nice to have it back!

  5. JeeZ Says:

    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.

    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
    line = DTE.ActiveDocument.Selection.Text
    End If
    line.Replace(ControlChars.NewLine, ControlChars.NewLine & “‘”)
    DTE.ActiveDocument.Selection.Text = line
    End Sub
    End Module

  6. Gary Oak Says:

    This didn’t work for me in VS 2008 Pro.

  7. Diego Says:

    Same here, this does not work with VS 2008 Pro.

  8. Diego Says:

    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.

  9. Jonny D Says:

    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
    End If
    objSel.Insert(objSel.Text, vsInsertFlags.vsInsertFlagsInsertAtEnd)
    End Sub
    End Module

  10. Mohamed Jbeli Says:

    this is my code to invert the equality on a line
    a = b becomes b = a
    Sub Invert()
    Dim line As String
    line = DTE.ActiveDocument.Selection.Text
    Dim EqMembers = line.Split(“=”)
    If EqMembers.Length > 0 Then
    Dim newLine = EqMembers(1) + “=” + EqMembers(0)
    While newLine.IndexOf(” “) > -1
    newLine = newLine.Replace(” “, ” “)
    End While
    DTE.ActiveDocument.Selection.Text = newLine
    End If
    End Sub

  11. Sven Says:

    Thanks for sharing!
    I used the macro recorder in VS 2010 Pro. Your example was very helpful to connect macro and shortcut.

  12. Maxime Says:

    Yay! Thanks a lot and have a good day! :)

  13. CSharpForeverMore Says:

    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.

  14. Mykola Says:

    Thanks for useful macro!
    Does anyone know how to achive similar results in VS Express, without Macro engine?

  15. mux Says:

    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:

Leave a Reply

WP Theme & Icons by N.Design Studio
Entries RSS Comments RSS Log in