908

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.

Sahan Serasinghe
  • 1,390
  • 15
  • 28
Chris
  • 43,908
  • 26
  • 122
  • 165
  • Does this answer your question? [Duplicate line in Visual Studio Code](https://stackoverflow.com/questions/38727047/duplicate-line-in-visual-studio-code) – Arash.Zandi Jun 04 '20 at 18:04
  • I've also posted an answer here that might help: https://stackoverflow.com/a/67620196/9868445 . It shows you how you can duplicate a __selection__, but it also does a bit more: Goes to the next line, and generates a print statement of that selection, in the active file's language. – aderchox May 20 '21 at 13:27

19 Answers19

1647

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.

jonasfh
  • 3,403
  • 2
  • 19
  • 34
Benjamin Pasero
  • 85,636
  • 11
  • 70
  • 52
  • 79
    And it seems `Alt+Up` or `Alt+Down` moves an existing line up and down. Nice – Chris May 13 '15 at 06:19
  • 3
    It's worth noting that the copyLines actions do not exist in the full Visual Studio, only Visual Studio Code. – user45623 Jan 08 '16 at 00:09
  • 4
    **Tools | Options | Environment | Keyboard** in VS2015 – Mike Keskinov Sep 09 '16 at 19:01
  • 19
    Do you know how to "duplicate selection"? I couldn't find appropriate command :( – nZeus Oct 11 '16 at 20:37
  • @nZeus For me it was the same `editor.action.copyLinesDownAction` I use for both duplicate line and selection. – Vadorequest Nov 24 '16 at 15:06
  • 3
    That is not the same. In this case when you select a _line ending + full next line_ it will copy 2 lines (the whole first line, not just the \r\n). That's very annoying :( – nZeus Nov 24 '16 at 22:28
  • @nZeus You can use https://marketplace.visualstudio.com/items?itemName=rivenvirus.duplicatetext – jpillora Jan 11 '17 at 14:11
  • 2
    Default `ctrl+shift+alt+down` didn't work for me on ubuntu. It worked though when reassigned to `ctrl+shift+d`. – Sergiy Ostrovsky Mar 15 '17 at 15:09
  • 34
    On Linux `ctrl+shift+alt+down/up` only works for the num pad arrow keys. – Jack Steam May 11 '17 at 16:08
  • That's copying the line, not duplicating it. See [my answer here](https://stackoverflow.com/a/48208930/11343) for duplication :) – CharlesB Jan 12 '18 at 08:41
  • 3
    Ctrl-Shift-Alt-Down/Up not working on Ubuntu 17.10.1 - this works as a workplace shifter for Gnome. – connexo Apr 05 '18 at 16:01
  • 1
    `Ctrl-Shift-Alt-Down/Up` is such a painful shortcut for this common sortcut :( I liked `Ctrl-D` in the Pycharm – iraj jelodari Apr 28 '18 at 19:34
  • 1
    Ctrl+Shift+Alt+Down/Up will switch workspaces because Ctrl+Alt+Down/Up is use for switching workspaces in Ubuntu. Define your own shortcut – sziraqui Jul 06 '18 at 10:02
  • Notice that `Alt+Shift-Up/Down` in JSON documents doesn't position the comma correctly when moving-up a line at the end of the document. – Meetai.com Jan 09 '19 at 17:05
  • i am using vscode 1.4 it (SHIFT + ALT + DOWN) is not working dear – Kamlesh Dec 22 '19 at 10:37
  • i have selected one row text, now want to duplicate this in next / bottom row by keyboard, how can i do? – Kamlesh Dec 22 '19 at 10:39
  • 1
    You can install 'IntelliJ IDEA Keybindings' extension, It will give you very useful other key bindings as well. – Sam Jan 11 '20 at 05:44
  • So in a Linux laptop without numpad the shortcut doesn't work. – Jean Paul Oct 07 '20 at 09:54
  • CTRL+C and CTRL+V simpler on linux ( – SanŚ́́́́Ý́́́́Ś́́́́ Jan 14 '21 at 08:57
  • Oddly, on Linux, `Add Cursor Above` and `Add Cursor Below` had two keybindings. One of which was the default Windows shortcut for `Copy Line Up / Down`. Not sure what the practical purpose of **When `!editorReadonly`** was, but I've reassigned the keybinding to have the same shortcut on Linux as Windows. – Andy2K11 May 01 '21 at 16:19
197

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"
    }
]
Sahan Serasinghe
  • 1,390
  • 15
  • 28
manukn
  • 2,312
  • 1
  • 10
  • 17
  • Also, if you have to move a line far, ctrl+C and ctrl+X will easily copy/cut the current line without having to select it. And `ctrl + L` (command **expandLineSelection**) will select a line. Repeating ctrl + L selects additional lines making a block select easy. Or you can make a rough block select with mouse and then ctrl+L to include the ends. – aamarks Jun 15 '19 at 06:16
  • Sadly there is nothing at all in default PDF with list of the shortcuts about Alt+Shift+up/down... Not quite true, it is mentioned "Multi cursor and selection" section with "Insert cursor above/below" action! – Marecky Dec 30 '20 at 00:28
  • TKS, shift+alt+arrow is working – datdinhquoc May 23 '21 at 03:16
72

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.

  • 1
    I was frustrated by "copy lines down" shortcut which did not exactly behave like Sublime Text/Intellij Ctrl+D feature (it copied the entire lines instead of just the selection). Thanks for pointing out this plugin! – Timothé Malahieude Feb 02 '20 at 18:24
  • I was about to ditch vscode and install atom, but this extension has prevented me. the default duplicate functionality of vscode is not up to the mark. atom is so much better in this regard. – vikrant Mar 04 '20 at 07:26
36

In VScode, they call this Copy Line Up and Copy Line Down

From the menu, go to:

File > Preferences > Keyboard Shortcuts

Check already assigned keyboard shortcut for this, or adjust yours.

Sometimes the default assigned shortcut may not work, mostly because of OS.

In my Ubuntu, I adjusted this to: Ctrl+Shift+D

enter image description here

Nahid
  • 2,657
  • 1
  • 18
  • 15
32

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.

Nick De Beer
  • 4,867
  • 4
  • 33
  • 47
24

Try ALT+SHIFT+UP/DOWN

It worked for me!

Sahan Serasinghe
  • 1,390
  • 15
  • 28
eildiz
  • 439
  • 1
  • 5
  • 13
14

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.

Misha Akopov
  • 9,542
  • 26
  • 54
  • 73
David Bodow
  • 613
  • 5
  • 15
11
  • For Jetbrains IDE Users who migrated to VSCode , no problem.

  • Install:
    1) JetBrains IDE Keymap: Extension
    2) vscode-intellij-idea-keybindings Extension (Preferred)

  • Use this Intellij Darcula Theme: Extension

  • The keymap has covered most of keyboard shortcuts of VS Code, and makes VS Code more 'JetBrains IDE like'.

  • Above extensions imports keybindings from JetBrains to VS Code. After installing the extension and restarting VS Code you can use VS Code just like IntelliJ IDEA, Webstorm, PyCharm, etc.

Sumit Shukla
  • 2,722
  • 3
  • 21
  • 30
11

Simply go to the file -> preferences -> keyboard shortcuts There you can change any shortcut you like. search for duplicate and change it to whatever you always use in other editors. I changed to ctrl + D

Amin Noura
  • 180
  • 1
  • 10
  • 3
    Absolutely my thought right now. I tried the accepted answer on linux and it simply did NOT work. Editing your own shortcuts is a great practice and a new start for a better future. CTRL + D is the most intuitive and easy shortcut command for this. Why .....why...... CTRL + SHIFT + ALT + .. please stop, my computer almost shut down and reset after doing this. – xdevx32 Jun 09 '20 at 07:50
10

For Windows :

To Copy Up - shift+alt+up

To Copy Down - shift+alt+down

For mac :

To Copy Up - shift+option+up

To Copy Down - shift+option+down

For linux :

To Copy Up - ctrl+shift+alt+8

To Copy Down - ctrl+shift+alt+2

Note : You may change your keyboard shortcuts keybinding for visual studio code by pressing ctrl+shift+p, then type open keyboard shortcut in the pallet and then hit enter so new file will be opened (Key board shortcut file) you can see all the shortcuts over their and can change keybindings by clicking twice over the respective keybinding and then by entering your own keybinding, finally hit enter.

Hope this will help somebody!

Aman Kumar Gupta
  • 1,247
  • 12
  • 13
7

If you coming from Sublime Text and do not want to relearn new key binding, you can use this extension for Visual Code Studio.

Sublime Text Keymap for VS Code

This extension ports the most popular Sublime Text keyboard shortcuts to Visual Studio Code. After installing the extension and restarting VS Code your favorite keyboard shortcuts from Sublime Text are now available.

https://marketplace.visualstudio.com/items?itemName=ms-vscode.sublime-keybindings

Community
  • 1
  • 1
cyberfly
  • 5,088
  • 8
  • 41
  • 61
3

Direct line duplication (without the clipboard step) is proposed by the Contextual Duplicate extension.

It is mapped on ctrl+K D

CharlesB
  • 75,315
  • 26
  • 174
  • 199
3

This keymap will duplicate the current line if nothing is selected or duplicate the selected words.

  1. Access:

    File/Preferences/Keyboard shortcuts

  2. In the right top corner, click on Open Keyboard Shortcuts (JSON).

  3. Insert this keymap:

     {
       "key": "shift+alt+d",
       "command": "editor.action.duplicateSelection"
     }
    
  4. Use alt+arrows up/down to move the line (Default behaviour of VS Code).

Seralto
  • 908
  • 14
  • 29
1

For Fedora 29 workstation (Gnome 3.30.2) and Ubuntu users.

Unbind unnecessary left/right workspace keyboard combinations, list them by terminal

$ gsettings list-recursively | grep -E "org.gnome.desktop.wm.keybindings move-to-workspace-|org.gnome.desktop.wm.keybindings switch-to-workspace-"

Unbind them

$ gsettings set org.gnome.desktop.wm.keybindings switch-to-workspace-left "[]"
$ gsettings set org.gnome.desktop.wm.keybindings switch-to-workspace-right "[]"
$ gsettings set org.gnome.desktop.wm.keybindings move-to-workspace-left "[]"
$ gsettings set org.gnome.desktop.wm.keybindings move-to-workspace-right "[]"

Reset duplicate shortcuts

  • Super+Pgdown/PgUp , Ctrl+Alt+DownArrow/UpArrow
  • Super+Shift+PgDown/PgUp , Ctrl+Alt+Shift+DownArrow/UpArrow

They can be easily reset to work with only one shortcut in Settings>Devices>Keyboard
Only type again Super+PgUp for "Move to workspace above" as an example.

enter image description here

Now with less duplicate shortcuts in fedora29 all vscode shortcuts for linux must work fine

christianbueno.1
  • 438
  • 5
  • 10
1

Problem

There seems to be a problem with the original "duplicate line down" shortcut on Ubuntu, mostly due to a conflict with an already existing workspace related shortcut on the operating system.

Workaround

However, an easy workaround is to simply ctrl+c (copies the entire line) and ctrl+v (pastes the copied line on to a new one)... Effectively, giving you the same end result.

Grateful
  • 7,657
  • 7
  • 34
  • 59
1

For those migrating from WebStorm/PhpStorm,

You could install IntelliJ IDEA Keybindings to keep using almost all the keyboard shortcuts as you did in Webstorm/Phpstorm.

So,

  • Duplicate lines => CTRL + D
  • Move a line/selection of code Up/Down => Ctrl + Shift + UP/DOWN

Also, here is a list of recommended VS Code extensions that will make your transition from WebStorm/Phpstorm much easier.

Niket Pathak
  • 4,417
  • 1
  • 30
  • 38
0

For Linux users: I noticed what on Linux you quite an often need to use win key. For Windows combo is:
ctrl + shift + alt + up

then for Linux is same just add win key:
ctrl + shift + win + alt + up

I noticed that in a few combos now. Say ctrl + alt + L locks Linux, but ctrl + win + alt + L for Intellij formats code. Under Windows is just ctrl + alt + L to format the code.

my-
  • 490
  • 5
  • 16
0

Duplicate line: alt + shift + /

Move line up/down: alt + /

Deja
  • 3,278
  • 2
  • 18
  • 46
-1

in my last version Visual Studio Code 1.30.2 it wil be change automatically into


ctrl + D

Misha Akopov
  • 9,542
  • 26
  • 54
  • 73
  • In vscode this highlights the word under the cursor. Perhaps Notepad++ used the ctrl+D shortcut. Just going from memory. – Flat Cat Mar 15 '19 at 15:01