Learn keyboard shortcuts that will make you a more efficient and productive Python programmer with VS Code.

Introduction

As a Python programmer, I spend my day inside my IDE. After all, it's my job!

This article will share some VS Code keyboard shortcuts that save me hours every week and make me a much more productive Python programmer. I will explain what each shortcut does and I will show a short video example.

(I will show the specific keyboard shortcuts for VS Code, but these actions are common and your IDE is likely to have a similar shortcut!)

Open command palette

All the shortcuts that will be shown are bound to a specific action and the shortcuts can always be edited to better suit your needs. If you forget a specific shortcut, you can open the command palette (which also shows all commands available) and you can type the name of the command you want to see the shortcut for.

To open the command palette, use:

  • Cmd + Shift + P on Mac;
  • Ctrl + Shift + P elsewhere.

Edit multiple occurrences of the same word

To edit multiple occurrences of the same word (or of the same selection of text), use:

  • Cmd + D on Mac;
  • Ctrl + D elsewhere.

Each time you press the shortcut, you select the next occurrence of the word. When you have selected as many occurrences as you want, start typing to edit them!

You can also use Ctrl + Shift + L to select all occurrences at once. (Use Cmd instead of Ctrl on a Mac.)

Jump to the definition of any symbol

If you want to jump to the definition of something (for example, a variable, an attribute, a function, etc), you can use:

  • Cmd + Shift + O on Mac;
  • Ctrl + Shift + O elsewhere.

When you press this combination, you will see a dropdown with all the symbols in the current file. You can start typing the name of the thing you want to find and select it to jump to its definition.

This is useful when you are in a big file and you want to find a specific function, for example.

Jump to the definition of the current symbol

Similarly, if you want to jump to the definition of a symbol when you are in a place where it is used, you can hold Ctrl (or Cmd on Mac) and click the symbol. This will jump to the definition site of that symbol. Conversely, if you click a symbol in its definition site, it will open a pane with all the usages of that symbol that VS Code can find.

Note that this pane is not guaranteed to list all usages; for example, this can fail on highly dynamic code that uses getattr, hasattr, and setattr.

Rename a symbol

To rename a symbol, like a variable, a class, a function, a method, etc, you can press F2 on top of the symbol. This will rename all occurrences of the symbol that VS Code can find in the current project.

Note that it is not guaranteed that this will rename all occurrences; for example, this can fail on highly dynamic code that uses getattr, hasattr, and setattr.

You can also preview the change before making it.

Jump to a specific line

To jump to a specific line number, you can use Ctrl + G (for example, after seeing a traceback with a specific line number).

Jump to previous position of the cursor

After jumping around a lot, you may need to go back to where you were and you may not remember. To go back to your previous cursor position, use:

  • Ctrl + - on Mac;
  • Ctrl + Alt + - elsewhere.

Conclusion

These are some shortcuts I use very often. These shortcuts can be edited and configured to better suit your taste.

Become a better Python 🐍 developer 🚀

+35 chapters. +400 pages. Hundreds of examples. Over 30,000 readers!

My book “Pydon'ts” teaches you how to write elegant, expressive, and Pythonic code, to help you become a better developer. >>> Download it here 🐍🚀.

Previous Post Next Post

Blog Comments powered by Disqus.