YouTip LogoYouTip

Pycharm Code Editor

PyCharm Code Editor

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:

Image 1

Input for + Tab β†’ Generates a complete for loop structure:

Image 2

Syntax Highlighting

PyCharm automatically colors code elements:

  • Keywords (def, class): Blue
  • Strings: Green
  • Comments: Gray
  • Incorrect Syntax: Red wavy line

Customizing Highlighting:

  1. Go to Settings β†’ Editor β†’ Color Scheme
  2. Adjust the colors for Python syntax elements
Image 3

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.

Image 4

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
Image 5

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
Image 6

In the code editor, typing "iter" displays template information:

Image 7

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

Image 8

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`)
← Pycharm GitPycharm New Project β†’