I am trying to find the shortcut for duplicating a line in Visual Studio Code (I am using 1.3.1) I tried the obvious CTRL + D but that doesn't seem to work.
Click File > Preferences > Keyboard Shortcuts:
Search for copyLinesDownAction
or copyLinesUpAction
in your keyboard shortcuts
Usually it is SHIFT+ALT + ↓
Update for Ubuntu:
It seems that Ubuntu is hiding that shortcut from being seen by VSCode (i.e. it uses it probably by its own). There is an issue about that on GitHub.
In order to work in Ubuntu you will have to define your own shortcut, e.g. to copy the line using ctrl+shift+alt+j and CTRL +SHIFT + ALT + k you could use a keybindings.json
like this:
[
{ "key": "ctrl+shift+alt+j", "command": "editor.action.copyLinesDownAction",
"when": "editorTextFocus && !editorReadonly" },
{ "key": "ctrl+shift+alt+k", "command": "editor.action.copyLinesUpAction",
"when": "editorTextFocus && !editorReadonly" }
]
-
1On Mac: Code > Preferences > Keyboard Shortcuts. Shift+Option+Down Arrow. – jarmod Oct 17 '19 at 0:49
-
2@DAXaholic answer was correct if you need even simple solution just keep the cursor on line which you want to duplicate and press ctrl+c and ctrl+v. it works fine – Gopi P Oct 18 '19 at 2:57
-
1On Ubuntu 16.04, by default, the
Ctrl
+Shift
+Up Arrow
/Down Arrow
shortcuts are mapped to moving windows to different workspaces. You can change this inUbuntu -->System Settings --> Keyboard --> Shortcuts --> Navigation
then pressBackspace
to delete the unwanted shortcuts. – mowen Oct 25 '19 at 16:09 -
1Mine is bound to Ctrl+Shift+Alt+DownArrow, but it doesn't actually do anything when I click those buttons. – Aaron Franke Apr 2 '20 at 7:22
-
1
The duplicate can be achieved by CTRL+C and CTRL+V with cursor in the line without nothing selected.
-
37I haven't downvoted, but I would imagine this isn't acceptable for the OP as this copies the line into the clipboard, which is one very good reason to want a "duplicate line" command instead of a copy / paste command. – Matt Dell Apr 18 '17 at 8:51
-
2This does not work if
editor.emptySelectionClipboard
option is set tofalse
(my preferred configuration). – Bill_Stewart Sep 21 '17 at 16:23 -
13
-
1One interesting this thing I noted in this method is , no need to release Ctrl Key after pressing 'C' ( Release only after releasing V) – Arun Apr 4 '20 at 0:16
-
2
You can use the following depending on your OS:
Windows:
Shift+ Alt + ↓ or Shift+ Alt + ↑
Mac:
Shift + Option + ↓ or Shift +Option + ↑
Linux:
Ctrl+Shift+Alt+↓ or Ctrl+Shift+Alt+↑
Note: For some linux distros use Numpad arrows
-
2
Search for copyLinesDownAction
or copyLinesUpAction
in your keyboard shortcuts
Usually, it is SHIFT+ALT+↓
Ubuntu :
- Duplicate Line Up : Ctrl + Alt + Shift + 8
- Duplicate Line Down : Ctrl + Alt + Shift + 2
-
1
-
1This is actually a good solution (working on Ubuntu 20 & 19) that does not require any changes to the default setting. Might not seem very intuitively but 8 & 2 are actual Up and Down in the NumPad – user273711 May 22 '20 at 14:09
There is a new command in v1.40: editor.action.duplicateSelection
unbound to any keybinding.
Duplicate selection
We have added a new action named Duplicate Selection. When executed, the current selection will be duplicated and the result will be selected. When there is no selection, the current line will be duplicated, all without writing to the system clipboard.
from https://github.com/microsoft/vscode-docs/blob/vnext/release-notes/v1_40.md
Some may find it helpful in certain situations.
Another 2 very usefull shortcuts are to move lines selected up and down, like sublime text does...
{
"key" : "ctrl+shift+down", "command" : "editor.action.moveLinesDownAction",
"when" : "editorTextFocus && !editorReadonly"
},
and
{
"key" : "ctrl+shift+up", "command" : "editor.action.moveLinesUpAction",
"when" : "editorTextFocus && !editorReadonly"
}
VC Code Version: 1.22.2 Go to: Code -> Preferences -> Keyboard Shortcuts (cmd + K; cms + S); Change (edit): "Add Selection To Next Find Match": "cmd + what you want" // for me this is "cmd + D" and I pur cmd + F; Go to "Copy Line Down": "cmd + D" //edit this and set cmd + D for example And for me that's all - I use mac;
Windows:
Duplicate Line Down : Ctrl + Shift + D
-
6
-
Your ans is correct, Thank you so much, I use it in my ubuntu. – Mohammad Habibur Rahman Nov 24 '19 at 18:24
It's possible to create keybindings that are only active when Vim for VSCode is on and in a certain mode (i.e., "Normal", "Insert", or "Visual").
To do so, use Ctrl + Shift + P to open up VSCode's Command Palette, then search for "Preferences: Open Keyboard Shortcuts (JSON)"--selecting this option will open up keybindings.json. Here, custom bindings can be added.
For example, here are the classic VSCode commands to move/duplicate lines tweaked for ease of use in Vim..
[
{
"key": "alt+j",
"command": "editor.action.moveLinesDownAction",
"when": "editorTextFocus && vim.active && vim.mode == 'Normal'"
},
{
"key": "alt+shift+j",
"command": "editor.action.copyLinesDownAction",
"when": "editorTextFocus && vim.active && vim.mode == 'Normal'"
},
{
"key": "alt+k",
"command": "editor.action.moveLinesUpAction",
"when": "editorTextFocus && vim.active && vim.mode == 'Normal'"
},
{
"key": "alt+shift+k",
"command": "editor.action.copyLinesUpAction",
"when": "editorTextFocus && vim.active && vim.mode == 'Normal'"
},
]
Now we can use these Vim-friendly commands in VSCode!
- Alt + J to move a line down
- Alt + K to move a line up
- Shift + Alt + J to duplicate a line down
- Shift + Alt + K to duplicate a line up
Update that may help Ubuntu users if they still want to use the ↑ and ↓ instead of another set of keys.
I just installed a fresh version of VSCode on Ubuntu 18.04 LTS and I had duplicate commands for Add Cursor Above
and Add Cursor Below
I just removed the bindings that used Ctrl and added my own with the following
Copy Line Up
Ctrl + Shift + ↑
Copy Line Down
Ctrl + Shift + ↓
CTRL + D
just select the next occurrence that was selected vs other IDEs . – Amir Kian Mar 17 '20 at 16:37