Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

PyCharm Code Navigation

PyCharm Code Navigation

Code navigation shortcuts

Mastering shortcuts lets you move quickly through code and free yourself from the mouse.

Below are the most commonly used navigation shortcuts in daily development.

Core navigation shortcuts

Function Windows/Linux macOS Description
Jump to definition Ctrl+B Cmd+B Jump to the definition of a variable, function, or class
Find usages Alt+F7 Alt+F7 Find all reference locations of the current symbol in the project
Global search Double-press Shift Double-press Shift Search for files, classes, symbols, actions, and everything else
Recent files Ctrl+E Cmd+E Open the list of recently edited files
Go back to the previous location Ctrl+Alt+Left Cmd+[ Return to the previous cursor position
Go forward to the next location Ctrl+Alt+Right Cmd+] Move forward to the next cursor position
Jump to line Ctrl+G Cmd+L Enter a line number to jump directly
Find file Ctrl+Shift+N Cmd+Shift+O Search by file name and open the file
File structure Ctrl+F12 Cmd+F12 Show the list of classes and methods in the current file

Double-press Shift for global search

Double-pressing Shift is the most powerful search feature in PyCharm.

It can search for files, class names, symbols, IDE actions, and everything else, fulfilling almost any navigation need.

How to use it:

Double-pressing Shift is the first shortcut beginners should master; once familiar, you can skip the tedious step-by-step search through the project file tree.

Ctrl+B to jump to definition

Place the cursor on a variable, function name, or class name and press Ctrl+B to jump to its definition.

While holding Ctrl, hovering the mouse over code also shows a clickable link effect.

Example: In the code below, place the cursor on my_func and press Ctrl+B to jump to the function definition.

Example

Define a simple function

def

my_func

(

)

:

   

"""
    This is a sample function for the runoob tutorial
    """

   

print

(

'RUNOOB - Learn PyCharm Quick Navigation'

)

Call the function - place the cursor on my_func and press Ctrl+B to jump to the definition

my_func

(

)

Alt+F7 to find usages

Place the cursor on the symbol to find, press Alt+F7 to view all its reference locations in the project.

The search results appear in the Find tool window at the bottom; click an entry to jump to the corresponding location.


Code Formatting

Keeping a consistent code style is an important foundation for team collaboration.

PyCharm provides automatic code formatting; one click makes your code neat and uniform.

Format via shortcut

Use Ctrl+Alt+L (Windows/Linux) or Cmd+Option+L (macOS) to format the current file.

You can select part of the code and press the shortcut to format only the selection.

Formatting rule configuration

Formatting rules can be configured under Settings > Editor > Code Style > Python.

Configurable items include:

Before and after formatting comparison

Before formatting (uneven indentation, messy blank lines):

Example

Messy code before formatting

def

messy_function

(

x

,

y

)

:

      result

=

x+y

   

if

result

10

:

       

print

(

'Result is greater than 10'

)

         

print

(

'This line has incorrect indentation'

)

name

=

"runoob"

print

(

"Test"

,

name

)

After formatting (uniform indentation, standard blank lines):

Example

Neat code after formatting

def

messy_function

(

x

,

y

)

:

    result

=

x + y

   

if

result

10

:

       

print

(

'Result is greater than 10'

)

       

print

(

'This line has incorrect indentation'

)

name

=

"runoob"

print

(

"Test"

,

name

)

Press Ctrl+Alt+L to format your code before committing; this is a good programming habit.


To-do comments

To-do comments are a habit of marking special comments to record work that needs to be done.

Add to-do comments

In Python code, use '# to-do' to add a to-do comment.

Example

User management module

class

UserManager:

   

"""User manager"""

   

def

init

(

self

,

db_name

=

"runoob_users"

)

:

       

self

.

db_name

=

db_name

   

To-do: Implement user login functionality

   

def

login

(

self

,

username

,

password

)

:

       

pass

   

To-do: Add input validation logic

   

To-do: Consider using bcrypt to encrypt passwords

   

def

register

(

self

,

username

,

password

)

:

       

"""Register new user"""

       

Currently a simplified implementation that only prints a hint

       

print

(

f

'Registering user: {username}'

)

   

FIXME: This line of code fails under special conditions and needs fixing

   

def

delete_user

(

self

,

user_id

)

:

       

"""Delete user"""

       

print

(

f

'Deleting user ID: {user_id}'

)

   

NOTE: The following methods depend on an external API and require internet access

   

def

sync_data

(

self

)

:

       

"""Sync data"""

       

pass

To-do tag types

PyCharm recognizes the following special comment tags by default:

tag Purpose
TODO Marks work that needs to be done
FIXME Marks issues that need fixing
NOTE Adds important notes

View the to-do list

Click the to-do tab in the tool window area to open the to-do tool window.

This window centrally displays all to-do, FIXME, and NOTE comments in the project and supports filtering by scope.

You can set the filter scope:

Double-click a to-do entry to jump to the corresponding code line.

Using to-do comments well keeps the development process organized and ensures no pending item is missed.


Bookmark feature

The bookmark feature lets you mark important positions in your code for quick navigation.

Add a bookmark

Place the cursor on the code line you want to mark, then press F11 to add an anonymous bookmark.

Once added, a checkmark appears on the left side of that line.

Press Ctrl+F11 to add a numbered or lettered bookmark; in the pop-up, choose 0-9 or A-Z as the bookmark number.

Example

Project Configuration File

Add a bookmark here to quickly locate key configuration

DATABASE_CONFIG

=

{

   

"host"

:

"localhost"

,

 

Press F11 to add a bookmark

   

"port"

:

5432

,

       

Press Ctrl+F11, then choose number 1 to add a numbered bookmark

   

"name"

:

"runoob_db"

,

   

"user"

:

"admin"

,

}

Key constant definitions; you can add a bookmark here

MAX_CONNECTIONS

=

100

   

Press Ctrl+F11, then choose number 2 to add a numbered bookmark

TIMEOUT_SECONDS

=

30

View and manage bookmarks

Press Shift+F11 to open the bookmark list window and view all added bookmarks.

In the bookmark list window:

Numbered bookmarks can also be jumped to directly with Ctrl plus a number, without opening the bookmark list.

In large projects, bookmarks are a great tool for tracking multiple important code locations; adopting the habit is recommended.


Additional editing tips

Beyond the features above, the following editing tips also improve daily development efficiency.

Multi-cursor editing

Hold down Alt and click multiple positions with the mouse to edit several lines of code at once.

You can also use Ctrl+Shift+Alt+Click to place the cursor at any position.

Move code blocks

After selecting a code block, press Ctrl+Shift+Up or Down to move the entire block up or down.

No need to cut and paste; a single line or an entire method can be repositioned quickly.

Duplicate a whole line

Place the cursor on a line (no need to select it), then press Ctrl+D to duplicate the whole line.

This is a very frequently used shortcut; it is recommended to start using it immediately.

Delete a whole line

Place the cursor on a line, then press Ctrl+Y to delete the whole line.

Compared with selecting first and then pressing Delete, this is much faster.


Quick reference summary of shortcuts

The table below summarizes all shortcuts covered in this chapter; print or save it for easy reference.

Operation Windows/Linux macOS
Jump to definition Ctrl+B Cmd+B
Find usages Alt+F7 Alt+F7
Global search Double-press Shift Double-press Shift
Recent files Ctrl+E Cmd+E
Back / Forward Ctrl+Alt+Left/Right Cmd+[/]
Jump to line Ctrl+G Cmd+L
Find file Ctrl+Shift+N Cmd+Shift+O
File structure Ctrl+F12 Cmd+F12
Code Formatting Ctrl+Alt+L Cmd+Option+L
Add a bookmark F11 F11
Numbered bookmarks Ctrl+F11 Cmd+F11
Bookmark list Shift+F11 Shift+F11
Duplicate a whole line Ctrl+D Cmd+D
Delete a whole line Ctrl+Y Cmd+Delete

You do not need to memorize all these shortcuts at once; start with double-pressing Shift and practice two or three each day.

After using them consistently for a few weeks, you will see a qualitative improvement in coding efficiency.