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 behaviour)
It's a feature that I use constantly, and am struggling using Visual Studio Code without it.
Duplicate lines => CTRL + D. Move a line/selection of code Up/Down => Ctrl + Shift + UP / DOWN.
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 .
You can press Ctrl + Shift + P , then Enter it also repeat the lastest command.
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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With