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

You can find keyboard shortcuts from

File > Preferences > Keyboard Shortcuts

Default Keyboard Shortcuts are,

Copy Lines Down Action : shift+alt+down

Copy Lines Up Action : shift+alt+up

Move Lines Up Action : alt+up

Move Lines Down Action : alt+down

Or you can override the keyboard shortcuts from

File > Preferences > Keyboard Shortcuts

And editing the keybindings.json

Example:

[
    {
        "key": "ctrl+d",
        "command": "editor.action.copyLinesDownAction",
        "when": "editorTextFocus"
    },
    {
        "key": "ctrl+shift+up",
        "command": "editor.action.moveLinesUpAction",
        "when": "editorTextFocus"
    },
    {
        "key": "ctrl+shift+down",
        "command": "editor.action.moveLinesDownAction",
        "when": "editorTextFocus"
    }
]

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.


It miss an answer to this question, "How to duplicate selection of code" Similar to Sublime's cmd/ctrl+shift+d or Jetbrains' cmd/ctrl+d behaviour.

Install plugin Duplicate selection or line from VS Code Marketplace

Duplicate selection or line VS Code plugin

This extension provides bindings for ctrl+d (Windows/Linux) and cmd+d (MacOS). To customize keyboard shortcuts Preferences -> Keyboard Shortcuts:

{
  "mac": "cmd+d",
  "key": "ctrl+d",
  "command": "geeebe.duplicateText",
  "when": "editorTextFocus"
}

Note : The shortcut does also the duplicate line so if you install it, remove your previous ctrl+d shortcut on copyLinesDownAction (if you did it before) otherwise duplicate selection can bug.