PyCharm Code Editor
PyCharm, as a professional Python IDE, offers powerful code editing features.
Core Features of Code Editing
This section will introduce the core functions of code editing, including intelligent completion, shortcut operations, and code refactoringβefficient development techniques.
Basic Editing Features
Code Completion
| Completion Type | Trigger Method | Description |
|---|---|---|
| Basic Completion | Ctrl + Space (Win/Linux), β + Space (Mac) | Displays suggestions for variables, functions, classes, etc. |
| Smart Type Completion | Ctrl + Shift + Space | Recommends more precise types based on context. |
| File Name Completion | Automatically triggered when entering a path | Quickly completes file paths. |
| Dynamic Template Completion | Enter an abbreviation (e.g., main + Tab) | Quickly generates code snippets (e.g., if __name__ ==...). |
Input im + Tab β Automatically completes to import:
Input for + Tab β Generates a complete for loop structure:
Syntax Highlighting
PyCharm automatically colors code elements:
- Keywords (def, class): Blue
- Strings: Green
- Comments: Gray
- Incorrect Syntax: Red wavy line
Customizing Highlighting:
- Go to Settings β Editor β Color Scheme
- Adjust the colors for Python syntax elements
Code Folding
| Action | Shortcut (Win/Linux) | Shortcut (Mac) |
|---|---|---|
| Fold Current Block | Ctrl + - | β + - |
| Unfold Current Block | Ctrl + + | β + + |
| Fold All Blocks | Ctrl + Shift + - | β + Shift + - |
| Unfold All Blocks | Ctrl + Shift + + | β + Shift + + |
Hover over the left side of the editor area to see a downward arrow; clicking it also folds the code.
Supported Code Structures for Folding:
- Functions/Methods
- Class Definitions
- Multiline Comments
- Import Statement Groups
Shortcuts and Efficient Operations
| Function | Shortcut (Win/Linux) | Shortcut (Mac) |
|---|---|---|
| Copy Current Line | Ctrl + D | β + D |
| Delete Current Line | Ctrl + Y | β + Delete |
| Move Line | Alt + Shift + β/β | β₯ + β§ + β/β |
| Quick Fix Suggestions | Alt + Enter | β₯ + Enter |
| Jump to Definition | Ctrl + B | β + B |
| View Parameter Hints | Ctrl + P | β + P |
Multi-Line Editing and Batch Operations
Column Selection Mode
1. Hold down Alt (Win) or β₯ (Mac) while dragging with the mouse.
2. Alternatively, use Alt + Shift + Insert to switch to column mode.
Application Scenarios:
- Bulk modification of variable prefixes: old_name = 1 β new_name = 1, old_value = 2 β new_value = 2
Multi-Cursor Editing
1. Hold down Alt and click with the mouse (to add multiple cursors).
2. Or press Ctrl + G (Win) / β + G (Mac) to select identical words.
Code Formatting and Refactoring
Automatic Code Formatting
1. **Format Current File**:
- Press Ctrl + Alt + L (Win/Linux)
- Press β₯ + β + L (Mac)
2. **Format Selected Code**:
- Select the code β Right-click β Reformat Code
Configuring Formatting Rules:
- Go to Settings β Editor β Code Style β Python
Rename Variables/Functions (Safe Refactoring)
1. Select the identifier (variable/function name).
2. Right-click β Refactor β Rename
3. Or use the shortcut Shift + F6
4. Enter the new name β Press Enter
Effect:
- All references to this identifier are updated simultaneously.
- Supports renaming across files.
Extract Method
Extract a selected code snippet into a new method:
- Select the code block.
- Press Ctrl + Alt + M (Win) / β + β₯ + M (Mac).
- Enter the method name β Confirm.
Example:
# Before Extraction
def process_data(data):
cleaned = []
for item in data:
if item.is_valid():
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):
return item.is_valid()
Inline
Inline a method or variable into its call site:
- Place the cursor at the method name or variable.
- Press Ctrl + Alt + N (Win) / β + β₯ + N (Mac).
Before Inline:
def calculate_discount(price):
return price * 0.9
total = calculate_discount(100)
After Inline:
total = 100 * 0.9
Advanced Editing Techniques
Live Templates
Quickly generate commonly used code patterns:
- Enter an abbreviation (e.g., iter) β Press Tab
- Preset templates:
- main β Generates if __name__ == '__main__'
- try β Generates a complete try-except block
Customizing Templates:
- Go to Settings β Editor β Live Templates
In the code editor, typing "iter" displays template information:
Code Intent Actions (Alt+Enter)
Quickly fix or optimize code based on context:
- Automatically import missing packages
- Convert string formats (f-string / format)
- Optimize conditional expressions
Operation: Place the cursor on a warning or suggestion β Press Alt + Enter
Summary: Efficient Editing Workflow
- Writing Phase:
- Use code completion (`Ctrl + Space`) for quick input
- Use dynamic templates to generate repetitive structures
- Optimization Phase:
- Format code (`Ctrl + Alt + L`)
- Use `Alt + Enter` to quickly fix issues
- Refactoring Phase:
- Extract methods (`Ctrl + Alt + M`)
- Perform safe renames (`Shift + F6`)
YouTip