Skip to content

VS Code for Python: Quality of Life Improvements

This guide covers snippets, shortcuts, and settings that make editing Python in VSCode smoother. Everything here will run within the scope of our setup with two extensions: Python and Pylance.

Select between macOS and WSL2 at the top. The instructions will update accordingly below.

Note

If you're stuck at any step in the Setup section, stop and contact a TA. The other sections are optional.


  1. Start Linux, and open a WSL2 terminal. Navigate to the course folder (cd course281), and run code .
  2. Ensure that the Python and Pylance extensions are installed within WSL.

Note

The VS Code window runs on Windows, so all shortcut keybindings are Windows ones, even though the terminal and Python run in Linux.

  1. Open a zsh terminal. Navigate to the course folder(cd course281), and run code .

Note

If the command isn't found, open the Command Palette (Cmd+Shift+P) in VS Code and run Shell Command: Install 'code' command in PATH.

  1. Ensure that the Python and Pylance extensions are installed.

Turn off AI features

Recent VS Code versions include Copilot chat features without installing anything. We would like you to disable them in order to better facilitate your learning experience. Please follow these steps.

In a Linux terminal window:

  1. Go to the course folder, and list everything in it, including hidden folders:

    cd ~/course281 
    ls -a # (1)!
    
    1. The -a option includes hidden folders, whose names start with a period, such as .vscode.
  2. Optional: If .vscode isn't in the list, create it:

    mkdir .vscode
    
  3. The .vscode folder is where VS Code looks for workspace settings. We will open the settings file in here:

    cd .vscode
    code settings.json 
    
  4. Paste the following into the file, save it, and close it:

    {
        "chat.disableAIFeatures": true // (1)!
    }
    
    1. If the file already has settings in it, don't paste the braces. Add only this line inside the existing { }, and put a comma at the end of the line before it.

In a zsh terminal window:

  1. Go to the course folder, and list everything in it, including hidden folders:

    cd ~/course281
    ls -a # (1)!
    
    1. The -a option includes hidden folders, whose names start with a period, such as .vscode.
  2. Optional: If .vscode isn't in the list, create it:

    mkdir .vscode
    
  3. The .vscode folder is where VS Code looks for workspace settings. We will open the settings file in here:

    cd .vscode
    code settings.json
    
  4. Paste the following into the file, save it, and close it:

    {
        "chat.disableAIFeatures": true // (1)!
    }
    
    1. If the file already has settings in it, don't paste the braces. Add only this line inside the existing { }, and put a comma at the end of the line before it.

Also avoid the IntelliCode extension, which ranks completions with a machine-learning model.

Optional

A snippet is a saved block of text that VS Code inserts when you type a short keyword, called a prefix, and press Tab. You define snippets in a JSON file.

The following snippet adds a standard header to a new Python file: a description of the file (a docstring) with the file name, author's name, and the date, followed by the numpy and matplotlib imports. After the snippet is inserted, the cursor stops at each placeholder in turn, so you can fill it in.

To create a snippets file:

  1. Open VSCode from the course281 directory.

  2. Press Ctrl+Shift+P to launch the Command Palette. Here, search for Snippets: Configure Snippets.

  3. Click on New Snippets file for 'course281'.... Name this file config when prompted.

  4. On line 1 of the file, copy and paste the following:

  "Course 281 header": {
    "scope": "python",
    "prefix": "init",
    "body": [
      "\"\"\"",
      "${1:Please enter a brief description of this file.}",
      "",
      "Physics 281",
      "Author: ${2:Your name}",
      "Date: ${CURRENT_YEAR}-${CURRENT_MONTH}-${CURRENT_DATE}",
      "\"\"\"",
      "",
      "import numpy as np",
      "import scipy",
      "import matplotlib",
      "import pandas as pd",
      "import matplotlib.pyplot as plt",
      "",
      "$0"
    ],
    "description": "Docstring and imports for course281 Python files"
  }
  1. Open VSCode from the course281 directory.

  2. Press Cmd+Shift+P to launch the Command Palette. Here, search for Snippets: Configure Snippets.

  3. Click on New Snippets file for 'course281'.... Name this file config when prompted.

  4. Next to the open brace on line 1, copy and paste the following.

  "Course 281 header": {
    "scope": "python",
    "prefix": "init",
    "body": [
      "\"\"\"",
      "${1:Please enter a brief description of this file.}",
      "",
      "Physics 281",
      "Author: ${2:Your name}",
      "Date: ${CURRENT_YEAR}-${CURRENT_MONTH}-${CURRENT_DATE}",
      "\"\"\"",
      "",
      "import numpy as np",
      "import scipy",
      "import matplotlib",
      "import pandas as pd",
      "import matplotlib.pyplot as plt",
      "",
      "$0"
    ],
    "description": "Docstring and imports for course281 Python files"
  }

To use this snippet, open a new Python file in VSCode. Type init and press Tab. Your snippet should autofill.

Snippet syntax
${1:text} is a tab stop with placeholder text. The cursor visits $1, $2, and so on in order, and ends at $0. Press Tab and Shift+Tab to move between them. and fill them out.

Try using these. If you don't like them/are uncomfortable, you can work without them just fine.

Action Shortcut Notes
Multi-cursors Pretty useful for bulk renaming
Select next match Ctrl+D Each press adds one match
Select all matches Ctrl+Shift+L
Skip current match Ctrl+K+D
Add cursor by click Alt + Click
Undo last cursor Ctrl+U
Add cursor above or below Ctrl+Alt+Up / Ctrl+Alt+Down
Column (box) select Shift+Alt + drag
Exit multi-cursor Esc
Selection
Select line Ctrl+L Repeat to extend
Expand selection Shift+Alt+Right Word, then expression, line, block
Move line Alt+Up / Alt+Down
Duplicate line Shift+Alt+Down
Delete line Ctrl+Shift+K
Toggle comment Ctrl+/
Folding Collapses blocks for readability.
Fold or unfold block Ctrl+Shift+[ / Ctrl+Shift+]
Fold all Ctrl+K, Ctrl+0
Unfold all Ctrl+K, Ctrl+J
Action Shortcut Notes
Multi-cursors Pretty useful for bulk renaming
Select next match Cmd+D Each press adds one match
Select all matches Cmd+Shift+L
Skip current match Cmd+K+D
Undo last cursor Cmd+U
Add cursor by click Option + Click
Add cursor above or below Cmd+Option+Up / Cmd+Option+Down
Column (box) select Shift+Option + drag
Exit multi-cursor Esc
Selection
Select line Cmd+L Repeat to extend
Expand selection Ctrl+Shift+Cmd+Right Word, then expression, line, block
Move line Option+Up / Option+Down
Duplicate line Shift+Option+Down
Delete line Cmd+Shift+K
Toggle comment Cmd+/
Folding Collapses blocks for readability.
Fold or unfold block Cmd+Option+[ / Cmd+Option+]
Fold all Cmd+K, Cmd+0
Unfold all Cmd+K, Cmd+J
Action Shortcut Notes
Rename symbol F2 Scope-aware, across files
Go to definition F12 or Ctrl + Click Works into libraries
Peek definition Alt+F12 Inline view
Show hover docs Ctrl+K+I Or hover with the mouse
Trigger autocomplete Ctrl+Space
Quick fix Ctrl+. Rule-based, such as adding a missing import
Problems panel Ctrl+Shift+M
Next problem F8
Action Shortcut Notes
Rename symbol Fn+F2
Go to definition F12 Works into libraries
Peek definition Option+F12 Inline view, no file switch
Show hover docs Cmd+K+I Or hover with the mouse
Trigger autocomplete Ctrl+Space
Quick fix Cmd+. Rule-based, such as adding a missing import
Problems panel Cmd+Shift+M
Next problem F8
Action Shortcut Notes
Run file Ctrl+R Custom binding; see Keybindings
Run without debugging Ctrl+F5 Built-in alternative
Run selection in REPL Shift+Enter Read-eval-print loop in the terminal
Debug F5 Click the left margin to set breakpoints
Select interpreter Ctrl+Shift+P, then Python: Select Interpreter Choose a Linux Python, such as /usr/bin/python3 or a virtual environment
Action Shortcut Notes
Run file Cmd+R Custom binding; see Keybindings
Run without debugging Ctrl+F5 Ctrl, not Cmd
Run selection in REPL Shift+Enter Read-eval-print loop in the terminal
Debug F5 Click the left margin to set breakpoints
Select interpreter Cmd+Shift+P, then Python: Select Interpreter
Action Shortcut
Command Palette Ctrl+Shift+P
Open file by name Ctrl+P
Jump to function or class Ctrl+Shift+O
Go to line Ctrl+G
Split editor Ctrl+\
Toggle sidebar Ctrl+B
Zoom in, out, reset Ctrl+= / Ctrl+- / Ctrl+Num 0
Action Shortcut
Command Palette Cmd+Shift+P
Open file by name Cmd+P
Jump to function or class Cmd+Shift+O
Go to line Ctrl+G
Split editor Cmd+\
Toggle sidebar Cmd+B
Zoom in, out, reset Cmd+= / Cmd+- / Cmd+Num 0
Action Shortcut Notes
Toggle terminal Ctrl+`
New terminal Ctrl+Shift+`
Split terminal Ctrl+Shift+5 With the terminal focused
Copy Ctrl+C With a selection; otherwise sends an interrupt
Paste Ctrl+V
Clear Ctrl+L
Search command history Ctrl+R Type part of an earlier command

The default shell is bash; its config file is ~/.bashrc.

Action Shortcut Notes
Toggle terminal Ctrl+` Ctrl, not Cmd
New terminal Ctrl+Shift+`
Split terminal Cmd+\ With the terminal focused
Copy Cmd+C
Paste Cmd+V
Clear Ctrl+L or Cmd+K Cmd+K also clears scrollback
Search command history Ctrl+R Type part of an earlier command

The default shell is zsh; its config file is ~/.zshrc.

Useful additions to your shell config file:

alias py=python3
alias ll='ls -lah'

After editing the file, run source ~/.bashrc or source ~/.zshrc, or open a new terminal.

Put the following in course281/.vscode/settings.json. Paste only the code if outermost braces are already present.

{
  "files.autoSave": "onFocusChange",
  "files.trimTrailingWhitespace": true,
  "files.insertFinalNewline": true,
  "editor.rulers": [100],
  "editor.mouseWheelZoom": true,
  "editor.minimap.enabled": false,
  "editor.stickyScroll.enabled": true,
  "python.analysis.inlayHints.variableTypes": true,
  "python.analysis.inlayHints.functionReturnTypes": true,
  "python.analysis.autoImportCompletions": true,
  "python.analysis.typeCheckingMode": "basic",
  "python.terminal.executeInFileDir": true,
  "chat.disableAIFeatures": true,
  "terminal.integrated.scrollback": 10000,
  "terminal.integrated.copyOnSelection": true,
  "terminal.integrated.fontSize": 13
}

Setting Effect
files.autoSave Autosaves when focus moves elsewhere, so you never run a stale file
files.trimTrailingWhitespace Removes trailing spaces on save
files.insertFinalNewline Ends every file with a newline
editor.rulers Draws a line-length guide at column 100
editor.mouseWheelZoom Ctrl + scroll (Cmd + scroll on macOS) resizes editor text
editor.minimap.enabled Hides the code overview on the right edge
editor.stickyScroll.enabled Pins the current function or class header while scrolling
python.analysis.typeCheckingMode Underlines likely bugs, such as wrong argument counts
terminal.integrated.scrollback Keeps 10,000 lines of terminal output
terminal.integrated.copyOnSelection Copies terminal text when you select it
terminal.integrated.fontSize Sets the terminal font size

Keybindings are per user, not per workspace. To open the file, open the Command Palette and run Preferences: Open Keyboard Shortcuts (JSON).

[
  {
    "key": "ctrl+r",
    "command": "python.execInTerminal",
    "when": "editorLangId == python && editorTextFocus"
  }
]

This file lives on the Windows side and applies to WSL windows too. In Python editors, it overrides Open Recent (Ctrl+R). The editorTextFocus condition keeps Ctrl+R as history search in the terminal.

[
  {
    "key": "cmd+r",
    "command": "python.execInTerminal",
    "when": "editorLangId == python && editorTextFocus"
  }
]

python.execInTerminal is the command behind the ▷ (Run) button.