Visual Studio : short cut Key : Duplicate Line

后端 未结 30 1310
[愿得一人]
[愿得一人] 2020-12-12 10:01

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

Some similar examples:

  • in
相关标签:
30条回答
  • 2020-12-12 10:09

    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

    0 讨论(0)
  • 2020-12-12 10:10

    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
    
    0 讨论(0)
  • 2020-12-12 10:12

    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
    
    0 讨论(0)
  • 2020-12-12 10:12

    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.

    0 讨论(0)
  • 2020-12-12 10:17

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

    0 讨论(0)
  • 2020-12-12 10:19

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

    0 讨论(0)
提交回复
热议问题