Essential VS Code shortcuts every Python developer should know

Have you ever learned a keyboard shortcut that changed your life? I still remember how eye-opening it felt when I learned how to copy and paste using Ctrl-C and Ctrl-V in elementary school: just think about how much time I’ve saved since then!

Just like keyboard shortcuts can save you time and inconvenience when writing in a document, the same is true for writing code! Even just the switch from clicking the “Run” button in VS Code to running from my keyboard (Ctrl-Enter if you aren’t familiar) has saved me at least a few hours over years of writing code. I’ve compiled a list of my favorite, most life-changing shortcuts in VS Code notebooks.

For my examples, I’m going to be using VS Code in SAS Viya Workbench. This allows me to show how these shortcuts can be applied when using both Python and SAS notebooks. For SAS programmers, for whom notebooks are a relatively new way to code arriving alongside the release of SAS Viya Workbench, these shortcuts are likely unfamiliar. However, this is a post for Python lovers, too! As a Python programmer who has used Jupyter notebooks for years, I still only found these recently and have been amazed at how much faster I can code.

Cell Shortcuts

To create and edit cells in notebooks using keyboard shortcuts, a crucial discovery was command mode. By simply clicking Esc, we enter command mode and can use simple shortcuts to create new code and markdown cells, move cells, change cell types, and delete cells.

Before using the following shortcuts, ensure you are in command mode by pressing Esc. To exit command mode, press Enter.

Cell Command | Keyboard Shortcut
Insert Cell Above | A
Insert Cell Below | B
Insert Markdown Above | AM
Insert Markdown Below | BM
Change Code Cell to Markdown | M
Change Markdown to Code Cell | Y
Delete Cell | DD
Move Cell Up/Down | ALT-Arrow (or Option-Arrow for Mac)

Navigating VS Code

Outside of cell creation and editing, we can also use keyboard shortcuts to more quickly navigate VS Code. From opening a terminal to copying a file path, here are my favorite time-savers:

File Search

Use this command to quickly search for a file without having to look through the explorer pane (or even touch your mouse!)

Open Terminal

Quickly open the terminal. When coding in Python, this is my most commonly used shortcut when I need to install new packages!

Copy Path of Active File

When accessing data, I often need to copy the file path of a CSV or other data type. With this shortcut, I can quickly reference my data in my code – both in SAS and Python!

Access Source Control

When using Github within Workbench, the source control pane is a commonly used feature. Access it quickly with a shortcut.

Open and Close the Left Pane

Use this shortcut when you need more space for your code and don’t need to access the explorer, source control, extensions, or other features on the left pane.

Open Command Bar

If there are commands you don’t use often enough to memorize a keyboard shortcut, you can search them in the command bar. Use this shortcut to easily access any command from your keyboard.

Command | Windows Shortcut | Mac Shortcut
Search for and navigate to a file | Ctrl-P | Ctrl-P
Open terminal | Ctrl-Shift-` | Ctrl-Shift-`
Copy path of active file | Shift-Alt-C | Option-Command-C
Access source control | Ctrl-Shift-G | Ctrl-Shift-G
Open and close left pane | Ctrl-B | Command-B
Open command bar | Ctrl-Shift-P | Command-Shift-P

Creating Your Own Shortcuts

If you find yourself doing the same function over and over in VS Code, but there isn’t a built-in keyboard shortcut, it’s easy to create one yourself! In this section, I’ll show the process for creating your own shortcut and show some of the ones I’ve created that I’ve found most helpful in Workbench.

Below, I show how to create a new shortcut. In this example, I create a shortcut for creating a new SAS notebook in Workbench. First, navigate to the keyboard shortcuts tab from VS Code settings, then find your desired command, click +, and input your desired shortcut and click Enter. You now have a new keyboard shortcut you can use!

Here are the most helpful shortcuts I’ve created in Workbench:

Creating new SAS files

Create shortcuts to quickly open a new SAS notebook or SAS file.

Run All Cells

While shortcuts exist for SAS notebooks and Jupyter notebooks separately, you can create a shortcut for the “Notebook: Run All” command. This way, I can use the same shortcut to run both SAS notebooks and Jupyter notebooks.

Navigate to SAS Libraries

When coding in SAS, I often reference the SAS libraries left pane. I can create a keyboard shortcut for the “SAS: Focus on Libraries View” command to quickly reference the names of your libraries.

Restart kernel

Finally, when coding in Python and importing new libraries, I often need to restart the kernel to make use of my new library. Using this shortcut, it’s easy to quickly restart and re-run.

Anything else you can think of!

No one knows how you use VS Code like you do! Any commands you find yourself manually using again and again, consider making a shortcut to save you time. I hope you find this as helpful as I have!

Learn more

SAS® Viya® Workbench Demo: Solving Your Productivity Challenges

Download this e-book: Five reasons developers hit a productivity wall – and what to do about it

5 reasons to use VS Code for your SAS code projects

Install VS Code extensions offline: step-by-step guide

SAS Extension for VS Code video series

Essential VS Code shortcuts every Python developer should know was published on SAS Users.