VS Code 配置

VSCode Python 配置

安裝 Python 插件ext install pythonpython

安裝包(用於 Python 代碼的格式化):git

pip install pep8   
pip install --upgrade autopep8

將 Python 的安裝路徑添加到環境變量中。
Ctrl+Shift+B 生成 tasks.json 文件,編輯:github

{
    // See https://go.microsoft.com/fwlink/?LinkId=733558
    // for the documentation about the tasks.json format
    "version": "0.1.0",
    "command": "python",
    "isShellCommand": true,
    "args": ["${file}"],
    "showOutput": "always"
}

調試

  • launch.json 文件中的 "stopOnEntry": true 改成 "stopOnEntry": false,這樣無斷點時按 Ctrl+F5F5,直接運行程序不調試。固然也能夠用此替代 Ctrl+Shift+Bjson

  • Python Console App:從外部終端顯示調試窗口,若 *.py 中有 input() 語句,這樣也就能夠在終端輸入。windows

Run Build Task 中文亂碼 BUG

解決 Tasks: Run Build Task 即:Ctrl+Shift+B 時亂碼 bug:
方法一:
Python 文件添加:app

import io
import sys
sys.stdout = io.TextIOWrapper(sys.stdout.buffer,encoding='utf8')
# 下面開始主程序
# .....

方法二:
tasks.json 文件添加 options 參數:ui

{
    "version": "0.1.0",
    "command": "python",
    "isShellCommand": true,
    "args": ["${file}"],
    "showOutput": "always",
    "options": {
        "env": {
            "PYTHONIOENCODING": "UTF-8"
        }
    }
}

方法三:spa

直接在系統環境變量中添加:PYTHONIOENCODING 項,值爲 UTF-8插件

研究下 bug 成因:調試

import sys
print(sys.stdout.encoding)

Ctrl+Shift+B 運行代碼上述代碼,輸出:cp936

Ctrl+F5 運行代碼上述代碼,輸出:utf8

參數說明:

  • ${workspaceRoot} -- the path of the folder opened in VS Code

  • ${file} -- 當前打開的文件

  • ${relativeFile} -- the current opened file relative to workspaceRoot

  • ${fileBasename} -- 當前打開文件的文件名

  • ${fileDirname} -- 當前打開文件所在的路徑

  • ${fileExtname} -- the current opened file's extension

  • ${cwd} -- the task runner's current working directory on startup

至關於在終端中運行:<command> args,如:python script.py

參見:Tasks

重構技巧

  • 重命名:F2

  • import 排序:Ctrl+Shift+P,輸入或選擇 Sort Imports 命令。可對 'python.sortImports' 選項設置快捷鍵。

  • 變量提取:選中要提取的表達式,Ctrl+Shift+P,輸入或選擇 Refactoring: Extract Variable 命令。可對 'python.refactorExtractVariable' 選項設置快捷鍵。

  • 方法提取:選中要提取的表達式,Ctrl+Shift+P,輸入或選擇 Refactoring: Extract Method 命令。可對 'python.refactorExtractMethod' 選項設置快捷鍵。

相關快捷鍵

快捷鍵 說明
ctrl+shift+k 刪除當前行
ctrl+del 刪除光標右側的全部字
alt+shift+f 格式化代碼
ctrl+d 選中下一個匹配項
ctrl+enter 在當前行下邊插入一行
ctrl+shift+enter 在當前行上方插入一行
shift+alt+up/down 向上/向下複製一行
alt+up/down 上/下移動一行
home/end 移動到行首/尾
ctrl+home/end 移動到文件開頭/結尾
ctrl+shift+] 移動到後半個括號
shift+home 選擇從行首到光標處
shift+end 選擇從光標到行尾
shift+alt+a /**/ 風格註釋
ctrl+/ // 風格註釋
shift+alt+f 格式化代碼
ctrl+shift+c 打開系統終端
ctrl+` 打開 VSCode 集成終端

ctrl+shift+p技巧

ctrl+shift+p 輸入(或簡寫,有提示):

  • insert Snippet -- 選擇代碼片斷。

  • compare -- 文件比較

  • reload -- 重啓窗口

參見:
Key Bindings for Visual Studio Code
PDF 版本快捷鍵彙總

Windows 快捷鍵

快捷鍵 說明
alt+tab 任務切換
win+e 打開「文件資源管理器」
win+tab 虛擬桌面
win+ctrl+left/right 多桌面切換
win+d 顯示/隱藏桌面
shift+鼠標右鍵 可看到「在此處打開命令窗口」、「複製爲路徑」選項
alt+雙擊文件(夾) 打開文件(夾)屬性
ctrl+shift+esc 打開任務管理器
相關文章
相關標籤/搜索