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

前端 未结 17 2137
青春惊慌失措
青春惊慌失措 2020-12-04 04:38

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 be

相关标签:
17条回答
  • 2020-12-04 04:47

    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"
        }
    ]
    
    0 讨论(0)
  • 2020-12-04 04:49

    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!

    0 讨论(0)
  • 2020-12-04 04:49

    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

    0 讨论(0)
  • 2020-12-04 04:52

    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.

    0 讨论(0)
  • 2020-12-04 04:55

    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.

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

    0 讨论(0)
  • 2020-12-04 04:59

    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

    0 讨论(0)
提交回复
热议问题