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
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.
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.
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.
Try ALT+SHIFT+UP/DOWN
It worked for me!
Direct line duplication (without the clipboard step) is proposed by the Contextual Duplicate extension.
It is mapped on ctrl+K D