Visual Studio : short cut Key : Duplicate Line

Visual StudioVisual Studio-2008Keyboard Shortcuts

Visual Studio Problem Overview


Is there a shortcut for Duplicate Line command in Visual Studio 2008?

Some similar examples:

Visual Studio Solutions


Solution 1 - Visual Studio

In Visual Studio 2022

Ctrl + E, V

In Visual Studio 2019

Ctrl + D

In Visual Studio 2017 (v15.6 and after)

Ctrl + D

In Visual Studio 2017 (pre v15.6)

(edit) This feature is now built-in in VS2017: Ctrl + E, V duplicates a line if nothing is selected, or duplicates selection. You can assign it to a different key combination, or find it in the menu:

Duplicate line in VS2017

See this reference for more information.

Pre VS2017, built-in method using clipboard

As @cand mentioned, you can just do Ctrl + C ; Ctrl + V.

Ctrl + C will copy the line if nothing is selected.

Macro solution (pre VS2017)

If you'd like to implement a more complete solution, perhaps to create a simpler keyboard shortcut or you don't want to effect the clipboard, see this guide:

> Visual Basic: > > > > 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 assign 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 wish 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!

Solution 2 - Visual Studio

There's a free extension you can download here that lets you duplicate lines without replacing the clipboard contents.

By default its bound to Alt + D, but you can change it to anything you want by going to Tools->Options->Environment->Keyboard. Type "Duplicate" in the search box and look for "Edit.DuplicateSelection" and edit the shortcut to whatever you want. I prefer Ctrl + D to be consistent with other editors.

Solution 3 - Visual Studio

It's simple Ctrl + C ; Ctrl + V , check this link. As long as you don't select any text, this will duplicate the line the cursor is over when you press Ctrl+C.

Solution 4 - Visual Studio

Ctrl + C + V works for me on VS2012 with no extension.

Solution 5 - Visual Studio

Ctrl + D works for me in VS2012 with Resharper. It's Resharper's hot key.

Solution 6 - Visual Studio

In Visual Studio 2013 you can use Ctrl+C+V

Solution 7 - Visual Studio

Here's a macro based on the one in the link posted by Wael, but improved in the following areas:

  • slightly shorter
  • slightly faster
  • comments :)
  • behaves for lines starting with "///"
  • can be undone with a single undo

Imports System
Imports EnvDTE
Imports EnvDTE80

Public Module Module1

    Sub DuplicateLine()
        Dim sel As TextSelection = DTE.ActiveDocument.Selection
        sel.StartOfLine(0) '' move to start
        sel.EndOfLine(True) '' select to end
        Dim line As String = sel.Text
        sel.EndOfLine(False) '' move to end
        sel.Insert(ControlChars.NewLine + line, vsInsertFlags.vsInsertFlagsCollapseToEnd)
    End Sub

End Module

Solution 8 - Visual Studio

In Visual studio 2017 and maybe other version No need Macro or Extension,

  1. Go to Tools > Options > Environment > Keyboards
  2. Under Show commands containing: write Edit.Duplicate
  3. Place cursor to Press shortcut keys: and press Ctrl + D and click Assign button
  4. click OK to save your new keyboard shortcut

enter image description here

Solution 9 - Visual Studio

If you like eclipse style line (or block) duplicating using CTRL+ALT+UP or CTRL+UP+DOWN, below I post macros for this purpose:

Imports System
Imports EnvDTE
Imports EnvDTE80
Imports System.Diagnostics

Public Module DuplicateLineModule
	Sub DuplicateLineDown()
		Dim selection As TextSelection = DTE.ActiveDocument.Selection
		Dim lineNumber As Integer
		Dim line As String

		If selection.IsEmpty Then
			selection.StartOfLine(0)
			selection.EndOfLine(True)
		Else
			Dim top As Integer = selection.TopLine
			Dim bottom As Integer = selection.BottomLine

			selection.MoveToDisplayColumn(top, 0)
			selection.StartOfLine(0)

			selection.MoveToDisplayColumn(bottom, 0, True)
			selection.EndOfLine(True)
		End If

		lineNumber = selection.TopLine
		line = selection.Text

		selection.MoveToDisplayColumn(selection.BottomLine, 0)
		selection.EndOfLine()
		selection.Insert(vbNewLine & line)
	End Sub
	Sub DuplicateLineUp()
		Dim selection As TextSelection = DTE.ActiveDocument.Selection
		Dim lineNumber As Integer
		Dim line As String

		If selection.IsEmpty Then
			selection.StartOfLine(0)
			selection.EndOfLine(True)
		Else
			Dim top As Integer = selection.TopLine
			Dim bottom As Integer = selection.BottomLine

			selection.MoveToDisplayColumn(top, 0)
			selection.StartOfLine(0)

			selection.MoveToDisplayColumn(bottom, 0, True)
			selection.EndOfLine(True)
		End If

		lineNumber = selection.BottomLine
		line = selection.Text

		selection.MoveToDisplayColumn(selection.BottomLine, 0)
		selection.Insert(vbNewLine & line)
		selection.MoveToDisplayColumn(lineNumber, 0)
	End Sub
End Module

Solution 10 - Visual Studio

In visual studio code (WebMatrix):

Copy Lines Down: Shift + Alt + down

Copy Lines Up: Shift + Alt + up

Delete Lines: Ctrl + Shift + k

Solution 11 - Visual Studio

Why so many long drawn out methods to accomplish something so simple? It takes less than a minute to download and install the extension from Microsoft. The page says that it will bind it to ALT+D by default, but for me it bound it to CTRL+D automatically in Visual Studio Community 2015 without any changes.

Here's the link to download the extension from Microsoft.com.

Solution 12 - Visual Studio

As I can't use Macros in my Visual Studio 2013 I found a Visual Studio Plugin (I use it in 2012 and 2013). [Duplicate Selection][1] duplicates selections and whole Lines - they only need to be partial selected. The standard shortcut is ALT + D.

[1]: https://visualstudiogallery.msdn.microsoft.com/830a6482-3b8f-41a8-97b5-b9c581e5ad8b "Duplicate Selection"

Solution 13 - Visual Studio

While I realize this is not a keyboard shortcut, I figured I would add this, as it does not require the usage of the clipboard and might help some people.

Highlight the row you want to duplicate. Press control, mouse click the highlighted text, and drag to where you want to go to. It will duplicate the highlighted text.

Solution 14 - Visual Studio

I don't know if this exists in Visual Studio 2008 but in Visual Studio 2010+ you can easily do this by:

Don't select anything, then press Ctrl + C And then (without doing anything else) Ctrl + V

Solution 15 - Visual Studio

I've been using the macro that Wael posted: Duplicate line command for Visual Studio, but it stopped working a week ago, I assumed because of a Windows update. And I was correct, as of February 2014, Macros have been disabled in VS2010 (and 2008 apparently).

To fix this you'll either have to uninstall the security updates, or add one line of code into the config files as shown here.

On a 64-bit Windows machine default paths to these files are:

  • C:\Program Files (x86)\Common Files\Microsoft Shared\VSA\9.0\VsaEnv\vsaenv10.exe.config

  • C:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\IDE\devenv.exe.config

      ...
      <configuration>
          <runtime>
              <AllowDComReflection enabled="true"/>
              ...
    

    editor screenshot

You MUST run your text editor with admin rights or it won't work! Hopefully this helps anyone else who suddenly has their macro functionality pulled out from underneath them.

Solution 16 - Visual Studio

for Visual Studio 2012, 2013, 2015, 2017 follow the link and download the extension

https://marketplace.visualstudio.com/items?itemName=ctlajoie.DuplicateSelection

Now go into Tools > Options > Keyboard, and type "Duplicate" in the search box (the full command string is "Edit.DuplicateSelection"). Here you can bind it to any shortcut in the same way you would for any other command.

Solution 17 - Visual Studio

Just put your mouse on the line to copy and do CTRL+C ,afterwards CTRL+V on the same line. Works like magic :-)

Solution 18 - Visual Studio

In visual studio 2008 you can use CTRL + C + V

Solution 19 - Visual Studio

For those still viewing this question on Visual Studio post-2008, a real Edit.Duplicate has been added:

  • CTRL+E, V
  • CTRL+D (VS 2017 15.6+)

Solution 20 - Visual Studio

For Visual Studio Code 2019:

Edit menu keyboard shortcuts with: ctrl+k and ctrl+s

Edit "Copy Line Down" (Shift + Alt + DownArrow) to your your own shortcut.

You can find it, with the command ID: editor.action.copyLinesDownAction

For me, It's ctrl+d

Solution 21 - Visual Studio

Ctrl + D is a new shortcut introduced in VS 2017 v15.6 that seems to do the exact thing that Ctrl + E, V

> Ctrl + D will duplicate the line the cursor is in and insert it right below the line in focus. If you’d like to duplicate a specific set of code, simply select the portion of code you want to duplicate before invoking the duplicate code command.

It won't affect your clipboard

Source

Solution 22 - Visual Studio

http://www.jetbrains.com/resharper/

My story: started working in a new company, never used Visual Studio before. One of the first things - how to duplicate line. After setting up macro ReSharper told me: would you like to substitute my shortcut which was: "duplicate text" :)

Solution 23 - Visual Studio

I use application link:AutoHotkey with below code saved in CommentDuplikateSaveClipboard.ahk file. You can edit/remove shortcuts it is easy.
I have link to this file "Shortcut to CommentDuplikateSaveClipboard.ahk" in Autostart in windows.
This script protect your clipboard.
If you are more curious you would add shortcuts to thisable/enable script.
I sometimes use very impressive Multi Clipboard script to easy handle with many clips saved on disk and use with CTRL+C,X,V to copy,paste,cut,next,previous,delete this,delete all.

;CommentDuplikateSaveClipboard.ahk

!c:: ; Alt+C === Duplicate Line
^d:: ; Ctrl+D
ClipSaved := ClipboardAll
Send, {END}{SHIFTDOWN}{HOME}{SHIFTUP}{CTRLDOWN}c{CTRLUP}{END}{ENTER}{CTRLDOWN}v{CTRLUP}{HOME}
Clipboard := ClipSaved
ClipSaved =
return

!x:: ; Alt+X === Comment Duplicate Line
ClipSaved := ClipboardAll
Send, {END}{SHIFTDOWN}{HOME}{SHIFTUP}{CTRLDOWN}c{CTRLUP}{LEFT}//{END}{ENTER}{CTRLDOWN}v{CTRLUP}{HOME}
Clipboard := ClipSaved
ClipSaved =
return

!z:: ; Alt+Z === Del uncomment  Line
ClipSaved := ClipboardAll
Send, {END}{SHIFTDOWN}{UP}{END}{SHIFTUP}{DEL}{HOME}{DEL}{DEL}
Clipboard := ClipSaved
ClipSaved =
return

!d:: ; Alt+D === Delete line
Send, {END}{SHIFTDOWN}{UP}{END}{SHIFTUP}{DEL}
return

!s:: ; Alt+S === Swap lines
ClipSaved := ClipboardAll
Send, {END}{SHIFTDOWN}{UP}{END}{SHIFTUP}{CTRLDOWN}x{CTRLUP}{UP}{END}{CTRLDOWN}v{CTRLUP}{HOME}
Clipboard := ClipSaved
ClipSaved =
return

!a:: ; Alt+A === Comment this line, uncomment above
Send, {END}{HOME}//{UP}{HOME}{DEL}{DEL}
return

Solution 24 - Visual Studio

In Visual Studio 2010 you copy the entire line the cursor is on with CTRL + INSERT then you can use Ctrl + V or SHIFT + INSERT to paste it.

Solution 25 - Visual Studio

VS 2017 its Ctrl + D or Ctrl + C ; Ctrl + V they both work for me.

Solution 26 - Visual Studio

The command you want is Edit.Duplicate. It is mapped to CtrlE, CtrlV. This will not overwrite your clipboard.

Solution 27 - Visual Studio

Not an answer, just a useful addition: As a freebie, I just invented (well... ehm... adjusted the code posted by Lolo) a RemoveLineOrBlock macro. Enjoy!

Imports System
Imports EnvDTE
Imports EnvDTE80
Imports EnvDTE90
Imports EnvDTE90a
Imports EnvDTE100
Imports System.Diagnostics

Public Module RemoveLineOrBlock

    Sub RemoveLineOrBlock()
        Dim selection As TextSelection = DTE.ActiveDocument.Selection
        Dim lineNumber As Integer
        Dim line As String

        If selection.IsEmpty Then
            selection.StartOfLine(0)
            selection.EndOfLine(True)
        Else
            Dim top As Integer = selection.TopLine
            Dim bottom As Integer = selection.BottomLine

            selection.MoveToDisplayColumn(top, 0)
            selection.StartOfLine(0)

            selection.MoveToDisplayColumn(bottom, 0, True)
            selection.EndOfLine(True)
        End If

        selection.LineDown(True)
        selection.StartOfLine(vsStartOfLineOptions.vsStartOfLineOptionsFirstColumn,True)

        selection.Delete()

        selection.MoveToDisplayColumn(selection.BottomLine, 0)
        selection.StartOfLine(vsStartOfLineOptions.vsStartOfLineOptionsFirstText)

    End Sub

End Module

Solution 28 - Visual Studio

in VS2019 and also in VS2017 you can select Tools->Options->Keyboard and you can choose in the scheme drop-down Resharper (Visual Studio) option, and you will get a mapping like if you were using Resharper, in this case Ctrl + D will do the trick for duplicating the line. Anyways, based on the documentation this should be out of the gate in Visual Studio 2017 Version 15.8 or greater

Solution 29 - Visual Studio

if you have a macos version, cmd+shift+D can make the job for you

Solution 30 - Visual Studio

> On Linux Mint, you can do; > Ctrl + Alt + Shift + 2 => Duplicate up > Ctrl + Alt + Shift + 8 => Duplicate down

Solution 31 - Visual Studio

Visual Studio Code : May 2020 (version 1.46) Shift + UpArrow/DownArrow : To Duplicate the line of code

Solution 32 - Visual Studio

For visual studio 2010, try using these commands for quick line duplication (uses clipboard):

  • Click on the line you want to copy. Ctrl + C will copy that line.

  • Then press Ctrl + Shift + Enter to insert a blank below insertion point

    (Alternatively use Ctrl + Enter to insert a blank line above the insertion point.)

  • Then simply use Ctrl + V to paste the line.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionPostoView Question on Stackoverflow
Solution 1 - Visual StudioWael DalloulView Answer on Stackoverflow
Solution 2 - Visual StudioBenView Answer on Stackoverflow
Solution 3 - Visual StudiocandView Answer on Stackoverflow
Solution 4 - Visual StudionGolineView Answer on Stackoverflow
Solution 5 - Visual StudiosaweView Answer on Stackoverflow
Solution 6 - Visual StudioVazgen TorosyanView Answer on Stackoverflow
Solution 7 - Visual StudioMark ForemanView Answer on Stackoverflow
Solution 8 - Visual StudioewwinkView Answer on Stackoverflow
Solution 9 - Visual StudioKrzysztofView Answer on Stackoverflow
Solution 10 - Visual StudiomghhgmView Answer on Stackoverflow
Solution 11 - Visual StudioDanielView Answer on Stackoverflow
Solution 12 - Visual StudioLebewesenView Answer on Stackoverflow
Solution 13 - Visual StudioWartyView Answer on Stackoverflow
Solution 14 - Visual StudioAlireza NooriView Answer on Stackoverflow
Solution 15 - Visual StudioMasterHDView Answer on Stackoverflow
Solution 16 - Visual StudioKalyan HalderView Answer on Stackoverflow
Solution 17 - Visual StudioDavid ConstantineView Answer on Stackoverflow
Solution 18 - Visual Studiouser5460893View Answer on Stackoverflow
Solution 19 - Visual StudioicanfathomView Answer on Stackoverflow
Solution 20 - Visual StudioUrucaView Answer on Stackoverflow
Solution 21 - Visual StudioKautsky LozanoView Answer on Stackoverflow
Solution 22 - Visual StudioMars RobertsonView Answer on Stackoverflow
Solution 23 - Visual StudioMrHIDEnView Answer on Stackoverflow
Solution 24 - Visual StudioCrackerjackView Answer on Stackoverflow
Solution 25 - Visual StudioDanyal Ahmed ChaudhryView Answer on Stackoverflow
Solution 26 - Visual StudioHaveSpacesuitView Answer on Stackoverflow
Solution 27 - Visual StudioPeter PerháčView Answer on Stackoverflow
Solution 28 - Visual StudioddieppaView Answer on Stackoverflow
Solution 29 - Visual Studiokia nasirzadehView Answer on Stackoverflow
Solution 30 - Visual StudioJoshua GatoView Answer on Stackoverflow
Solution 31 - Visual StudioNikeView Answer on Stackoverflow
Solution 32 - Visual Studioprotoss1210View Answer on Stackoverflow