Best VS Code shortcuts to write code more efficiently
In this article, you will find some very useful keyboard shortcuts that aim to make writing code in VS Code more easily, quickly and safely.
These shortcuts apply to MAC users, but there is also a list for Windows and Linux users.
You can find even more tips and tricks in the official VS Code documentation:
> https://code.visualstudio.com/docs/getstarted/tips-and-tricks
Stay tune ! There is a bonus at the end !
Moving around the code
Go to Beginning of Line: CTRL + A
Go to End of line: CTRL + E
Go to Beginning of code (first character) in this line: CMD + ←
Go to End of code (last character) in this line: CMD + →
Go to Beginning of file: CMD + ↑
Go to End of file: CMD + ↓
Moving code around !
Move an entire portion of code (line or block) up / down
ALT + ↑
ALT + ↓
Selecting more quickly !
Highlight from cursor position to last character (left to right)
CTRL + SHIFT + E
Highlight from cursor position to first character (right to left)
CTRL + SHIFT + A
Highlight words by words
ALT + SHIFT + →
ALT + SHIFT + ←
Multiply cursor up or down
My favourite by far ! Make your life so easy
CMD + ALT + ↑
CMD + ALT + ↓
Add Selection to next find match
Imagine a code block with 5 occurrences of the variable server
You want to change the name of this variable from server
to httpServer
The code block is (let’s say) 20 lines long, and the server
variable is located at random places.
You could go with the mouse click trick mentioned above, by clicking in front of every occurrence of server
while holding SHIFT key.
But there is much faster !
CMD + D
- Click on the variable name
- CMD + D (first time): it will copy the name in the search field
- CMD + D : it will put the cursor in front of the next occurrence
- CMD + D : x2, x3, x4… as many times as you need to select
Bonus ! Select all occurrences of the same word !
- CMD + F : this will display the search field
- Enter the keyword you are looking for
- ALT + Enter: this will put the cursor in front of every occurrence of the word
Inserting !
Insert a comment / Commenting a line of code
CMD + /
Deleting !
What a better feeling that deleting code !
But how complicated it becomes when you have to delete specific lines, or specific portions in a line.
Have you ever experience the slow feeling, where you have to highlight manually what you want to delete?
Here are some ways to make it faster.
Delete — from right to left
CMD + DEL (delete char, backspace)
CTRL + K
If your cursor is positioned in the middle of the code, it will delete everything on the left.
If your cursor is positioned at the beginning of the line, it will delete the entire line.
Delete — from left to right
CMD + DELETE
If your cursor is positioned in the middle, it will delete everything on the right.
If your cursor is positioned at the end of the line, it will delete the entire line.
Delete an entire word
ALT + DEL
Delete an entire line
CMD + X
In a line of code, whether your cursor is positioned at the beginning, in the middle, or at the end, this will delete the entire line.
Duplicating / Copying
Duplicate a line of code + insert it above
SHIFT + ALT + ↑
This will copy the line of code where your cursor is positioned, and insert it above.
Duplicate aline of code + insert it below
SHIFT + ALT + ↓
This will copy the line of code where your cursor is positioned, and insert it above.
Juggling between files
Switch to previous / next file
CMD + ALT + ←
CMD + ALT + →
Switch between different files / editors
If you have the following 3 files opened in this order:
- client.c
- server.c
- command.c
- etc…
CMD + 1: switch to client.c
CMD + 2: switch to server.c
CMD + 3: switch to command.c
CMD + n…: etc…
Moving inside VS Code !
Show / Hide VS Code terminal
CMD + J
Show / Hide Sidebar
CMD + B
Open Command Palette
CMD + SHIFT + P
Quickly open a file
CMD + P
Very useful when you are working on a big project with a lot of files.
- CMD + P
- Type the file name in the search field that appear
Change Language
CMD + K M
Zen Mode
For those who like to write code distraction free !
CMD + K followed by Z
Mouse Tricks !
Here are some good tricks
Opening files
Switch between opened files: hold SHIFT + scroll) (your mouse pointer should be over the file tabs area)
Open file in new tab: scroll click
Open file on right side (right split window): hold ALT + click
Navigating through code
Scroll code horizontally: hold SHIFT + scroll (your mouse pointer should be over the code field)
Multiple Selection
hold ALT + click