PyCharm Code Editing
As a professional Python IDE, PyCharm provides powerful code editing features.
This chapter will introduce in detail the core functions of code editing, including efficient development techniques such as intelligent completion, shortcut operations, code refactoring, etc.
Basic Editing Functions
Code Completion
PyCharm provides multiple intelligent completion methods:
| Completion Type | Trigger Method | Description |
|---|---|---|
| Basic Completion | Ctrl + Space (Win/Linux) |
|
⌘ + Space (Mac) |
Display suggestions for variables, functions, classes, etc. | |
| Smart Type Completion | Ctrl + Shift + Space |
Recommend more accurate types based on context |
| File Name Completion | Automatically triggered when entering a path | Quickly complete the file path |
| Live Template Completion | Enter abbreviation (such as main + Tab) |
Quickly generate code snippets (such as if __name__ ==...) |
Enter im + Tab → auto-complete to import:

Enter for + Tab → generate a complete for loop structure:

Syntax Highlighting
PyCharm automatically colors code elements:
-
The keyword (
def,class): blue -
String: green
-
Notes: gray
-
Error syntax: red wavy line
Custom Highlight:
-
设置 → 编辑器 → 配色方案(Color Scheme) -
Adjust the colors of Python syntax elements

Code Folding
| Operation | Shortcut (Win/Linux) | Shortcut (Mac) |
|---|---|---|
| Fold current code block | Ctrl + - |
⌘ + - |
| Expand current code block | Ctrl + + |
⌘ + + |
| Fold all code blocks | Ctrl + Shift + - |
⌘ + Shift + - |
| Expand all code blocks | Ctrl + Shift + + |
⌘ + Shift + + |
Move the mouse to the left of the editor code area and you can see a downward arrow; click it to collapse:

Code structures that support folding:
-
Function/Method
-
Class definition
-
Multi-line comment
-
Import statement group
Shortcuts and Efficient Operations
Common shortcut quick reference
| Function | Shortcut (Win/Linux) | Shortcut (Mac) |
|---|---|---|
| Copy current line | Ctrl + D |
⌘ + D |
| Delete current line | Ctrl + Y |
⌘ + Delete |
| Move line | Alt + Shift + ↑/↓ |
⌥ + ⇧ + ↑/↓ |
| Quick fix suggestion | Alt + Enter |
⌥ + Enter |
| Jump to definition | Ctrl + B |
⌘ + B |
| View parameter hints | Ctrl + P |
⌘ + P |
Multi-line editing and batch operations
Column Selection Mode
-
Hold
Alt(Win) or⌥(Mac) + mouse drag -
Or use
Alt + Shift + InsertToggle column mode
Application scenarios:
# 批量修改多个变量前缀
old_name = 1 → new_name = 1
old_value = 2 → new_value = 2
Multi-cursor editing
-
Hold
Alt+ mouse click (add multiple cursors) -
or
Ctrl + G(Win)/⌘ + G(Mac) select same word
Code Formatting and Refactoring
Auto format code
-
Format current file:
Ctrl + Alt + L (Win/Linux)
⌥ + ⌘ + L (Mac)
Ctrl + Alt + L (Win/Linux)
⌥ + ⌘ + L (Mac)
-
Format selected code:
Select code → right click → Reformat Code
Select code → right click → Reformat Code
Configure formatting rules:
设置 → 编辑器 → 代码样式 → Python

Rename variable/function (safe refactoring)
-
Select identifier (variable/function name)
-
Right click → Refactor → Rename
-
Or shortcut
Shift + F6 -
Enter new name → press
Enter
effect:
-
All references to this identifier are updated synchronously
-
Supports cross-file renaming
Extract Method
Extract selected code snippet into a new method:
-
Select code block
-
Ctrl + Alt + M(Win)/⌘ + ⌥ + M(Mac) -
Enter method name → confirm
Example
Before extraction
def
process_data
(
data
)
:
cleaned
=
[
]
for
item
in
data:
if
item.
is_valid
(
)
:
← select this part of the code
cleaned.
append
(
item
)
return
cleaned
After extraction
def
process_data
(
data
)
:
cleaned
=
[
]
for
item
in
data:
if
is_valid_item
(
item
)
:
cleaned.
append
(
item
)
return
cleaned
def
is_valid_item
(
item
)
:
← automatically generated new method
return
item.
is_valid
(
)
Inline
Inline the method/variable to the call site:
-
Cursor positioned on method name/variable
-
Ctrl + Alt + N(Win)/⌘ + ⌥ + N(Mac)
# 内联前
def calculate_discount(price):
return price * 0.9
total = calculate_discount(100) # ← 光标在此行
# 内联后
total = 100 * 0.9 # ← 方法调用被替换为实际代码
Advanced Editing Techniques
Live Templates
Quickly generate common code patterns:
-
Enter abbreviation (such as
iter) → pressTab -
Preset templates:
main → generate if __name__ == '__main__'
try → generate complete try-except block
main → generate if __name__ == '__main__'
try → generate complete try-except block
Custom Templates:
设置 → 编辑器 → 实时模板

Enter iter in the code editor to display template information:

Code Intention Actions (Alt+Enter)
Quickly fix or optimize code based on context:
-
Auto import missing packages
-
Convert string format (f-string / format)
-
Optimize conditional expressions
Operation: cursor positioned at warning/suggestion → Alt + Enter

Summary: Efficient Editing Workflow
-
Writing stage:
Use code completion (Ctrl + Space) to quickly input
Use live templates to generate repetitive structures
Use code completion (Ctrl + Space) to quickly input
Use live templates to generate repetitive structures
-
Optimization stage:
Format code (Ctrl + Alt + L)
Using Alt + Enter Quickly fix problems
Format code (Ctrl + Alt + L)
Using Alt + Enter Quickly fix problems
-
Refactoring stage:
Extract method (Ctrl + Alt + M)
Safe rename (Shift + F6)
Extract method (Ctrl + Alt + M)
Safe rename (Shift + F6)