To move a line up or down in Visual Studio Code, use this keyboard shortcut:
- Windows and Linux:
Alt + ↑ (Up arrow)
to move line up;Alt + ↓ (Down arrow)
to move line down. - Mac:
Option + ↑
to move line up;Option + ↓
to move line down.
Move selection up or down in VS Code
Similarly, to move a selection up or down in Visual Studio Code, use this keyboard shortcut:
- Windows and Linux:
Alt + ↑ (Up arrow)
to move selection up,Alt + ↓ (Down arrow)
to move selection down. - Mac:
Option + ↑
to move selection up,Option + ↓
to move selection down.
Why would you need to move a line/selection up or down in code?
- Refactoring: When cleaning up your code, you may need to move lines of code around in and out of functions and classes, to make it more readable and maintainable.
- Debugging: Like when fixing that error caused by using a variable before declaration/initializing it; with the keyboard shortcuts you easily move the declaration line up before the usage.
- Changing control flow: For those instances where the order of function calls or assignments need to change to reflect a new code logic update.
Commands in Visual Studio Code
In VS Code, we have commands, defined actions that carry our various operations in the editor.
We can easily run a command with the Command Palette, which we can access with these keyboard shortcuts:
- Windows / Linux:
Ctrl + Shit + P
- Mac:
Command + Shift + P
The Move Line Up command in VS Code
To move a line/selection up in Visual Studio Code, we use the Move Line Up command.
Or with the keyboard shortcuts:
- Windows/Linux:
Alt + ↑ (Up arrow)
- Mac:
Option + ↑
The Move Line Down command in VS Code
In the same manner, to move a line/selection down in Visual Studio Code, we use the Move Line Down command.
Or with the keyboard shortcuts:
- Windows/Linux:
Alt + ↓ (Down arrow)
- Mac:
Option +
↓
Change keyboard shortcut to move line up or down
Personally, I think they’re fine, but if you don’t like the keyboard shortcut to move the line up or down, then navigate to the Keyboard Shortcuts
page and change the keybinding for the Move Line Up and Move Line Down commands.
There are multiple ways to get this page; you can click the Keyboard Shortcuts
item on the Manage
popup shown below or use the shortcut next to the text (Ctrl + K Ctrl + S
) here.
To change the keybinding, search for “move line up” or “move line down” in the search bar.
Then double-click on the Move Line Up or Move Line Down command, type a new keyboard shortcut, and press the Enter key to carry out the change.
The change here was Ctrl + E, Ctrl + E
– certainly not the smartest choice, but now you’ve seen how it works.
Key takeaways
- To move a line or selection up or down in Visual Studio Code, use the
Alt + ↑
(Up arrow) for up andAlt + ↓
(Down arrow). - Moving lines or selections up or down in code can be useful for refactoring, debugging, and changing control flow.
- You can change the keyboard shortcut to move a line up or down by changing the Move Line Up and Move Line Down command.