How do I duplicate a line or selection within Visual Studio Code?

前端 未结 17 2139
青春惊慌失措
青春惊慌失措 2020-12-04 04:38

Using Microsoft\'s Visual Studio Code, how do I duplicate a line of code and then move it up and down? (Similar to Sublime\'s cmd+shift+d be

相关标签:
17条回答
  • 2020-12-04 05:06

    The commands your are looking for are editor.action.copyLinesDownAction and editor.action.copyLinesUpAction.

    You can see the associated keybindings by picking: File > Preferences > Keyboard Shortcuts

    Windows:

    Shift+Alt+Down and Shift+Alt+Up

    Mac:

    Shift+Option+Down and Shift+OptionUp

    Linux:

    Ctrl+Shift+Alt+Down and Ctrl+Shift+Alt+Up

    (Might need to use numpad Down and Up for Linux)

    Furthermore, commands editor.action.moveLinesUpAction and editor.action.moveLinesDownAction are the ones to move lines and they are bound to Alt+Down and Alt+Up on Windows and Mac and Ctrl+Down and Ctrl+Up on Linux.

    0 讨论(0)
  • 2020-12-04 05:06

    In VSCode Ctrl+CCtrl+V duplicates the whole line below.

    I prefer this to the accepted answer, because it only requires one hand to do this and feels way more natural.

    The accepted answer will probably do it for most people, however Down sits the other side of the keyboard. So you have two options, use both hands on (Left Hand:L Shift+L Alt+ Right Hand:Up/Down), or with a single hand use the right R Shift+R Alt+Up/Down. The second option feels weird in my opinion. I'd rather use the option where my hand naturally sits on the keyboard, and if its one hand, even better.

    0 讨论(0)
  • 2020-12-04 05:06

    Note that for Ubuntu users (<= 17.4), Unity uses CTRL + ALT + SHIFT + Arrow Key for moving programs across virtual workspaces, which conflicts with the VS Code shortcuts. You'll need to rebind editor.action.copyLinesDownAction and editor.action.copyLinesUpAction to avoid the conflict (or change your workspace keybindings).

    For Ubuntu 17.10+ that uses GNOME, it seems that GNOME does not use this keybinding in the same way according to its documentation, though if someone using vanilla workspaces on 17.10 can confirm this, it might be helpful for future answer seekers.

    0 讨论(0)
  • 2020-12-04 05:07

    Try ALT+SHIFT+UP/DOWN

    It worked for me!

    0 讨论(0)
  • 2020-12-04 05:11

    Direct line duplication (without the clipboard step) is proposed by the Contextual Duplicate extension.

    It is mapped on ctrl+K D

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