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

PyCharm Code Editing

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:

Custom Highlight:

  1. 设置 → 编辑器 → 配色方案(Color Scheme)

  2. 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:


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

  1. Hold Alt(Win) or ⌥(Mac) + mouse drag

  2. Or use Alt + Shift + Insert Toggle column mode

Application scenarios:

# 批量修改多个变量前缀

old_name = 1    →  new_name = 1

old_value = 2   →  new_value = 2

Multi-cursor editing


Code Formatting and Refactoring

Auto format code

  1. Format current file:

  2. Ctrl + Alt + L (Win/Linux)

  3. ⌥ + ⌘ + L (Mac)

  4. -

    Ctrl + Alt + L (Win/Linux)

    -

    ⌥ + ⌘ + L (Mac)

    1. Format selected code:

    2. Select code → right click → Reformat Code

    3. -

      Select code → right click → Reformat Code

      Configure formatting rules:
      设置 → 编辑器 → 代码样式 → Python


      Rename variable/function (safe refactoring)

      1. Select identifier (variable/function name)

      2. Right click → Refactor → Rename

      3. Or shortcut Shift + F6

      4. Enter new name → press Enter

      effect:


      Extract Method

      Extract selected code snippet into a new method:

      1. Select code block

      2. Ctrl + Alt + M (Win)/ ⌘ + ⌥ + M (Mac)

      3. 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:

      1. Cursor positioned on method name/variable

      2. 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:

      1. Enter abbreviation (such as iter) → press Tab

      2. Preset templates:

      3. main → generate if __name__ == '__main__'

      4. try → generate complete try-except block

      5. -

        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:

        Operation: cursor positioned at warning/suggestion → Alt + Enter


        Summary: Efficient Editing Workflow

        1. Writing stage:

        2. Use code completion (Ctrl + Space) to quickly input

        3. Use live templates to generate repetitive structures

        4. -

          Use code completion (Ctrl + Space) to quickly input

          -

          Use live templates to generate repetitive structures

          1. Optimization stage:

          2. Format code (Ctrl + Alt + L)

          3. Using Alt + Enter Quickly fix problems

          4. -

            Format code (Ctrl + Alt + L)

            -

            Using Alt + Enter Quickly fix problems

            1. Refactoring stage:

            2. Extract method (Ctrl + Alt + M)

            3. Safe rename (Shift + F6)

            4. -

              Extract method (Ctrl + Alt + M)

              -

              Safe rename (Shift + F6)