In programming you often need to type a lot of the same code and switch between mouse and keyboard. Apart from increasing tension on your hands, that also creates a distraction from focusing on the actual task. Shortcuts might help with these issues (to an extent of course).
I’ve recently read a blog post on the similar topic and found a few new shortcuts I haven’t used before. And, as for me the best way to learn and memorize something is writing about my own experience with it, here are some thoughts about the shortcuts I use (or want to start using).
As I use VS Code as my code editor, I will primarily talk about its features.
There are of course a lot of built-in shortcuts (I guess we can also call them keyboard keys combinations) in any editor. And when it comes to extensions and personalization, the possibilities are countless, so this will be just a fraction of them that so far I found useful.
There is no particular order or preference, just a list with a brief description and a few thoughts about the shortcuts usage.
Ctrl + P — go to file. It opens the search dropdown where you can type and choose files in your project that you want to open. This way you don’t need to look for them by clicking through the folders in the explorer section on the right side of the editor.
Ctrl + Shift + F — switch to file. It opens the search section on the right section of the editor where you can search for particular words/code lines/etc., so you don’t need to open it with a mouse. Here we can also mention Ctrl + Shift + E — switch to the explorer section of the editor (the one that is usually opened by default). Of course there is Ctrl + F to open a search menu in the current opened file (it’s pretty much the same in any text editor or browser, so I assume most people know about it already).
Ctrl + , — show settings. It opens VS Code settings tab. This one is pretty straightforward. If you need to tweak the editor settings you use this shortcut.
Side note: these first three shortcuts are also shown in the main section of the VS Code when no tab is opened, so you can always check them from there.
Ctrl + F4 — close currently opened tab. It also remembers the order of the tabs you were on before the current one and will redirect you according to it. Another shortcut to mention along with this one is Ctrl + Shift + T — same as in the browser, it will open previously closed tab(s).
Ctrl + S — save currently opened file. If you prefer saving manually, this one can simplify the process (personally, I also have Prettier set to format on save and mostly use this one to put my code in order periodically).
I think the above mentioned shortcuts can be considered as related to the navigation/files-management. Let’s check the ones that are closer to the actual coding now.
Ctrl + C, Ctrl + X, Ctrl + V — copy/cut/paste shortcuts. One nice thing about these commands is that if you place your cursor on a line you want to copy/cut and use the appropriate shortcut, the entire line will be copied/cut so you don’t need to highlight it. Also, if after that you paste this line, it will be inserted above the line with your cursor.
Ctrl + Z, Ctrl + Y— let’s mention two other popular ones to deal with them right away. Undo/redo. These are pretty much the same as everywhere else.
Side note: as someone who’s been using mice with more than five additional buttons for years, I constantly bind copy/cut/paste/undo/redo to these buttons. Not sure if it suits everyone, but if you don’t mind installing additional software for your mouse to program its buttons, this might come in handy. Yes, we are switching from the keyboard in this case, but there are still a lot of situations when you go through your code using mouse and being able to perform some keyboard functions with it might help a bit. Personally, I’ve also found that copy/cut/paste/undo/redo (in that order of preference) are usually among the commands I perform a lot while using the mouse.
Ctrl + / — comment code. For quite some time I’ve been using Ctrl + K + C and Ctrl + K + U (which is already a bit too much work since both of them comment/uncomment the needed sections, basically doing the same task), but recently found out that Ctrl + / can be used instead. Less keys with the same result.
Tab / Shift + Tab — indent/unindent lines of code. Pretty simple one. Indents/unindents line(s) of code by one tab. I use these ones less than the others due to the editor’s auto-indentation (plus thanks to the tools like Prettier), but still it’s worth remembering this one if you want to use tab instead of the spacebar.
Shift + Alt + down arrow — copy the highlighted section of the code below it (using up arrow will do the same but above it). Same as with the copy/cut shortcut, if nothing is highlighted, it will copy the entire line your cursor is currently on.
Ctrl + Enter — create a new line below the current one. This way you don’t need to move your cursor to the end of the line and press enter. No matter where the cursor is, this will add a new line beneath the current one.
Ctrl + Left/Right Arrow — move cursor by words (left or right). Quite useful when you need to jump to a certain point of the code. In that way you don’t need to hold the arrow button or use your mouse.
Shift + Left/Right Arrow — highlight code (by characters). This way you don’t need to switch to mouse to highlight certain sections of your code.
Ctrl + Shift + Left/Right Arrow — highlight code (by words). Sort of an upgraded version of the previous shortcut.
Ctrl + A — highlight the entire code in the currently opened file. Let’s finish with the common and probably well-known one. Sometimes you need to highlight everything and this shortcut is the same in a lot of programs.
Final tip(s): VS Code shortcuts of course customizable. Their list is located in the following section: File — Preferences — Keyboard Shortcuts. This is also the place to check all of them (be ready to see a long list though). Another way to find some of the shortcuts is to check the “on-hover” tooltips on various buttons and menus (many of them show what combination you can use to open/use these buttons and menus). Same goes for the VS Code dropdown menus (only in that case shortcuts are written directly there, as long as they are available). In other words, if you click on the same buttons/menus too often, check their tooltips as there’s a chance that they have a shortcut.
There are a lot of ways to use shortcuts and everyone has their own preferences, here I tried to simply mention a few of the relatively popular ones and some possibilities to adjust them to your own needs.
Thank you for reading.