Visual Studio Code (VS Code) provides powerful features for code editing, including efficient ways to duplicate and move lines of code. These capabilities can significantly speed up your development workflow, especially when dealing with repetitive structures or refactoring code. This tutorial will cover several methods for achieving this, from built-in keyboard shortcuts to extensions that mimic the behavior of popular editors like Sublime Text and JetBrains IDEs.
Duplicating Lines of Code
There are multiple ways to duplicate a line (or selection) in VS Code:
1. Using Keyboard Shortcuts (Built-in)
VS Code provides built-in keyboard shortcuts for duplicating lines both above and below the current line:
- Copy Line Down: This duplicates the current line and inserts the copy immediately below it.
- Copy Line Up: This duplicates the current line and inserts the copy immediately above it.
The default keybindings are:
| Operating System | Keybinding |
|——————|———————-|
| Windows | Shift + Alt + Down
/ Shift + Alt + Up
|
| macOS | Shift + Option + Down
/ Shift + Option + Up
|
| Linux | Ctrl + Shift + Alt + Down
/ Ctrl + Shift + Alt + Up
|
2. Simple Copy-Paste
A straightforward method is to use the standard copy and paste commands:
- Select the line(s) you want to duplicate.
- Press
Ctrl + C
(orCmd + C
on macOS) to copy. - Position the cursor where you want to insert the duplicated line(s).
- Press
Ctrl + V
(orCmd + V
on macOS) to paste.
While this works, it requires more keystrokes than the dedicated duplication shortcuts.
3. Using Extensions for Sublime/JetBrains-like Behavior
If you’re accustomed to the line duplication behavior in Sublime Text or JetBrains IDEs (using Ctrl + D
or Cmd + D
), you can install a VS Code extension to replicate that functionality:
- Duplicate selection or line: This extension adds bindings for
Ctrl + D
(Windows/Linux) andCmd + D
(macOS) to duplicate the selected text or the current line.
To install the extension:
- Open VS Code.
- Click on the Extensions icon in the Activity Bar (or press
Ctrl + Shift + X
/Cmd + Shift + X
). - Search for "Duplicate selection or line".
- Click the "Install" button.
After installation, the Ctrl + D
/ Cmd + D
shortcut will duplicate the selected text or line. You may need to remove or adjust any existing keybindings for Ctrl+D
to avoid conflicts.
Moving Lines of Code
VS Code also offers convenient methods for moving lines of code:
1. Using Keyboard Shortcuts (Built-in)
- Move Line Down: Moves the current line down one position.
- Move Line Up: Moves the current line up one position.
The default keybindings are:
| Operating System | Keybinding |
|——————|———————-|
| Windows | Alt + Down
/ Alt + Up
|
| macOS | Option + Down
/ Option + Up
|
| Linux | Ctrl + Down
/ Ctrl + Up
|
2. Drag and Drop
You can also move lines of code using drag and drop:
- Click and hold on the line number (the area to the left of the code).
- Drag the line up or down to the desired position.
- Release the mouse button to drop the line.
Customizing Keyboard Shortcuts
VS Code allows you to customize keyboard shortcuts to suit your preferences. To do this:
- Open VS Code.
- Go to
File > Preferences > Keyboard Shortcuts
(or pressCtrl + K Ctrl + S
/Cmd + K Cmd + S
). - Search for the command you want to customize (e.g., "editor.action.copyLinesDownAction", "editor.action.moveLinesDownAction").
- Double-click on the command or right-click and select "Change Keybinding".
- Enter your desired key combination and press Enter.
Remember to check for conflicts with other commands before assigning a new keybinding. You can also edit the keybindings.json
file directly for more advanced customization.
By mastering these techniques, you can significantly enhance your coding efficiency in VS Code. Choose the methods that best fit your workflow and customize the keybindings to create a comfortable and productive coding environment.