416

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.

Randy Levy
  • 22,566
  • 4
  • 68
  • 94
TheWebGuy
  • 11,429
  • 16
  • 52
  • 72
  • 5
    Possible duplicate of [How do I duplicate a line or selection within Visual Studio Code?](http://stackoverflow.com/questions/30203752/how-do-i-duplicate-a-line-or-selection-within-visual-studio-code) – Chris Feb 20 '17 at 08:27
  • 3
    Ctrl+D for line duplication is not that obvious: it works in Notepad++ for example, but it *deletes* the current line in Eclipse :) – kol Aug 08 '17 at 09:01
  • See the [Contextual Duplicate](https://marketplace.visualstudio.com/items?itemName=lafe.contextualduplicate) extension – CharlesB Jan 11 '18 at 14:07
  • 1
    by default ,`CTRL + D` just select the next occurrence that was selected vs other IDEs . – Amir Kian Mar 17 '20 at 16:37
  • 1
    @kol Having used Visual Studio previously, and VSCode being MS product I would assume that VSCode inherits a lot of things from it. CTRL+D works fine in Visual Studio 2019. – Sahil Singh May 15 '21 at 01:26

17 Answers17

627

The default is:

  • Windows / Linux SHIFT+ALT +
  • Mac SHIFT+OPTION +

To change it, click File > Preferences > Keyboard Shortcuts:

enter image description here

Search for copyLinesDownAction or copyLinesUpAction in your keyboard shortcuts


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" }
]
mikemaccana
  • 110,530
  • 99
  • 389
  • 494
DAXaholic
  • 33,312
  • 6
  • 76
  • 74
  • 10
    On Mac: Code > Preferences > Keyboard Shortcuts. Shift+Option+Down Arrow. – jarmod Oct 17 '19 at 00:49
  • 3
    @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 02:57
  • 2
    On 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 in ```Ubuntu -->System Settings --> Keyboard --> Shortcuts --> Navigation``` then press ```Backspace``` to delete the unwanted shortcuts. – mowen Oct 25 '19 at 16:09
  • 1
    Mine is bound to Ctrl+Shift+Alt+DownArrow, but it doesn't actually do anything when I click those buttons. – Aaron Franke Apr 02 '20 at 07:22
  • 1
    for ubuntu according to Mwiza answer Ctrl + Shift + Alt + ↓ – yas17 Aug 15 '20 at 19:07
  • 13
    On Ubuntu 20.04 it's `Ctrl` + `Alt` + `Shift` + `Numpad 2` / `Numpad 8`. – bwdm Aug 05 '21 at 22:06
  • This is an annoying default shortcut for two reasons. First, I always inadvertently hit it when doing multicursor selection (which defaults to Shift + Alt with a mouse but for some reason Shift + _Ctrl_ + Alt with arrow keys). Second, it is hard to hit when right hand is on the mouse which is often the case when you've just copied something from the browser which is when you'd use this over Ctrl+C Ctrl+V to begin with. – gargoylebident May 02 '22 at 05:12
  • Isn't is easier to just not select anything, ```ctrl```+```c``` / ```ctrl```+```v```? It just copies the whole line and pastes it below. – user281681 Dec 20 '22 at 17:11
226

The duplicate can be achieved by CTRL+C and CTRL+V with cursor in the line without nothing selected.

Mwiza
  • 7,780
  • 3
  • 46
  • 42
rylek90
  • 2,666
  • 1
  • 11
  • 10
  • 78
    I 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 08:51
  • 2
    This does not work if `editor.emptySelectionClipboard` option is set to `false` (my preferred configuration). – Bill_Stewart Sep 21 '17 at 16:23
  • 39
    This blows away what I had in my clipboard – Kris Kilton Sep 18 '19 at 16:37
  • 4
    One 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 04 '20 at 00:16
  • 6
    This ruins the whole purpose of instant line duplication. – Arman Karimi Jul 23 '20 at 16:52
  • Windows 10 has built-in clipboard history; `Win+V` – darda May 28 '21 at 16:03
  • if i already copy something and wants to duplicate some lines this will not helpful then. – Anupam Hayat Shawon Mar 28 '22 at 07:10
  • Wow. Usefull tip. Works fine for me on ubuntu. – Marcellin Wabo May 18 '22 at 08:49
  • Yes, who doesn't know about Ctrl+C, Ctrl+V? If you don't know that much, then you don't even know about StackOverflow. The idea is that in all IDEs supporting a native "duplicate line" option, this feature does not clobber what is already on your clipboard. That is the ENTIRE reason for needing it. (I downvoted this answer.) – Darrin Dec 01 '22 at 18:13
  • this answer is not what the user asked – Omar N Shamali Dec 05 '22 at 20:17
164

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

Paul Verest
  • 60,022
  • 51
  • 208
  • 332
Mwiza
  • 7,780
  • 3
  • 46
  • 42
34

Ubuntu :

  • Duplicate Line Up : Ctrl + Alt + Shift + 8
  • Duplicate Line Down : Ctrl + Alt + Shift + 2
Sabunkar Tejas Sahailesh
  • 4,436
  • 2
  • 28
  • 32
  • 1
    working perfectly fine for me. thanks for the answer. – singhkumarhemant Dec 27 '19 at 07:30
  • 2
    This 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 – lui May 22 '20 at 14:09
  • 1
    Default options didn't work for me on Ubuntu 20... neither the keys that you showed here then I went to "File > Preferences > Keyboard Shortcuts then editor.action.copyLinesDownAction" changed the Ctrl + Alt + Shift + ArrowDown (default) to Ctrl + Alt + NumPad2 and worked fine! Thanks! – Jhonnatas Alencar Apr 19 '22 at 20:40
  • perfect answer for Ubuntu. – sam Dec 06 '22 at 17:13
32

You can use the following depending on your OS:

Windows:

Shift + Alt + ↓ OR Shift+ Alt + ↑

Mac:

Shift + Option + ↓ OR Shift +Option + ↑

31

These are the default settings:

Windows:

SHIFT+ALT+ or SHIFT+ALT+

macOS:

SHIFT+OPTION+ or SHIFT+OPTION+

However, if you want to duplicate the line with CTRL+D, then you have to modify the keyboard shortcuts of your VS Code.

Windows:

File > Preferences > Keyboard shortcuts

macOS:

Manage > Keyboard shortcuts

and search for Copy Line Down. From there add CTRL+D or your desired key combination.

enter image description here

Mr. Kabir
  • 695
  • 8
  • 9
18

Mac:

Duplicate Line Down :shift + option +

Duplicate Line Up:shift + option +

Owen Kelvin
  • 14,054
  • 10
  • 41
  • 74
Brittany
  • 181
  • 1
  • 3
16

Use the following: Shift + Alt+( or )

Mwiza
  • 7,780
  • 3
  • 46
  • 42
Luciano Marinho
  • 343
  • 1
  • 5
  • 1
    The best tip here! – Jakub Dec 01 '22 at 14:06
  • This is the BEST answer in my opinion, and is exactly what I was looking for. It sure would be nice if there were just "Universal" standards for keyboard shortcuts that *everybody* who desires to build a reputable product could adhere to. Then, eventually, it would be as commonly understood as "Ctrl-C" which is even typed on keyboards. Why can't we have about 26*3 == 78 universally standardized keyboard shortcuts in the world? 26 for "Ctrl", 26 for "Ctrl+Shift", 26 for "Alt". Then, if products/tools want more than the 78 standard shortcuts, add "Alt+Shift", "Ctrl+Alt+Shift" etc. ? :-) – Darrin Dec 01 '22 at 18:20
14

Search for copyLinesDownAction or copyLinesUpAction in your keyboard shortcuts

Usually, it is SHIFT+ALT+

Stephen Rauch
  • 47,830
  • 31
  • 106
  • 135
Jcesar
  • 149
  • 1
  • 2
11

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.

Mark
  • 143,421
  • 24
  • 428
  • 436
1

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"
}
Luciano Marinho
  • 343
  • 1
  • 5
1

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
1

Though there are some right answers already here, I always forget shortcuts in different IDEs and Text Editors especially switching between Windows and Linux.

In Windows VSCode v1.77.3, I've just found that Ctrl+C, Ctrl+V works perfect and easy to remember:

  1. Set cursor on the line you want to duplicate. (Don't select anything)
  2. Ctrl+C
  3. Ctrl+V (press as many as the line count you want to get)
elshev
  • 1,164
  • 3
  • 16
  • 30
0

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;

Carnaru Valentin
  • 1,690
  • 17
  • 27
0

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

Original Keybindings

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 +

New Keybindings

Terrence
  • 31
  • 1
  • 3
0

To copy the current line without selection, ⌘ Command + C will do the trick. For those of you who have vim enabled on VSCode, you will need to ensure that the following is set:

"vim.overrideCopy": false
jyt
  • 15
  • 2
  • 5
-2

Windows:

Duplicate Line Down : Ctrl + Shift + D

Umair Sultan
  • 618
  • 6
  • 10