1165

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)

Pat Myron
4,6774 gold badges30 silver badges52 bronze badges
asked May 13, 2015 at 0:38
2
  • 2
    Does this answer your question? Duplicate line in Visual Studio Code Commented Jun 4, 2020 at 18:04
  • 1
    I've also posted an answer here that might help: 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. Commented May 20, 2021 at 13:27

25 Answers 25

2070

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
4,6572 gold badges24 silver badges42 bronze badges
answered May 13, 2015 at 6:16
Sign up to request clarification or add additional context in comments.

24 Comments

And it seems Alt+Up or Alt+Down moves an existing line up and down. Nice
Tools | Options | Environment | Keyboard in VS2015
Do you know how to "duplicate selection"? I couldn't find appropriate command :(
On Linux ctrl+shift+alt+down/up only works for the num pad arrow keys.
Ctrl-Shift-Alt-Down/Up is such a painful shortcut for this common sortcut :( I liked Ctrl-D in the Pycharm
|
241

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"
 }
]
Samuel RIGAUD
1,6981 gold badge19 silver badges28 bronze badges
answered Feb 3, 2016 at 6:11

4 Comments

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.
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!
I just add my own ctrl+D and remove the conflict other shortcut.
This will not work for selection inside a single line
108

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.

Samuel RIGAUD
1,6981 gold badge19 silver badges28 bronze badges
answered Nov 16, 2018 at 14:15

5 Comments

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!
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.
Ubuntu 22.04: I set copy line down to ctrl-shift-d and it works like in Atom. (Remove it from showing run and debug first.)
Install it with Ctrl+P, ext install geeebe.duplicate Unfortunately the extension isn't yet available for VSCodium. Issue with request here
Keep in mind that by installing this extension it will override vsCode's default key bindings for ctrl+d. : Notebook: Add Find Match to Selection and Add Selection To Next Find Match which are very handy for fast editing code or JSON. You can always remap either of those if you want to keep ctrl+d specifically for "Duplicate selection or line".
37

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

answered Apr 3, 2018 at 0:12

1 Comment

Not quite the same, but an interesting hidden feature nonetheless. Why is this not in the menu?
36

In VSCode Ctrl+CCtrl+V duplicates the whole line below.

It only requires one hand to do this and feels way more natural.

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. I'd rather use the option where my hand naturally sits on the keyboard, and if its one hand, even better.

Pat Myron
4,6774 gold badges30 silver badges52 bronze badges
answered Nov 15, 2018 at 17:15

2 Comments

The main issue with that is you loose what was on your clipboard
This answer has been broken with the 2023 May release of vscode
26

Try ALT+SHIFT+UP/DOWN

It worked for me!

answered Dec 10, 2016 at 18:36

Comments

26
  • 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.

answered Sep 2, 2019 at 17:03

1 Comment

Just the JetBrains IDE Keymap extension is required. This gives you the familiar ctrl-D shortcut.
20

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

Josh Correia
4,4003 gold badges42 silver badges67 bronze badges
answered May 13, 2020 at 9:26

2 Comments

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.
The Command you need to change in the Keyboard shortcuts list is called: Duplicate Selection (editor.action.duplicateSelection)
17

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.

Pat Myron
4,6774 gold badges30 silver badges52 bronze badges
answered Jul 14, 2019 at 9:04

1 Comment

NONE of these suggestions work with VS Ccode 1.49.2 (Electron 9.2.1, Chrome 83.0.4103.122, Node.Js 12.14.1, Linux 5.11.0-36-generic). I wonder with VS Code if it can sometimes be a moving target.
16

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
13.1k27 gold badges72 silver badges87 bronze badges
answered Apr 28, 2018 at 22:44

2 Comments

I'm on Ubuntu 19.10 and Ctrl+Alt+Down still moves between workspaces.
To change your workspace keybindings, tested on Ubuntu 22.04, run in your terminal: gsettings set org.gnome.desktop.wm.keybindings move-to-workspace-down "[]" && gsettings set org.gnome.desktop.wm.keybindings move-to-workspace-up "[]" && gsettings set org.gnome.desktop.wm.keybindings switch-to-workspace-down "[]" && gsettings set org.gnome.desktop.wm.keybindings switch-to-workspace-up "[]"
12

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

answered Feb 14, 2022 at 11:56

Comments

11

Duplicate line: alt + shift + しろさんかく/

Move line up/down: alt + しろさんかく/

answered Apr 30, 2021 at 2:17

Comments

11

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

Samuel RIGAUD
1,6981 gold badge19 silver badges28 bronze badges
answered Jan 8, 2021 at 13:49

1 Comment

This is exactly what I was searching for. Same as in Notepad++ or Sublime. Thanks!
10

In MacOS I usually use:

  • Copy line above shift + option +
  • Copy line bellow shift + option +
answered Oct 21, 2022 at 10:36

1 Comment

With this command, you are only able to duplicate the whole line, its not possible to duplicate just selected text one
8

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

answered Aug 8, 2017 at 8:58

Comments

8

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.

Josh Correia
4,4003 gold badges42 silver badges67 bronze badges
answered Sep 5, 2020 at 12:12

Comments

6

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.

answered Jun 15, 2023 at 9:39

Comments

5

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

VSCODE notepad++ keymap

answered Oct 6, 2024 at 8:57

Comments

3

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

answered Apr 7, 2019 at 18:08

2 Comments

This worked for me in Ubuntu 22.04 although it was "move-to-workspace-down" and "move-to-workspace-up" that I had to modify to get VS Code copy line up/down to work. Annoyingly it doesn't look like you can modify them in the UI but this CLI seems to do it. Thanks muchly!
Single command for Ubuntu 22.04: gsettings set org.gnome.desktop.wm.keybindings move-to-workspace-down "[]" && gsettings set org.gnome.desktop.wm.keybindings move-to-workspace-up "[]" && gsettings set org.gnome.desktop.wm.keybindings switch-to-workspace-down "[]" && gsettings set org.gnome.desktop.wm.keybindings switch-to-workspace-up "[]"
2

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

It is mapped on ctrl+K D

answered Jan 11, 2018 at 14:05

Comments

2

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.

answered Apr 11, 2020 at 12:37

Comments

2

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.

answered Sep 25, 2020 at 11:01

Comments

1

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.

answered Apr 7, 2023 at 8:57

Comments

1

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

General Grievance
5,12039 gold badges39 silver badges58 bronze badges
answered Aug 31, 2022 at 5:06

Comments

-2

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


ctrl + D

Misha Akopov
13.1k27 gold badges72 silver badges87 bronze badges
answered Jan 8, 2019 at 19:25

2 Comments

In vscode this highlights the word under the cursor. Perhaps Notepad++ used the ctrl+D shortcut. Just going from memory.
Yes, Ctrl+D is definitly "Add Next Occurrence". Not something that you'd be using all that often to need a shortcut, and D is a bad mnemonic for it anyway, so VS-Code could just move it and bind Ctrl+D to Duplicate Selection.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.