As a programmer you spend a lot of time typing on a keyboard, well that is if you aren’t stuck in meetings all day. In order to optimize the time you spend in front of a keyboard, it is best to learn useful keyboard shortcuts that can save you a few seconds every time you use them. That may not sound like a lot, but if you are using these shortcuts hundreds or thousands of times a day you could end up saving yourself hours of work each week.
Here, in this article, I will be covering some of my favorite keyboard shortcuts that work in nearly all text editors. These shortcuts may not work in all editors, though, but they are guaranteed to work in Visual Studio Code. Also, if you are on a Mac computer just replace any use of Ctrl with Cmd and you should be good to go.
-> Copy/Cut/Paste Entire Lines
You are probably already familiar with how to cut/copy/paste highlighted sections of code by using
ctrl + x,
ctrl + c, and
ctrl + v respectively, but you can actually take this a step further by copying an entire line at a time. If you just place your cursor on a line without highlighting anything and press
ctrl + c it will copy the entire line your cursor is on including the line break. Then if you press
ctrl + v it will paste the entire line directly above the line your cursor is currently on.
-> Toggle Comments
When you are writing or debugging code you commonly need to comment out individual sections. This can be tedious to do manually which is why most editors allow you to comment out all of the code on the lines you have highlighted by pressing
ctrl + /. Also, if you do not have any code highlighted at all when pressing this shortcut it will comment out the line your cursor is on which is really useful for quickly turning on or off logging statements.
-> Open File Within Project
If you spend time searching through the hundreds of nested folders in your sidebar to find a particular file then you are wasting time that can be saved by a keyboard shortcut. By pressing
ctrl + p you can open up a search box that will search all of the files in your project for whatever you type into it.
-> Find In File/Project
If you need to search for a particular set of text inside of the file you have open just press
ctrl + f. This will open a search box that you can type into and it will find all matches to your search within your current file.
Sometimes you need to search across your entire project, though. To do that you can just press
ctrl + shift + f and that will open a search box that will search for the entered text across all files in your current project. This is incredibly useful when refactoring code.
-> Create New File
While programming a new feature you are sure to create many new files. Normally this is done with the mouse by selecting the new file button, but you can actually create a new file just by typing the shortcut
ctrl + n. This will immediately open a new file for you to work in.
-> Create A New Line Below The Current Line
This is an incredibly useful trick. Normally if you want to add a new line below your current line you need to move your cursor to the end of the current line and press enter. This is a bit of a pain to do so instead you can just press the shortcut
ctrl + enter and a new line will be added below your current line without needing to move your cursor to the end of the line.
-> Highlight Characters Of Code
If you want to highlight a section of code next to your cursor you can use
shift + left arrow and
shift + right arrow to move your cursor one character at a time to the left or right and also highlight the character you move over at the same time. This is great for selecting small typos in your code to fix or to select small sections of code to copy and paste. You can even combine this with the previous shortcut by pressing
ctrl + shift + left/right arrow to highlight entire words at a time instead of just one character.
Here are the last few shortcuts that you need to know. They are all fairly common and you most likely know them all already.
- Save File
Use the shortcut
ctrl + s to save your currently open file.
- Select Entire File
ctrl + a you can highlight all of the code inside a single file.
If you need to undo a change you made just press
ctrl + z. In order to redo the change, you just undid you can press
ctrl + shift + z or
ctrl + y. They will both do the same thing.
These are just the beginning of the hundreds of keyboard shortcuts available but in my opinion, are the most useful shortcuts you can use to become a more productive programmer.