Some PyCharm tips
Because discussing shortcuts and configuration with coworkers isn't always the most exciting conversation, I wrote it down instead.
Read the "Shortcuts" sectionShortcuts
Read the "Essentials" sectionEssentials
CTRL+SHIFT+A
is the shortcut I find the most useful.
It opens the 'Actions' search modal, an equivalent of the 'command palette' in VS Code.
I use it to:
- do something but I don't know where the button is
- get the keyboard shortcut for something (displayed in search results)
- assign a new shortcut to something (once you found an action, there's an option to create a shortcut for it at the bottom of the search results)
I also recommend the 'Key Promoter X' plugin as a tool to learn new keyboard shortcuts.
Read the "What I use" sectionWhat I use
Your key combination might differ (I've customized some). Use the 'CTRL+SHIFT+A' shortcut to find your specific bindings.
Code:
Ctrl+Space
: Code completion. To useLongClassNameFromThatPackage
I typeLongClass<completion shortcut, multiple times>
, then select the relevant option: the name completes and the import is added if it was missing. Amazing!
Pressing it multiple times triggers different completions, so I make sure to give it a few presses before accepting that maybe PyCharm does not find what I'm looking for.Alt+Enter
: Show intention actions. For example, ifUser
is underlined in red because it's not imported, I place the cursor onUser
and hitalt+enter
. PyCharm offers to import it. Useful in many situations (not just imports!), whenever a 'light bulb' icon appears.ALT+J
: Add a cursor on the next occurrence of selected text. I use the IdeaVim plugin but occasionally toggle out of it for multi-cursor text editing.SHIFT+F6
: Rename variable/file. Part of 'refactoring' options.
Navigate in code:
CTRL+B
: Go to declaration. Also doubles as 'find usages'.Alt+left
: Navigate > Back. Useful after a 'go to definition' to get back to the original place. I customized the bindings for this shortcut.CTRL+SHIFT+ALT+DOWN/UP
: Go to the next/previous 'version control change'. I changed the bindings toALT+N/ALT+B
due to conflicts with window management in my environment. I use this frequently to navigate to modified lines.
Navigate between files:
CTRL+E
: Recent files. I use this constantly, especially when toggling between two files.CTRL+SHIFT+N
: Search for and open a project file.ALT+F1
+Enter
: Show the current open file in the file tree ('Project view') without manually clicking through folders.
Things I use occasionally:
SHIFT+SHIFT
: Search everywhere. The specialized variants are also useful.CTRL+SHIFT+N/V
: Inline/extract variable.CTRL+SHIFT+I
: Quick info. A preview of where 'go to declaration' would go. Convenient to avoid navigating away.CTRL+P
: Parameter info. Useful for a quick look at a function signature.CTRL+SHIFT+P
: Type info for the thing under cursor.CTRL+SHIFT+ALT+C
: Copy the path to the current line. Something like:src/scripts/cli.py:13
. Useful for sharing specific code locations with coworkers. The command is part of a 'Copy Path/reference' menu.CTRL+ALT+Z
: Version control 'revert'. I often use this to view the code before my changes. I revert my changes to see the original code, then 'undo' the revert. The gutter color indicates the lines that will be affected.
Things I don't really use:
- Bookmarks: I don't use them much, though they seem useful when frequently going back to specific locations.
- Auto reformat code: On most projects I favor precommit hooks for consistent formatting. I might still use the shortcut while writing code but I don't care that it is not the source of truth.
- Navigation between tabs (and more) with the 'Switcher' (
CTRL+TAB
). I'm happy enough with 'recent files' and other shortcuts.
Read the "Navigate to data files using 'go to source'" sectionNavigate to data files using 'go to source'
Sometimes the code loads some data files and I want to view their content (fairly common in tests).
For example:
@pytest.fixture
def response_data_ok() -> dict:
return load_file("response_data_ok.json")
Goal: With the cursor on the filename string, 'go to declaration' opens the relevant json file.
This works if:
-
The
load_file
function uses correct type hints:def load_file(fname: os.PathLike | str): with open(os.path.join(TEST_DATA_DIR, fname), "rb") as f: return json.load(f)
-
In PyCharm, the folder where the json file lives is marked as Resource root (right click > mark directory as > Resource root).
My understanding is that PyCharm looks for file names in all resource folders and jumps to the first match (meaning this isn't perfect and conflicts might occur).
Read the "Personal preference: old 'local changes' tab" sectionPersonal preference: old 'local changes' tab
For a couple of years I have been using an old version of the 'local changes' tab. I find it nice to git reset HEAD~1
and have the highlighting of all changes in the commit.
The new UI was introduced a while ago but the old one can still be restored by unchecking Settings > Version Control > Commit > Use non-modal commit interface.
Other people sharing my feeling.
Read the "Open issues" sectionOpen issues
If you have a solution for these issues I'd love to hear them!
-
Noisy import suggestions: Sometimes, with many packages installed in a Python virtual environment, PyCharm offers multiple versions of imports. For example, with pytest, it might suggest
celery.contrib.pytest
orjedi.plugins.pytest
.
It adds a tiny bit of friction to the 'auto import' feature because I need to pick the right option (typically not the first one). -
The setup that allows to navigate to data files seems to mess up renaming a file (this only relates to non-python files, for instance the json file from the fixture). This is a very marginal issue but if there's a simple solution I'd take it!