visual-studio Visual Studio:快捷键:重复行

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2279000/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-22 11:33:29  来源:igfitidea点击:

Visual Studio : short cut Key : Duplicate Line

visual-studiovisual-studio-2008keyboard-shortcuts

提问by Posto

Is there a shortcut for Duplicate Linecommand in Visual Studio2008?

Visual Studio2008 中是否有Duplicate Line命令的快捷方式?

Some similar examples:

一些类似的例子:

采纳答案by Wael Dalloul

In Visual Studio 2019

在 Visual Studio 2019 中

Ctrl+ D

Ctrl+ D

In Visual Studio 2017 (v15.6 and after)

在 Visual Studio 2017(v15.6 及更高版本)中

Ctrl+ D

Ctrl+ D

In Visual Studio 2017 (pre v15.6)

在 Visual Studio 2017(v15.6 之前)中

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

(编辑)此功能现已内置在 VS2017 中:Ctrl+ EV如果未选择任何内容,则复制一行,或重复选择。您可以将其分配给不同的组合键,或在菜单中找到它:

Duplicate line in VS2017

VS2017中的重复行

See this referencefor more information.

有关更多信息,请参阅此参考资料

Pre VS2017, built-in method using clipboard

Pre VS2017,使用剪贴板的内置方法

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

正如@cand 提到的,你可以做Ctrl+ C; Ctrl+ V

Ctrl+ Cwill copy the line if nothing is selected.

CtrlC如果没有选择任何内容,+将复制该行。

Macro solution (pre VS2017)

宏解决方案(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:

如果你想实现一个更完整的解决方案,也许是为了创建一个更简单的键盘快捷键,或者你不想影响剪贴板,请参阅本指南:

Duplicate line command for Visual Studio

Visual Studio 的重复行命令

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 whish to use for it (Ctrl+Shift+Din 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!

视觉基础:

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

要创建宏,只需转到宏资源管理器(“工具->宏->宏资源管理器”或Alt+ F8)并将代码复制粘贴到新模块中。现在只需为其分配一个键盘快捷键:

  1. 转到工具->选项...
  2. 在环境下,单击键盘
  3. 在“Show Commands Containing”文本框中,输入“duplicate”(根据您为模块提供的名称。)
  4. 您现在应该在下面的列表中看到宏
  5. 从“使用新快捷方式”列表中选择“文本编辑器”
  6. 在“按快捷键”文本框中设置焦点并点击您希望使用的键盘上的组合(在我的情况下为Ctrl+ Shift+ D
  7. 点击“分配”按钮
  8. 您现在应该在“所选命令的快捷方式”文本框中看到快捷方式
  9. 点击确定按钮

就是这样。享受!

回答by Ben

There's a free extension you can download herethat 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+ Dto be consistent with other editors.

默认情况下它绑定到Alt+ D,但您可以通过转到工具->选项->环境->键盘将其更改为您想要的任何内容。在搜索框中键入“Duplicate”并查找“Edit.DuplicateSelection”并将快捷方式编辑为您想要的任何内容。我更喜欢Ctrl+D与其他编辑器保持一致。

回答by cand

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.

很简单Ctrl+ C; Ctrl+ V,检查此 链接。只要您不选择任何文本,这将复制您按Ctrl+时光标所在的行C

回答by nGoline

Ctrl+ C+ Vworks for me on VS2012 with no extension.

Ctrl+ C+V在 VS2012 上对我来说有效,没有扩展名。

回答by sawe

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

Ctrl+D在 VS2012 中使用 Resharper 对我来说有效。这是 Resharper 的热键。

回答by Vazgen Torosyan

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

在 Visual Studio 2013 中,您可以使用Ctrl+ C+V

回答by Mark Foreman

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

这是一个基于 Wael 发布的链接中的宏,但在以下方面进行了改进:

  • 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

回答by Krzysztof

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

如果你喜欢日食风格的行(或块)使用复制CTRL+ ALT+UPCTRL+ UP+ DOWN,下面我交宏为了这个目的:

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

回答by Daniel

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+Dby default, but for me it bound it to CTRL+Dautomatically in Visual Studio Community 2015 without any changes.

为什么有这么多冗长的方法来完成如此简单的事情?从 Microsoft 下载并安装扩展程序只需不到一分钟。该页面说它默认情况下会将其绑定到ALT+ D,但对我而言,它在 Visual Studio Community 2015 中自动绑定到CTRL+ D,无需任何更改。

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

这是从 Microsoft.com 下载扩展的链接。

回答by mghhgm

In visual studio code(WebMatrix):

Visual Studio 代码( WebMatrix) 中:

Copy Lines Down: Shift+ Alt+ down

向下复制行:Shift+ Alt+down

Copy Lines Up: Shift+ Alt+ up

复制行:Shift+ Alt+up

Delete Lines: Ctrl+ Shift+ k

删除线:Ctrl+ Shift+k