Chris
Chris

Reputation: 58182

How do I duplicate a line or selection within Visual Studio Code?

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.

Upvotes: 1149

Views: 802898

Answers (25)

Susobhan Das
Susobhan Das

Reputation: 1144

Install VSCode extension Notepad++ keymap, all the Notepad++ keymap including the duplicate line with Ctrl D, will work, flawlessly.

VSCODE notepad++ keymap

Upvotes: 2

manukn
manukn

Reputation: 2888

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"
  }
]

Upvotes: 241

Seralto
Seralto

Reputation: 1076

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).

Upvotes: 9

Antoine Floury
Antoine Floury

Reputation: 1191

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.

Upvotes: 107

Hansana Prabath
Hansana Prabath

Reputation: 141

Try: CTRL + ALT + or (Up Arrow or Down Arrow)

Upvotes: 1

Professor Dragon
Professor Dragon

Reputation: 315

I rebound the Duplicate Selection keybind (editor.action.duplicateSelection) to Ctrl+D, this duplicates the line if nothing is selected or duplicates the current selection if there is.

To open the keyboard shortcuts menu, go to File -> Preferences -> Keyboard Shortcuts or press Ctrl+K then Ctrl+S.

Upvotes: 6

Daria
Daria

Reputation: 11

To duplicate SELECTION within Visual Studio Code (Similar to Sublime's ctrl+shift+d behaviour) you have to setup a shortcut in preferences.

For windows: Open File > Preferences > Keyboard Shortcuts. In the opened tab, search for "Duplicate Selection" and press the edit button in the first column for binding key combination.

Upvotes: 1

K. Stopa
K. Stopa

Reputation: 777

In MacOS I usually use:

  • Copy line above shift + option +
  • Copy line bellow shift + option +

Upvotes: 10

Yatharth Tiwari
Yatharth Tiwari

Reputation: 127

Ctrl + c + v press these keys together not separate for windows

Upvotes: 11

Sumit Shukla
Sumit Shukla

Reputation: 4494

  • For Jetbrains IDE Users who migrated to VSCode.

  • Install(Anyone):

  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.

Upvotes: 27

Grateful
Grateful

Reputation: 10175

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.

Upvotes: 9

Amin Noura
Amin Noura

Reputation: 317

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

Upvotes: 21

nCardot
nCardot

Reputation: 6587

Duplicate line: alt + shift + /

Move line up/down: alt + /

Upvotes: 13

Niket Pathak
Niket Pathak

Reputation: 6800

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.

Upvotes: 2

my-
my-

Reputation: 624

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.

Upvotes: 2

Bouchaib Ennakr
Bouchaib Ennakr

Reputation: 7

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


ctrl + D

Upvotes: -2

David Bodow
David Bodow

Reputation: 717

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.

Upvotes: 15

Aman Kumar Gupta
Aman Kumar Gupta

Reputation: 3011

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!

Upvotes: 18

christianbueno.1
christianbueno.1

Reputation: 592

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

Upvotes: 3

Benjamin Pasero
Benjamin Pasero

Reputation: 123814

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.

Upvotes: 2051

Nick De Beer
Nick De Beer

Reputation: 5262

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.

Upvotes: 37

Nahid
Nahid

Reputation: 3045

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

Upvotes: 39

CharlesB
CharlesB

Reputation: 90316

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

It is mapped on ctrl+K D

Upvotes: 2

cyberfly
cyberfly

Reputation: 5858

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

Upvotes: 8

eildiz
eildiz

Reputation: 537

Try ALT+SHIFT+UP/DOWN

It worked for me!

Upvotes: 27

Related Questions